【问题标题】:Is there a way to preselect points in ggiraph (R shiny)?有没有办法在 ggiraph (R shiny) 中预选点?
【发布时间】:2017-09-17 17:11:07
【问题描述】:

我想在 ggiraph::renderggiraph() 输出中预选一些点。

我可以制作以下闪亮的应用程序,它允许我选择点,然后像这样在其他地方使用这些选定的点:

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 最初已经“开启”,我希望用户能够将它们“关闭”。

所以我的问题是,是否有可能实现这样的目标?

【问题讨论】:

    标签: r shiny ggiraph


    【解决方案1】:

    是的,通过在session$onFlushed 中使用session$sendCustomMessage

    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)
    

    【讨论】:

    • session$sendCustomMessage(type = 'ggobj_set', message = character(0))
    • 谢谢!到目前为止,我真的很喜欢 ggiraph——它太棒了!
    • 谢谢。你的问题很有趣。我会考虑一种无需调用 sendCustomMessage 的方法
    猜你喜欢
    • 1970-01-01
    • 2020-07-31
    • 2019-10-20
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 2022-12-09
    • 2017-02-23
    • 1970-01-01
    相关资源
    最近更新 更多