Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

r - 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)
question from:https://stackoverflow.com/questions/66053447/add-click-event-to-shiny-app-with-2-sunburstr-plots

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...