gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:54:42 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com