【问题标题】:Hide/show outputs Shiny R隐藏/显示输出 Shiny R
【发布时间】:2016-05-10 05:29:16
【问题描述】:

我正在尝试了解如何在每次用户更改 widjet 中的某些内容时显示和隐藏我的输出,例如图形和表格。例如,我的变量“性别”有一个滑块输入,有两个选择:男性和女性。我还有一个按钮,当用户点击它时执行估计。每次当用户在不同的widjets之间改变至少一个选择时,我想隐藏输出。例如,经过一次估计后,用户决定只更改教育水平,当用户单击滑块输入框时,我想隐藏以前的结果。

我尝试使用 R 包 shinyjs 和隐藏/显示功能,但它们不适用于输出。

你知道如何在不使用 shinyjs 包的情况下做到这一点吗?

这是我的代码的一部分:

shinyUI(fluidPage(

  sidebarLayout(
    fluidRow( 
      column(4, wellPanel(

  fluidRow(
      column(5,selectInput("gender",
                          label = div("Sexe",style = "color:royalblue"),
                          choices = list("Male", "Female"),
                          selected = "Female")),

        # other different widjets..        

              column(8, plotOutput('simulationChange')),
              column(4, tableOutput('simulationChangeTable'),
                                    tags$style("#simulationChangeTable table {font-size:9pt;background-color: #E5E4E2;font-weight:bold;margin-top: 121px; margin-left:-30px;overflow:hidden; white-space:nowrap;text-align:left;align:left;}", 
                                    media="screen", 
                                    type="text/css"), 
                  fluidRow(
                     column(6, tableOutput('simulationChangeEsperance'),
                                    tags$style("#simulationChangeEsperance table {font-size:9pt;background-color: #E5E4E2;font-weight:bold;margin-top: -10px; margin-left:-30px;overflow:hidden; white-space:wrap;word-break: break-word;width:173px;text-align:left;}"))
                  )
              )
            )
        )
     )
    ))  

shinyServer(function(input, output, session) {
# part of my server.R code
    observe({


   if (input$gender|input$age|input$birthplace|input$education){  
      shinyjs::hide("simulationChange")
      shinyjs::hide("simulationChangeTable")
      shinyjs::hide("simulationChangeEsperance")
      }      
})

谢谢。

【问题讨论】:

  • 看看在 ui.r 中使用的 conditionalPanel
  • 我是 shinyjs 的作者,它确实适用于输出。我尝试运行您的代码以查看问题所在,但您提供的代码错误太多,难以处理。如果你能提供一段更短的能够运行的代码,我就能明白为什么你的 shinyjs::hide() 不起作用
  • 嗨,很高兴认识你。谢谢您的回答。我有很长的 server.R 脚本和 ui.R 脚本。另外,我不能发布所有内容,因为我不被允许。这就是为什么我写了一部分代码当然不能单独工作。

标签: r shiny show-hide


【解决方案1】:

正如 Dieter 在 cmets 中提到的,您需要使用 conditionalPanel。例如,在您的 ui.R 中,而不是

plotOutput('simulationChange')

使用

conditionalPanel("output.show", plotOutput('simulationChange'))

并在您的 server.R 中添加以下内容:

values <- reactiveValues()
values$show <- TRUE

observe({
    input$gender
    input$age
    input$birthplace
    input$education
    values$show <- FALSE
})

output$show <- reactive({
    return(values$show)
})

另外,点击按钮时不要忘记更改values$show

observeEvent(input$button, {
    ...
    values$show <- TRUE
})

【讨论】:

  • 在闪亮的 1.0.5 中,我必须在条件中添加 == TRUE 才能使其工作。它看起来像这样:conditionalPanel("output.show == TRUE", plotOutput('simulationChange'))
【解决方案2】:

您的代码不起作用的原因是因为您没有调用useShinyjs()(如果您阅读文档或查看使用shinyjs 的任何示例,您会发现您必须调用@987654322 @ 在 UI 中)。

我无法复制您的代码,因为它有太多错误,但只是为了证明它确实适用于输出,这里有一个您可以运行的小示例。在您的项目中,只需在 UI 中的某处添加 shinyjs::useShinyjs()

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  actionButton("hideshow", "Hide/show plot"),
  plotOutput("plot")
)

server <- function(input, output, session) {
  output$plot <- renderPlot({
    plot(rnorm(100))
  })

  observeEvent(input$hideshow, {
    # every time the button is pressed, alternate between hiding and showing the plot
    toggle("plot")
  })
}

shinyApp(ui = ui, server = server)

【讨论】:

  • 我知道这只是一个例子,但我认为人们会参考这段代码,您是否也可以通过检查按钮是否除以 2 来添加 showhide observeEvent(input$hide, { if(input$hide%%2 ==0){show("plot")} if(input$hide%%2 !=0){hide("plot")} })
  • 这就是shinyjs::toggle() 函数的用途:)
  • 另请注意toggle() 中有一个condition 参数,以防您不想每次都交替,而是使用计算结果为真/假的表达式来确定是显示还是隐藏
  • 如何响应式更改按钮内的文本(从显示更改为隐藏,反之亦然)?
【解决方案3】:

这里的其他答案似乎没有提供正确/完整的答案。解决方案其实很简单。

你需要使用outputOptions(output, 'show', suspendWhenHidden = FALSE)

下面是一个示例代码,如果下拉选择为 2,则在 conditionalPanel 中显示文本,如果为 1,则隐藏。

  library(shiny)
  
  ui <- fluidPage(
    selectInput("num", "Choose a number", 1:2),
    
    conditionalPanel(
      condition = "output.show",
      "The selected number is 2 so this text is displayed. Change it back to 1 to hide."
    )
    
  )
  
  server <- function(input, output, session) {
    output$show <- reactive({
          input$num == 2 # Add whatever condition you want here. Must return TRUE or FALSE
      })
    
    outputOptions(output, 'show', suspendWhenHidden = FALSE)
  }
    
  shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 2021-09-08
    • 1970-01-01
    • 2017-04-25
    • 2021-10-06
    • 2019-04-10
    • 2017-12-01
    • 2021-11-01
    • 2018-09-30
    • 2018-02-27
    相关资源
    最近更新 更多