R 基于闪亮应用程序中的两个动作按钮隐藏和显示绘图
我有一个闪亮的应用程序,它下面最初应该是空白的。当我按R 基于闪亮应用程序中的两个动作按钮隐藏和显示绘图,r,shiny,R,Shiny,我有一个闪亮的应用程序,它下面最初应该是空白的。当我按“显示绘图”时,应显示绘图,当我按“隐藏绘图”时,应隐藏绘图 library(shiny) ui <- fluidPage( sidebarLayout( sidebarPanel( actionButton("showplot", "Show plot"), actionButton("hideplot&quo
“显示绘图”
时,应显示绘图,当我按“隐藏绘图”
时,应隐藏绘图
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)
库(闪亮)
ui您只能使用hidePlot
:
observeEvent(input$hideplot, {
hidePlot(TRUE)
})
observeEvent(input$showplot, {
hidePlot(FALSE)
})
output$car_plot <- renderPlot({
if (hidePlot()){
return(NULL)
}
else {
plot(cars)
}
})
observeEvent(输入$hideplot{
hidePlot(真)
})
observeEvent(输入$showplot{
hidePlot(假)
})
输出$car\u plot一旦按下按钮,您的反应值将被设置为TRUE
,但它们永远不会被设置回FALSE
。这可能以类似的方式实现,但使用'shinyjs'包的hide
和show
功能会更容易。不,事实上,这很容易:只使用一个布尔反应值,hidePlot
或showPlot
,而不是两者都使用。
observeEvent(input$showplot, {
showPlot(TRUE)
})
observeEvent(input$hideplot, {
showPlot(FALSE)
})
output$car_plot <- renderPlot({
req(showPlot())
plot(cars)
})