user1507889
user1507889

Reputation: 473

Dynamic Options For Dropdown Menu in Shiny R With HTML UI

I'm trying to convert a ui.r into an HTML ui in Shiny following http://shiny.rstudio.com/articles/html-ui.html I'm not sure how to translate the following code from my ui.r into HTML.

I have a dropdown that uses the following code

selectInput("data","Choose A Section:", choices=Sections[,2])

The inputs in the dropdown are generated based on a sections variable that is loaded on my server. The sections variable may change from time to time.

I know I could paste in all of the sections and create a dropdown used in the example ui http://rstudio.github.io/shiny/tutorial/#html-ui

<label>Sections:</label><br />
    <select name="dist">
      <option value="Section1">Section1</option>
      <option value="Section2">Section2</option>
      <option value="Section3">Section3</option>
      <option value="Section4">Section4</option>
    </select> 

But I'm not sure how to set it up for options that may change if the data changes. Is there an easy way to do this?

Upvotes: 1

Views: 2085

Answers (1)

maloneypatr
maloneypatr

Reputation: 3622

While this isn't the best answer, it may get you started. In Shiny, I upload .csv files, which will dynamically update the dropdown menu with the header names of the uploaded files.

In server.R, I include

observe({

infile <- input$datfile

print(infile)
if(is.null(infile))
  return(NULL)

d <- read.csv(infile$datapath, header = T)

updateSelectInput(session, 'dropdown_1', choices = names(d))
updateSelectInput(session, 'dropdown_2', choices = names(d))

})

In ui.R, I include

selectInput('dropdown_1', '', ''),
selectInput('dropdown_2', '', '')

As long as you can point to a data source, I'm thinking the logic should hold. For instance, this would grab the unique items in the field named column1 of data_set.

observe({

 data_set <- xxxxx
 updateSelectInput(session, 'dropdown_menu', choices = unique(data_set$column1))

})

Upvotes: 3

Related Questions