Hide/show outputs Shiny R

北城以北 提交于 2019-11-28 12:45:51

As mentioned by Dieter in the comments you need to use conditionalPanel. For example, in your ui.R, instead of

plotOutput('simulationChange')

use

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

And in your server.R add the following:

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:

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

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).

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.

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)
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!