Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 基于闪亮应用程序中的两个动作按钮隐藏和显示绘图_R_Shiny - Fatal编程技术网

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)
  })