2017-09-17 103 views
0

我想預先選擇ggiraph :: renderggiraph()輸出中的某些點。有沒有辦法預先選擇ggiraph(R閃亮)中的點?

我可以做如下的閃亮應用程序,它可以讓我選擇點,然後在其他地方使用這些選定的點,像這樣:

dat <- data.table(x = 1:6, y = 1:6 %% 3, id = 1:6, status = rep(c('on','off'),3)) 

ui <- fluidPage( ggiraphOutput("plot"), 
        verbatimTextOutput("choices")) 

server <- function(input, output, session){ 


    output$plot <- renderggiraph({ 
    p <- ggplot(dat) + 
     geom_point_interactive(aes(x = x, y = y, data_id = id), size = 5) + 
     scale_color_manual(limits = c('on','off'),values = c('red','black')) 

    ggiraph(code = print(p), 
      hover_css = "fill:red;cursor:pointer;", 
      selection_type = "multiple", 
      selected_css = "fill:red;") 
    }) 

    output$choices <- renderPrint({ 

    input$plot_selected 
    }) 


} 

shinyApp(ui = ui, server = server) 

但有時我想有選擇的某些點之前,我初始化程序。 例如,如果點1,3和5已經在原點上,我希望用戶能夠將它們關閉。

所以我的問題是,是否有可能實現這樣的事情?

回答

0

是,通過session$sendCustomMessagesession$onFlushed

library(shiny) 
library(ggiraph) 
library(data.table) 
library(magrittr) 
dat <- data.table(x = 1:6, y = 1:6 %% 3, id = 1:6, status = rep(c('on','off'),3)) 

ui <- fluidPage( fluidRow(
    column(width = 7, 
     ggiraphOutput("ggobj")), 
    column(width = 5, verbatimTextOutput("choices")) 
)) 

server <- function(input, output, session){ 

    output$ggobj <- renderggiraph({ 
    p <- ggplot(dat) + 
     geom_point_interactive(aes(x = x, y = y, data_id = id), size = 5) + 
     scale_color_manual(limits = c('on','off'),values = c('red','black')) 

    ggiraph(code = print(p), 
      hover_css = "fill:red;cursor:pointer;", 
      selection_type = "multiple", 
      selected_css = "fill:red;") 
    }) 
    session$onFlushed(function(){ 
    session$sendCustomMessage(type = 'ggobj_set', message = 1:3) 
    }) 
    output$choices <- renderPrint({ 
    input$ggobj_selected 
    }) 


} 

shinyApp(ui = ui, server = server) 
+0

會議$ sendCustomMessage(類型= 'ggobj_set',消息=字符(0)) –

+0

謝謝!我真的很喜歡ggiraph - 這太棒了! – michael

+0

謝謝。你的問題很有趣。我會考慮一種不必調用sendCustomMessage的方法 –

相關問題