david
david

Reputation: 825

renderUI with reactive value

I want to show a selectInput list when the reactive value values$myresults contains data. Let´s assume that values$myresults is filled in a function (not shown). When values$myresults is filled the list is shown as expected. However when the values$myresults is NULL an error message is displayed.

My idea is to only show the selectinput when there is data to show.

server.R

values = ReactiveValues(myresults=NULL)
  output$fcomparisons <- renderUI({
     selectInput("comparisons",label="Select comparisons",
                choices = resultsNames(values$myresults))
})

ui.R

  tabsetPanel(id = "foldchanges",type = "tabs",
              tabPanel(title="Summary", value=1, 
              verbatimTextOutput("summary_foldchanges")),
              tabPanel(title="Volcano Plot", value=2, 

              conditionalPanel(
                                                    condition='!(is.null(values$myresults))',
                                                    uiOutput("fcomparisons"),
                                                    plotOutput("volcanoplot")

Thanks

Upvotes: 0

Views: 775

Answers (1)

SeGa
SeGa

Reputation: 9809

First of all, please try to include a fully functioning example in future, with a complete shiny-app and some data. Otherwise your question will probably be downvoted and maybe not answered at all.

There is no function ReactiveValues, it should start with lower case r -> reactiveValues.

To hide the printed NULL, you can use req().

See the following example:

library(shiny)

ui <- fluidPage(
  uiOutput("fcomparisons"),
  actionButton("go", "setValues"),
  actionButton("go1", "setNULL"),
  verbatimTextOutput("printValues")
)

server <- function(input, output){

  values = reactiveValues(myresults=NULL)

  output$fcomparisons <- renderUI({
    selectInput("comparisons",label="Select comparisons",
                choices = values$myresults)
  })

  observeEvent(input$go, {
    values$myresults <- sample(1:20, 5, T)
  })  
  observeEvent(input$go1, {
    values$myresults <- NULL
  })

  output$printValues <- renderPrint({
    req(values$myresults)
    values$myresults
  })
}

shinyApp(ui, server)

Upvotes: 1

Related Questions