【问题标题】:Add click event to shiny app with 2 SunburstR plots使用 2 个 SunburstR 图将点击事件添加到闪亮的应用程序
【发布时间】:2021-02-04 20:36:18
【问题描述】:

我希望将点击事件添加到页面上有 2 个 sund2b 图的闪亮应用程序。我希望文本输出仅参考他们最后单击的任何图,但它似乎只对第一个图而不是第二个图作出反应。输出对一个来说很好,但我希望它对两者都有效。这是我所拥有的一个简化示例:


library(shiny)
library(sunburstR)

sequences <- read.csv(
    system.file("examples/visit-sequences.csv",package="sunburstR")
    ,header = FALSE
    ,stringsAsFactors = FALSE
)[1:200,]
sequences2 <- read.csv(
    system.file("examples/visit-sequences.csv",package="sunburstR")
    ,header = FALSE
    ,stringsAsFactors = FALSE
)[201:400,]

server <- function(input,output,session){
    #sunburst1
    output$sunburst <- renderSund2b({
        add_shiny(sund2b(sequences))
    })
    #sunburst2
    output$sunburst2 <- renderSund2b({
        add_shiny(sund2b(sequences2))
    })
    #sunburst click event
    selection <- reactive({
        input$sunburst_click
    })
    output$selection <- renderText(selection())
    
}
ui<-fluidPage(
    sidebarLayout(
        sidebarPanel(
        ),
        # plot sunburst
        mainPanel(
            sund2bOutput("sunburst"),
            sund2bOutput("sunburst2"),
            textOutput("selection")
        )
    )
)
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny sunburst-diagram


    【解决方案1】:

    点击事件输入的名称取决于输出名称(input$sunburst2_click 对应于output$sunburst2)。

    请检查以下内容:

    library(shiny)
    library(sunburstR)
    
    sequences <- read.csv(
      system.file("examples/visit-sequences.csv", package = "sunburstR"),
      header = FALSE,
      stringsAsFactors = FALSE
    )[1:200,]
    sequences2 <- read.csv(
      system.file("examples/visit-sequences.csv", package = "sunburstR"),
      header = FALSE,
      stringsAsFactors = FALSE
    )[201:400,]
    
    ui <- fluidPage(sidebarLayout(
      sidebarPanel(),
      # plot sunburst
      mainPanel(
        sund2bOutput("sunburst"),
        sund2bOutput("sunburst2"),
        textOutput("selection"),
        textOutput("selection2"),
        textOutput("selectionLatest")
      )
    ))
    
    server <- function(input, output, session) {
      #sunburst1
      output$sunburst <- renderSund2b({
        add_shiny(sund2b(sequences))
      })
      #sunburst2
      output$sunburst2 <- renderSund2b({
        add_shiny(sund2b(sequences2))
      })
      #sunburst click event
      selection <- reactive({
        input$sunburst_click
      })
      selection2 <- reactive({
        input$sunburst2_click
      })
      
      latestClick <- reactiveVal()
      
      observeEvent(input$sunburst_click, {
        latestClick(input$sunburst_click)
      })
      
      observeEvent(input$sunburst2_click, {
        latestClick(input$sunburst2_click)
      })
      
      output$selection <- renderText(paste("plot 1:", paste(selection(), collapse = " - ")))
      output$selection2 <- renderText(paste("plot 2:", paste(selection2(), collapse = " - ")))
      output$selectionLatest <- renderText(paste("plot 1 or 2:", paste(latestClick(), collapse = " - ")))
    }
    
    shinyApp(ui = ui, server = server)
    

    Here“官方”的例子可以找到。

    【讨论】:

      猜你喜欢
      • 2015-06-15
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      • 2020-06-16
      • 2017-07-27
      • 2023-03-27
      • 2013-04-10
      • 2020-09-12
      相关资源
      最近更新 更多