在 Shiny app 的 Divs 上生成不同的可点击事件

Generate different clickable events on Divs in Shiny app

我有一个非常简单的 Shiny 应用程序,如下所示 -

library(shiny)
ui <- fluidPage(
  div(id = "01", style = "cursor:pointer; height: 300px; width: 300px; text-align: center; background-color: green", HTML("01")),
  div(id = "02", style = "cursor:pointer; height: 300px; width: 300px; text-align: center; background-color: blue", HTML("02")),
  div(id = "03", style = "cursor:pointer; height: 300px; width: 300px; text-align: center; background-color: red", HTML("03")),

  plotOutput("plot")
)

shinyApp(ui, server = function(input, output) { })
}

在此框架内,我想在三个 divs 上实现可点击事件,其中点击第一个 div 会生成 cdf plot of Normal distribution,第二个 div 会生成t distribution 和第三个会生成 GED distribution.

我可以使用 drop-down box 等来实现同样的效果,或者,我也可以为每个 div 使用 3 个不同的 plotOutput()。然而,在我目前的情况下,我有太多这样的 divs,因此不可能有这么多 individual plotOutput()。所以我希望通过 individual divs 和一个输出来实现这样的功能。

有什么方法可以在 Shiny 中实现同样的效果吗?任何指针将不胜感激。

根据@bretauv shinyjs 具有 onclick 功能,因此您可以执行以下操作,请注意,您必须为图表提供分布,我只是在使用正常

library(shiny)
library(shinyjs)

ui <- fluidPage(
    useShinyjs(),  # Set up shinyjs
    column(3,
           div(id = "01", style = "cursor:pointer; height: 300px; width: 300px; text-align: center; background-color: green", HTML("01")),
           div(id = "02", style = "cursor:pointer; height: 300px; width: 300px; text-align: center; background-color: blue", HTML("02")),
           div(id = "03", style = "cursor:pointer; height: 300px; width: 300px; text-align: center; background-color: red", HTML("03")),
    ),
    column(9,
           plotOutput("plot")
    )
)

v <- reactiveValues()

normal <- function(v){
    v$x <- seq(-10, 10, by = .1)
    v$y <- rnorm(v$x)
    v$title <- "Normal distribution"
}

tdistr <- function(v){
    v$x <- seq(-10, 10, by = .1)
    v$y <- rnorm(v$x)
    v$title <- "T-distribution"
}

geddistr <- function(v){
    v$x <- seq(-10, 10, by = .1)
    v$y <- rnorm(v$x)
    v$title <- "GED distribution"
}

server = function(input, output,session) { 

    observe({
        onclick("01",normal(v))
        onclick("02",tdistr(v))
        onclick("03",geddistr(v))
    })

    output$plot <- renderPlot({
        req(v$x)
        plot(v$x,v$y,main = v$title)
    })

}

shinyApp(ui, server)