2016-03-25 16 views
8

Tôi có ứng dụng shiny và tôi muốn một số ggplot với số brush khi bắt đầu, vì vậy người dùng không cần phải chọn khu vực ưa thích nhất định mỗi khi ứng dụng bắt đầu. Sau đó tất nhiên người dùng có thể chọn khu vực khác nhau. Dưới đây là ví dụ để bắt đầu với:Có thể khởi chạy cọ trong ggplot trong ứng dụng sáng bóng không?

library(shiny) 
library(ggplot2) 

runApp(shinyApp(
    ui = fluidPage(plotOutput('plotA', brush = brushOpts(id = 'plotA_brush')), 
       plotOutput('plotZ')), 
    server = function(input, output, session) { 
    pollData <- reactivePoll(60 * 1000, session, 
          checkFunc = function(){ Sys.time() }, 
          valueFunc = function(){ data.frame(x = 1:100, y = cumsum(rnorm(100)))}) 
    output$plotA <- renderPlot({ 
     dt <- pollData() 
     ggplot(dt, aes(x, y)) + geom_line() 
    }) 
    ranges <- reactiveValues(x = NULL, y = NULL) 
    observe({ 
     brush <- input$plotA_brush 
     if(!is.null(brush)) { 
     ranges$x <- c(brush$xmin, brush$xmax) 
     ranges$y <- c(brush$ymin, brush$ymax) 
     } else { 
     ranges$x <- NULL 
     ranges$y <- NULL 
     } 
    }) 
    output$plotZ <- renderPlot({ 
     dt <- pollData() 
     ggplot(dt, aes(x, y)) + geom_line() + coord_cartesian(xlim = ranges$x, ylim = ranges$y) 
    }) 
    } 
)) 
+0

khởi nó với những gì mặc dù? Nó bắt đầu "khởi tạo" cho toàn bộ khu vực. Tôi nghi ngờ bạn muốn khởi tạo nó vào một phần cụ thể, nhưng phần nào? Bất cứ điều gì khác nhỏ hơn toàn bộ sự việc? –

+0

Có, bất cứ điều gì khác nhỏ hơn toàn bộ sự việc. Trong ví dụ cụ thể này, giả sử tôi muốn 'x' nằm trong phạm vi từ 25 đến 75 và' y' từ nhỏ nhất đến tối đa. –

Trả lời

0

Có, có thể.

Trong mã bên dưới, tôi đã thêm một vài dòng. Đầu tiên, tôi đã thêm set.seed(42) để đồ họa có thể tái sản xuất được. Thứ hai, có một số dput(brush) đã được nhận xét. Điều này rất hữu ích cho việc xác định bàn chải ban đầu mà tôi muốn có. Cuối cùng, trong môi trường observe kiểm soát ranges Tôi đã thêm ifelse được đặt để sử dụng mặc định brush là giá trị NULL từ đối tượng input$plotA_brush.

library(shiny) 
library(ggplot2) 
set.seed(42) 

runApp(shinyApp(
    ui = fluidPage(plotOutput('plotA', brush = brushOpts(id = 'plotA_brush')), 
       plotOutput('plotZ')), 
    server = function(input, output, session) { 


    pollData <- reactivePoll(60 * 1000, session, 
          checkFunc = function(){ Sys.time() }, 
          valueFunc = function(){ data.frame(x = 1:100, y = cumsum(rnorm(100)))}) 
    output$plotA <- renderPlot({ 
     dt <- pollData() 
     ggplot(dt, aes(x, y)) + geom_line() 
    }) 
    ranges <- reactiveValues(x = NULL, y = NULL) 
    observe({ 
     if (is.null(input$plotA_brush)) { 
     brush <- structure(list(xmin = 14.313925002001, xmax = 39.942241912585, ymin = 1.1077251080591, ymax = 5.5028180250535, mapping = structure(list(x = "x", y = "y"), .Names = c("x", "y")), domain = structure(list(left = -3.95, right = 104.95, bottom = -4.07771077213569, top = 9.69030145758825), .Names = c("left", "right", "bottom", "top")), range = structure(list(left = 32.3904099935947, right = 674.020527857828, bottom = 368.859578048224, top = 5.47945189149413), .Names = c("left", "right", "bottom", "top")), log = structure(list(x = NULL, y = NULL), .Names = c("x", "y")), direction = "xy", brushId = "plotA_brush", outputId = "plotA"), .Names = c("xmin", "xmax", "ymin", "ymax", "mapping", "domain", "range", "log", "direction", "brushId", "outputId")) 
     } else { 
     brush <- input$plotA_brush 
     } 
     # dput(brush) # Useful for finding the initial brush 
     if(!is.null(brush)) { 
     ranges$x <- c(brush$xmin, brush$xmax) 
     ranges$y <- c(brush$ymin, brush$ymax) 
     } else { 
     ranges$x <- NULL 
     ranges$y <- NULL 
     } 
    }) 
    output$plotZ <- renderPlot({ 
     dt <- pollData() 
     ggplot(dt, aes(x, y)) + geom_line() + coord_cartesian(xlim = ranges$x, ylim = ranges$y) 
    }) 
    } 
)) 

Các ban đầu trang bắt đầu trông như thế này:

enter image description here

Các vấn đề liên quan