DS4PS / cpp-526-spr-2020

Course shell for CPP 526 Foundations of Data Science I for Spring 2020.
http://ds4ps.org/cpp-526-spr-2020/
3 stars 0 forks source link

Final Project - Select Input not returning correct values #31

Open ecking opened 4 years ago

ecking commented 4 years ago

Hi there,

I'm perplexed. I'm trying to set up a widget where the user selects the street name. Instead of returning the street name, its only returning values 1-300+. I've done nothing to modify the code except for adding the following.


selectInput(
  inputId = "Street", label = h4("Street Name"), 
    choices = c(sort(dat$StreetName)), 
    selected = c("Rural Rd"))
ecking commented 4 years ago

Stack overflow for the win. Nevermind!

lecy commented 4 years ago

Do you mind sharing the solution? Was it stored as a factor?

jamisoncrawford commented 4 years ago

I'm not looking at the code but...

choices = c(sort(dat$StreetName))

...would likely result in thousands of selections and at least hundreds of duplicates. I imagine using unique() would be a good start!

choices = c(sort(unique(dat$StreetName)))
ecking commented 4 years ago

Hi there! @jamisoncrawford I tried adding unique and I still received the number values.

So here is what I learned from an example on stack overflow:

dat$StreetName must be a factor rather than a character so when c() is used on a factor it strips "all information used and converts it to a simple numeric column." So basically -- dont use c(). So to convert the factor to a character value use as.character.

Here is what is left:

selectInput( inputId = "Street", label = h4("Street Name"), choices = as.character(sort(unique(dat$StreetName))), selected = c("Rural Rd"))

Not sure if that makes 100% sense. But adding as.character works wonderfully!

jamisoncrawford commented 4 years ago

Ah, that makes sense! I didn't even notice function c() when I rewrote the code, haha.

Great knowledge add - thanks for explaining this for us and others @ecking!