我有一个闪亮的应用程序,它最初应该是空白的。当我按 "Show plot"
时,应该显示图,当我按 "Hide Plot"
时,应该隐藏图。
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
actionButton("showplot",
"Show plot"),
actionButton("hideplot",
"Hide plot")
),
mainPanel(
plotOutput(outputId = "car_plot")
)
)
)
server <- function(input, output) {
hidePlot <- reactiveVal(FALSE)
showPlot <- reactiveVal(TRUE)
observeEvent(input$hideplot, {
hidePlot(TRUE)
})
observeEvent(input$showplot, {
showPlot(TRUE)
})
output$car_plot <- renderPlot({
if (hidePlot()){
return(NULL)
}
else if (showPlot()){
plot(cars)
}
})
}
shinyApp(ui = ui, server = server)
答案 0 :(得分:1)
您只能使用hidePlot
:
observeEvent(input$hideplot, {
hidePlot(TRUE)
})
observeEvent(input$showplot, {
hidePlot(FALSE)
})
output$car_plot <- renderPlot({
if (hidePlot()){
return(NULL)
}
else {
plot(cars)
}
})
也许还有其他方式:
observeEvent(input$showplot, {
showPlot(TRUE)
})
observeEvent(input$hideplot, {
showPlot(FALSE)
})
output$car_plot <- renderPlot({
req(showPlot())
plot(cars)
})