如何在 Shiny 中隐藏条件面板?

How to hide a conditional panel in Shiny?

如何在 Shiny 中隐藏条件面板?请看下面的例子:

library(shiny)

ui <- fluidPage(
  actionButton("eval","Evaluate"),
  numericInput("num_input", "If number is changed, cp must hide", value = 0),
  conditionalPanel(
      condition = "input.eval",
      "text"))

server <- function(input, output, session) {
  observeEvent(input$num_input, {
    input$eval <- 0
  })}

shinyApp(ui, server)

我想要实现的是:一旦用户点击评估按钮,条件面板就会出现,但是一旦 num_input 中的数字发生变化,面板就会消失。我的想法是让评估按钮无效,但这不起作用(应用程序以灰色背景打开并且似乎冻结)。

我也像这样用 shinyjs 试过:

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  actionButton("eval","Evaluate"),
  numericInput("num_input", "If number is changed, cp must hide", value = 0),
  conditionalPanel(
      id = "cond_panel",
      condition = "input.eval",
      "text"))

server <- function(input, output, session) {
  observeEvent(input$num_input, {
    reset("cond_panel")})}

shinyApp(ui, server)

但这也行不通:应用程序会定期打开,点击评估按钮后会显示条件面板,但数字更改后什么也不会发生。

我从来没有玩过 conditionalPanel,所以不确定它是否有默认设置 hide/show。以下可能会为您提供所需的输出。

library(shiny)
library(shinyjs)

if(interactive()){
  shinyApp(
    ui <- fluidPage(
      shinyjs::useShinyjs(),
      actionButton("eval","Evaluate"),
      numericInput("num_input", "If number is changed, cp must hide", value = 0),
      shinyjs::hidden(
        div(
          id = "cp1",
          conditionalPanel(condition = "input.eval", 
                           textOutput("text1")))
        )
      ),

    server = function(input, output, session){
      output$text1 <- renderText({
        input$num_input
      })
      observeEvent(input$eval,{
        shinyjs::show("cp1")
      })
      observeEvent(input$num_input,{
        shinyjs::hide("cp1")
      })
    }
  )
}

我最初使用 shinyjs 隐藏 conditionalPanel,显示使用 renderText 输入的数字输入,并相应地有两个 observeEventhide\show 面板.

您可以创建一个输出值并将其仅用于条件面板。关于动态的文章 UI 解释了如何做到这一点:

http://shiny.rstudio.com/articles/dynamic-ui.html

The condition can also use output values; they work in the same way (output.foo gives you the value of the output foo). If you have a situation where you wish you could use an R expression as your condition argument, you can create a reactive expression in the server function and assign it to a new output, then refer to that output in your condition expression.

If you do this, make sure to also set outputOptions(output, [newOutputName], suspendWhenHidden = FALSE). (This is necessary because Shiny normally doesn’t send values to the browser for outputs that are hidden or not present in the UI. In this case, however, the browser does need to know the most up-to-date output value in order to correctly evaluate the condition of the contitionalPanel function - suspendWhenHidden = FALSE ensures this will happen.)

library(shiny)

ui <- fluidPage(
  actionButton("eval","Evaluate"),
  numericInput("num_input", "If number is changed, cp must hide", value = 0),
  conditionalPanel("input.eval && !output.hide_panel", "text")
)

server <- function(input, output, session) {

  output$hide_panel <- eventReactive(input$num_input, TRUE, ignoreInit = TRUE)

  outputOptions(output, "hide_panel", suspendWhenHidden = FALSE)
}

shinyApp(ui, server)

另一种方法是 renderUI 条件面板,并显示它直到 input$num_input 发生变化。