How can I have a hidden output variable?

I currently have to use conditional panel to control the visibility of the UI. It is apparently only possible to control conditions on input or output variables. However, I don't have any variable that can actually be connected. What I would like to have is to add an "invisible" output variable that is not associated to a visual component and that I can set on the server routine. This output variable will then be used in the conditional panel. Is it possible?

I hope there is a better way to do this because this is a horrible hack. I made the visibility of the plot conditional on whether output$Flag is a single space and then I rendered output$Flag in a convenient place. The alternative value of output$Flag is an empty string. I made the value of output$Flag depend on input$Comp because I do not know what you want the conditionalPanel to depend on.

library(shiny)
library(ggplot2)

set.seed(3)
DF <- data.frame(Date = seq.Date(from = as.Date("2019-01-01"), 
                                 to = as.Date("2019-01-24"), by = "day"),
                 Company = rep(c("A", "B"), 12), 
                 ApprovalID = sample(c("AD", "GF"), 24, replace = TRUE),
                 State = sample(c("NY", "CO"), 24, replace = TRUE),
                 Value = rnorm(24, mean = 10, sd = 3))
ui <- fluidPage(
  fluidRow(
  column(width = 3, selectInput("Comp", label = "Company",
                                choices = list("A", "B") ))
  ),
  fluidRow(
  column(width = 1, textOutput("Flag")),
  conditionalPanel(condition = "output.Flag == ' '", #
                   column(width = 6, plotOutput("Plot"))
  )
  )
)

server <- function(input, output) {
  Tbl <- reactive({
    DF %>% filter(Company == input$Comp)
  })
  
  output$Plot <- renderPlot({
    ggplot(Tbl(), aes(x = ApprovalID, y = Value)) + geom_boxplot()
  })
  
  output$Flag <- renderText({
    if (input$Comp == "A") Var <- " " else Var <- ""
    Var
  })
}

# Return a Shiny app object
shinyApp(ui = ui, server = server)

That was also what I suspected and assumed as a possible solution, but it's far from ideal: a dummy output that is there, but hidden. My first thought was to have a css visibility clause for the controlling element, but I was also hoping for a more elegant solution.

Hi @sbo. You can do it with output and set suspendWhenHidden to false. Then, you can set the output in server to control the conditionalPanel in ui.

ui <- fluidPage(
  fluidRow(
    conditionalPanel(
      condition = "output.dummy",
      ...
    )
  )
)


server <- function(input, output, session) {
  output$dummy <- reactive(FALSE)
  outputOptions(output, "dummy", suspendWhenHidden = FALSE)
  
  observe({
    if(...)
      output$dummy <- TRUE
  })
}
1 Like

That's clever. Thanks!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.