繁体   English   中英

隐藏/显示输出 Shiny R

[英]Hide/show outputs Shiny R

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

我尝试使用 R 包 Shinyjs 和函数 hide/show 但它们不适用于输出。

你知道如何在不使用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")
      }      
})

谢谢你。

你的代码没有工作的原因是因为你没有做出通话useShinyjs()如果你在阅读使用shinyjs的任何实例文档或看,你会看到,你必须调用useShinyjs()在用户界面)。

我无法复制您的代码,因为它有太多错误,但只是为了证明它确实适用于输出,这里有一个您可以运行的小示例。 在您的项目中,只需在 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)

正如 Dieter 在评论中提到的,您需要使用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
})

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

您需要使用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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM