Walleo
Walleo

Reputation: 41

Add click event to shiny app with 2 SunburstR plots

I am looking to add a click event to a shiny app that has 2 sund2b plots on the page. I would like the text output to just be in reference to whichever plot they clicked last, however it only seems to react to the first plot and not the second. The output is great for the one but I would like it to work for both. This is a simplified example of what I have:


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)

Upvotes: 1

Views: 267

Answers (1)

ismirsehregal
ismirsehregal

Reputation: 33452

The name of the click event input depends on the output name (input$sunburst2_click for output$sunburst2).

Please check the following:

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)

result

Here the "official" example can be found.

Upvotes: 2

Related Questions