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

标签 r shiny shinyjs

如何在 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)

但这也不起作用:应用程序会定期打开并在单击评估按钮后显示条件面板,但一旦更改数字,则不会发生任何事情。

最佳答案

您可以创建一个输出值并将其仅用于条件面板。关于动态 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变化。

关于r - 如何在 Shiny 中隐藏条件面板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46735135/

相关文章:

r - 在 Shiny 的仪表板中调整信息框的高度

R中的按行排序

r - 如何在 Shinyapps.io for Google Sheets (googlesheets) 上验证 Shiny 应用程序的用户

jquery - R Shiny 的鼠标悬停到所有表格单元格

r - 在 R Shiny 应用程序中访问 Javascript 变量

r - 具有交互作用的线性回归图

css - 更改 Shiny 小部件的背景和字体颜色

r - 使用 renderUI 更新数据集而不是条件面板

R Shiny 必填字段调查表

r - 将向量中的唯一值对相乘并对结果求和