使用 2 个 SunburstR 图将点击事件添加到闪亮的应用程序

Add click event to shiny app with 2 SunburstR plots

我希望将点击事件添加到页面上有 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)

点击事件输入的名称取决于输出名称(input$sunburst2_click for 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可以找到“官方”示例。