Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: making a list of unique values usable in Shiny selectInput

I'm working on a small Shiny App that will provide access to some publicly available labour market data. I've a relatively simple data set in a long format that resembles the extract below:

data

Geography Measure    Time  Value
ABC       Measure A  2010  3
ABC       Measure A  2011  4
ABC       Measure A  2013  5
ABC       Measure B  2010  0.2
ABC       Measure B  2011  4
DEF       Measure A  2010  4
DEF       Measure A  2011  8
DEF       Measure A  2012  7
DEF       Measure B  2010  8
DEF       Measure B  2010  7

I'm intending to generate a simple chart and I would like to use the selectInput method to provide end-users with a convenient way of filtering the tables and the corresponding data. Consequently, I would like for the selectInput to contain a list of unique values derived from columns against which I'm intending to apply filters. On the example of the Measure column I'm getting a list of unique values using the code below:

## Create list of unique measures
lst.measures <- as.list(unique(dta$Measure))

but when I attempt to introduce the obtained list to the Shiny ui.R using the code below:

  # Selection of the available indicators
  selectInput("selectVariable", label = h3("Select box"), 
              choices = lst.measures, 
              selected = 1)
),

The generated list does not contain the desired text (Measure A, Measure B) but a list of numbers, like in the picture below (I've more measure in my data than in the sample data): Sample List

I'm guessing that the problem is concerned with how the list is structured, presently

> as.list(unique(dta.nom$Measure))
[[1]]
[1] Measure A
Levels: Measure A Measure B

[[2]]
[1] Measure B
Levels: Measure A Measure B

Consequently, my question is: How can I reorder the elements within the list, so the generated list can be passed to the selectInput in a meaningful manner, showing the text values corresponding to the unique values of the chosen column?

like image 564
Konrad Avatar asked Mar 24 '15 11:03

Konrad


1 Answers

Try converting dta.nom$Measureto character

dta.nom$Measure = as.character(dta.nom$Measure)

Further from the top of my head I am not sure that you actually are required to pass a list to selectInput.

# Create list of unique measures
#lst.measures <- as.list(unique(dta$Measure))
lst.measures <- unique(dta$Measure)
like image 100
Pewi Avatar answered Oct 05 '22 00:10

Pewi