Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R-Shiny using Reactive renderUI value

Tags:

r

rstudio

shiny

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

like image 426
James Willcox Avatar asked Nov 21 '14 03:11

James Willcox


People also ask

How do you use reactive in Shiny R?

You can create reactive output with a two step process. Add an R object to your user interface. Tell Shiny how to build the object in the server function. The object will be reactive if the code that builds it calls a widget value.

What is Renderui in Shiny?

An expression that returns a Shiny tag object, HTML() , or a list of such objects.

What is a reactive value Shiny?

Description. The reactiveVal function is used to construct a "reactive value" object. This is an object used for reading and writing a value, like a variable, but with special capabilities for reactive programming.

How do I make text bold in R Shiny?

Shiny has many functions that can transform plain text into formatted text. Simply place text inside the h1() function to create a primary header (e.g. a title), h2() for a secondary header, strong() to make text bold, em() to make text italicized, or any of the other formatting functions.


1 Answers

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])
      })
    }
    )
  )
like image 180
jdharrison Avatar answered Sep 23 '22 18:09

jdharrison