Reputation: 435
I have a JSON file with information on birds. It looks a bit like this:
{
"Cardinal":{
"Wingspans":[22.9,21.2,23.4],
"Sightings":["Stream","Scramble","Stream"],
"Gender":["M","F","M"],
...
},
"Bluejay":{...},
...
}
In my app, I use a selectInput to allow users to pick between different types of bird (Cardinal, Bluejay, Junco, etc), and update the data. I've successfully hardcoded the keying for any given single case, for example:
savedData <<- fromJSON(file = "birds.json")
wingspans <<- data.frame(savedData$Cardinal$Wingspans)
sightings <<- data.frame(savedData$Cardinal$Sightings)
And so on. What I would like is for the "Cardinal" part to instead use the selectInput Id "typeSelect" so that when the app goes to load the data, it uses whichever bird the user has specified from the drop down menu. Here are some bad ideas which did not solve the problem:
wingspans <<- data.frame(paste0("savedData$",input$typeSelect,"$Wingspans"))
wingspans <<- data.frame(savedData$(input$typeSelect)$Wingspans)
wingspans <<- data.frame(savedData$input$typeSelect$Wingspans)
These all either produced syntax errors, or just gave me a 1x1 data frame with the pasted path name as the cell value. Thanks everyone!
Upvotes: 1
Views: 35
Reputation: 886948
It is an object value, so use [[
instead of $
as $
will literally evaluate
wingspans <- data.frame(savedData[[input$typeSelect]]$Wingspans)
Or can have all the extractions with [[
wingspans <- data.frame(savedData[[input$typeSelect]][["Wingspans"]])
Or if we have purrr
loaded, pluck
may be useful as well
library(purrr)
wingspans <- data.frame(pluck(savedData, input$typeSelect, "Wingspans"))
Upvotes: 1