Using rendered selectInput as input in server function

Hi there,
I am using uiOutput in my ui code to create a selectInput based on renderUI in my server code, but I also want to use the output of the selectInput later on in my server code. How do I do this? Currently, the created selectInput is populated based on the selection in another selectInput in the ui code... For an example, see below.

I'm pretty sure this should be quite simple, but I'm not seeing it at the moment :thinking:

Regards,
Will

ui <- fluidPage(
	
	  fluidRow(
	    
	    column(3,  tagList(
	                  selectInput('group', 'Report group', choices = reportgroups),
	                  uiOutput('reportdd')
	               )
	    )	           
	  )
)

server <- function(input, output) {
  
  output$reportdd <- renderUI({
    selectInput('report', 'Report', grdropdown[group==input$group, report])
  })
  
  #I want to use the value selected in the 'report' selectInput to filter some data...
  qvars <- reactive({
    qgroups[group==input$group & report==input$report][['bvar']]
  })
  
}

Hi @WillP,

I am not quite sure what doesn't work, what error messages are you getting? Could you provide a reproducible example?

Maybe, you need some req() statements in your renderUI() and reactive() calls just to make sure that the input variables you need for these to function properly are "truthy" - you can read about req() here: https://shiny.rstudio.com/reference/shiny/latest/req.html

1 Like

Ah, yes, I think that might be it. I think what is happening is that the data.table does not have a value from input$report on which to filter until the 'report' control has been rendered. I will try req() and let you know how I get on! :slight_smile:

Will

That worked! Thanks!

Would you mind if I used this an example of making a reprex for the shiny book?

No, feel free to use it.

Will

After rewriting this into a reprex (below), I no longer think this makes a particularly good case study because it depends on a specific behaviour of data.table which won't be familiar to that many readers of the book.

library(shiny)
library(data.table)

mtcars <- as.data.table(mtcars)

ui <- fluidPage(
  fluidRow(
    column(3, tagList(
      selectInput("cyl", "Cylinders", choices = mtcars$cyl),
      uiOutput("vs"),
      textOutput("mpg")
    ))
  )
)

server <- function(input, output) {
  output$vs <- renderUI({
    selectInput("vs", "VS", mtcars[cyl == input$cyl, vs])
  })

  mpg <- reactive({
    mtcars[cyl == input$cyl & vs == input$vs, mpg]
  })
  
  output$mpg <- renderText({
    mean(mpg())
  })
}

shinyApp(ui, server)

The use of a render function is critical here, otherwise the problem is never triggered because nothing forces the reactive to evaluate.

(In this case I'd also recommend using updateSelectInput() instead of renderUI(); it doesn't solve the problem but it does make the code simpler)

I must admit, I have found data.table a little challenging to learn having been accustomed to data frames. It's very fast, however, which is why I wanted to use it in my app. I look forward to getting a copy of your Shiny book!

Will

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