隐藏/显示输出Shiny R.

Mil*_*ily 4 r show-hide shiny

我试图找出如何在每次用户更改widjets中的内容时显示和隐藏输出,如图形和表格.例如,我有一个名为"gender"的变量的滑块输入,有2个选项:男性和女性.我还有一个按钮,当用户点击它时执行估算.每当用户在不同的widjets之间更改至少一个选项时,我想隐藏输出.例如,在一次估计之后,用户决定仅更改教育级别,并且当用户单击sliderInput框时,我想隐藏先前的结果.

我试图使用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")
      }      
})
Run Code Online (Sandbox Code Playgroud)

谢谢.

Dea*_*ali 9

您的代码不起作用的原因是因为您没有打电话useShinyjs()(如果您阅读文档或查看使用shinyjs的任何示例,您将看到您必须useShinyjs()在UI中调用).

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

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)
Run Code Online (Sandbox Code Playgroud)


wik*_*lev 5

正如Dieter在评论中提到的那样,您需要使用conditionalPanel。例如,在您的ui.R中,

plotOutput('simulationChange')
Run Code Online (Sandbox Code Playgroud)

使用

conditionalPanel("output.show", plotOutput('simulationChange'))
Run Code Online (Sandbox Code Playgroud)

在您的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)
})
Run Code Online (Sandbox Code Playgroud)

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

observeEvent(input$button, {
    ...
    values$show <- TRUE
})
Run Code Online (Sandbox Code Playgroud)