R-Shiny using Reactive renderUI value

r, rstudio, shiny

Solution

You would refer to the elements by their id for example `input$compName`. As a contrived example here is a simple shiny app with two selectInput's. The second `selectInput` choices depend on the value of the first. Referencing the output of widgets created by `renderUI` is no different from referencing the same widgets if they had been in UI.R from the beginning:

library(shiny)
myDF <- data.frame(A = 1:4, B = 3:6, C = 6:9, D = 10:13)
runApp(
  list(
    ui = fluidPage(
      uiOutput("myList"),
      uiOutput("myNumbers")
      )
    , server = function(input, output, session){
      output$myList <- renderUI({
        selectInput("compName", "Company Name:", LETTERS[1:4])
      })

      output$myNumbers <- renderUI({
        selectInput("compNo", "Product Line:", myDF[, input$compName])
      })
    }
    )
  )

Problem

How do you use values obtained from a renderUI element in a reactive wrapper? i.e. My code: ``` CompanyNames <- sqlQuery(connection, "SELECT Companynm FROM RiskMgm_Company") output$CompNameSelector <- renderUI({ selectInput("compName","Company Name:",as.vector(CompanyNames[,1])) }) CompID <- reactive({ CompID <<- sqlQuery(paste("SELECT CompanyID FROM RiskMgm_Company WHERE Companynm = '",compName,"'")) }) output$MotorSelector <- renderUI({ selectInput("MachSer","Machine:",sqlQuery(connection,paste("SELECT Motor_func FROM RiskMgm_Motor WHERE Company_ID='",CompID,"'"))) }) ``` My error: ``` Successfilly opened connection to db Error in paste("SELECT CompanyID FROM RiskMgm_Company WHERE Companynm = '", : could not find function "compName" ``` What am I doing wrong? Essentially what I want is a list of companies given by the SQL query. Then depending on the Company selected it will show the motors that belong to that company in the next dropdown box Thanks

Original source