简体   繁体   English

隐藏/显示输出 Shiny R

[英]Hide/show outputs Shiny R

I am trying to find out how to show and hide my outputs like graphics and tabels each time when the user change something in the widjets.我试图找出每次用户在 widjets 中更改某些内容时如何显示和隐藏我的输出,如图形和表格。 For instance I have a slider input for my variable called "gender" with 2 choices : male and female.例如,我有一个名为“性别”的变量的滑块输入,有 2 个选择:男性和女性。 I also have a button which executes estimations when the user click on it.我还有一个按钮,当用户点击它时执行估计。 I want to hide the outputs each time when the user changes at least one choice between the different widjets.每次当用户在不同的 widjet 之间更改至少一个选择时,我都想隐藏输出。 For instance after one estimation the user decides to change only the level of education and when the user click on the sliderInput box, I would like to hide the previous results.例如,经过一次估计后,用户决定仅更改教育水平,当用户单击滑块输入框时,我想隐藏以前的结果。

I tried to use the R package shinyjs and the functions hide/show but they are not working for outputs.我尝试使用 R 包 Shinyjs 和函数 hide/show 但它们不适用于输出。

Do you have any idea how to do it without using shinyjs package?你知道如何在不使用shinyjs包的情况下做到这一点吗?

Here is a part of my code:这是我的代码的一部分:

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")
      }      
})

Thank you.谢谢你。

The reason your code didn't work is because you didn't make a call to useShinyjs() (if you read the documentation or look at any examples of using shinyjs, you'll see that you HAVE to call useShinyjs() in the UI).你的代码没有工作的原因是因为你没有做出通话useShinyjs()如果你在阅读使用shinyjs的任何实例文档或看,你会看到,你必须调用useShinyjs()在用户界面)。

I couldn't replicate your code because it had too many errors, but just to demonstrate that it does work with outputs, here's a small example you can run.我无法复制您的代码,因为它有太多错误,但只是为了证明它确实适用于输出,这里有一个您可以运行的小示例。 In your project, just add shinyjs::useShinyjs() somewhere in the 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)

As mentioned by Dieter in the comments you need to use conditionalPanel .正如 Dieter 在评论中提到的,您需要使用conditionalPanel For example, in your ui.R, instead of例如,在您的 ui.R 中,而不是

plotOutput('simulationChange')

use

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

And in your server.R add the following:在你的 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)
})

Also, don't forget to change values$show , when clicking on your button:另外,不要忘记在单击按钮时更改values$show

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

The other answers here don't seem to provide the right/complete answer.这里的其他答案似乎没有提供正确/完整的答案。 The solution is actually quite simple.解决方法其实很简单。

You need to use outputOptions(output, 'show', suspendWhenHidden = FALSE)您需要使用outputOptions(output, 'show', suspendWhenHidden = FALSE)

Below is a sample code that displays the text inside a conditionalPanel if the dropdown selection is 2 and hides if it is 1.下面是一个示例代码,如果下拉选择为 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