我有一个非常简单的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) { })
}
Run Code Online (Sandbox Code Playgroud)
在此框架内,我想在这三个事件上实现一个可点击事件divs
,其中点击第一个div
将生成一个cdf plot of Normal distribution
,第二个div
将生成一个t distribution
,第三个将生成一个GED distribution
。
我可以使用 adrop-down box
等来实现相同的效果,或者,我也可以plotOutput()
为每个 div 使用 3 个不同的。然而,就我目前的情况而言,我有太多这样的人divs
,因此拥有这么多个人是不可行的plotOutput()
。所以我希望通过divs
具有一个输出的个体来实现这样的功能。
有什么办法可以达到同样的效果Shiny
吗?任何指针都将受到高度赞赏。
根据 @bretauvshinyjs
具有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)
Run Code Online (Sandbox Code Playgroud)