I don't know if the names in the list are important or not, but this should give you what you want:
setNames(as.list(categories), categories)
Frage
I'm trying to convert factor levels output into a list so I can create a dynamic input for a SelectInput function in R Shiny.
Basically, I import a CSV file, and would like to create the drop-down list in SelectInput by using the "categories" (levels) in the first column of the CSV file.
So, in UI.R I have:
uiOutput("categoryPicker")
And in server.R I have:
datatwo <- read.csv(file="data2.csv",head=TRUE,sep=",")
then, in the "shinyServer(function(input, output)" section ("category" is a header in the CSV file, by the way):
output$categoryPicker <- reactiveUI(function() {
categories <- levels(datatwo$category)
selectInput("category", "Category:", categories)
})
This doesn't work, gives me the following error:
Error in choices[[choiceName]] : subscript out of bounds
The str(categories) output is as follows:
chr [1:22] "" "Cat1" "Cat2" "Cat3" "Cat4" "Cat5" ...
If I do "list(levels(datatwo$category)", it just gives me one drop down, which is the entire str(categories) output:
List of 1 $ : chr [1:22] "" "Cat1" "Cat2" "Cat3" ...
I'm trying to get this in the form of (such that when str(x) is done the output would be):
List of X
$ Cat1: chr "Cat1"
$ Cat2: chr "Cat2"
$ Cat3: chr "Cat3"
...
But I'm not sure how to do this. I'm also not sure why the first item is a "" [empty string]. I'm pretty new to R and Shiny so forgive my ignorance, but any help would be greatly appreciated.
BTW, I've been working off this tutorial: http://rstudio.github.com/shiny/tutorial/#dynamic-ui
Thanks.
Lösung
I don't know if the names in the list are important or not, but this should give you what you want:
setNames(as.list(categories), categories)