34

My ui.R file has a selectInput like this:

selectInput("variable1", "Choose Option:", camps)

where camps is supposed to be a vector of options. This vector depends on a sql query that runs on the server script and returns the IDs of the camps:

server.R

df1 <- getCamps("date")
camps <- unique(df1$idCamps)

When I run the App the ui.R does not know what "camps" is because it is only created in the server.R file. How can I pass the vector of camps created in the server.R file to the ui.R file so that they are now the options to choose from in the selectInput selector?

Jesse Anderson
  • 4,507
  • 26
  • 36
Cybernetic
  • 12,628
  • 16
  • 93
  • 132

2 Answers2

36

You need to create an input object in server.R, and return it to ui.R as part of the output list:

In server.R:

df1 <- getCamps("date")
camps <- unique(df1$idCamps)
output$campSelector <- renderUI({
   selectInput("variable1", "Choose Option:", as.list(camps)) 
})

In ui.R:

uiOutput("campSelector")
MDe
  • 2,478
  • 3
  • 22
  • 27
  • I get ``` Unexpected character output for userTypes``` where userTypes is is a character vector nested within as.list() per the answer – Doug Fir Jan 30 '18 at 05:24
0

Easier way: Worked for me with barPlot() function. names(dataframe_name[colm]) ,

where in my case colm was colm <- as.numeric(input$parameters)

and I was getting parameters from ui.r, where parameters was

selectInput("parameters", label = "1. Select the variable from the U.F.O. dataset", choices = c("Shape" = 7, "Country" = 4, "AM/PM" = 3, "State" = 6))