2

I have a shiny app in which there is a dropdown with values that I have been entering manually. However, since there are a lot of values that I need to populate in my drop down, it would be better if I could let the users type the words in a textbox and the app display only the matching elements(something like a browser). Currently, I am using the code:

 lapply(1:num, function(i) {
  selectInput(paste0("n_input_", i), label = paste0("n_input", i),  
              choices = list("IN120" = 1, "CR23" = 2, "FG45" = 3,"OR45"=4), 
              selected = 1)
})

Can we do this in shiny? In case not, how do we read values from a csv file to populate our drop down?

Abhi
  • 61
  • 2
  • 9
  • Some ideas in this link https://groups.google.com/forum/#!topic/shiny-discuss/09UoBQEoCv8 including using `type ahead text input` from `shinysky`, and directly using a javascript library called `select2` (sample code in link) – Xiongbing Jin May 11 '16 at 03:47
  • I'll suggest the same. Take a look at https://stackoverflow.com/questions/35265920/auto-complete-and-selection-of-multiple-values-in-text-box-shiny and https://github.com/AnalytixWare/ShinySky . Still if you need any help on last section you mentioned [reading a csv and populating choices, That will be done using `updateSelectInput` functions] , let me know. – Indranil Gayen May 11 '16 at 05:45
  • you can use selectizeinput in the ui and populate all your options in the server side using updateselectizeInput – vikash Aug 21 '17 at 18:40

2 Answers2

1

To answer your second question try:

myData <- read.csv("my_csv_file_path.csv", row.names=NULL, na.strings="", stringsAsFactors=FALSE)

myList <- as.list(unique(myData[[1]])) ## The number should be the column/field in myData that has the desired list.

lapply(1:num, function(i) {
  selectInput(paste0("n_input_", i), label = paste0("n_input", i),  
              choices = myList)
})

This should read in the .csv file and create a list of the unique values in the desired list. Edited your script to use choices = myList and removed the selected = 1 portion. The drop down will default to the first item in your list when this is omitted. A technique is to add an initial value to the top of the list (e.g., "Select Something") to display until the user selects something from the dropdown.

Cheers!

Jeffrey
  • 11
  • 2
-1

To answer your first question, Shiny selectInput() now uses the selectize.js library by default for the input element. This allows searching of the choices by default in Shiny.

phassett
  • 1
  • 1