Reputation: 149
I am trying to build an RShiny app that displays 2 dropdown menus in the UI, where the first dropdown menu selects the species, and the second dropdown menu selects a data that is available for that species.
I looked at this post for a possible way to do this, but this seems best for data where the options to include in the dropdown menu are rows of a data frame, not columns. I also realize this example created their own data table from scratch, but I want to use my data frame because I want to keep the data associated with it.
The data I am currently working with looks more like this format:
Haddock_age1length <- c("33", "34", "32")
Haddock_weight <- c("14", "16", "15")
squid_width <- c("30", "32", "34")
squid_weight <- c("10", "11", "15")
Year <- c("2000", "2001", "2002")
df <- data.frame(Haddock_age1length, Haddock_weight,squid_width, squid_weight,Year)
Haddock_age1length Haddock_weight squid_width squid_weight Year
33 14 30 10 2000
34 16 32 11 2001
32 15 34 15 2002
my question is, how can I use the selectInput()
function to create one dropdown menu to select species such as "Haddock" or "Squid", and then another one that lists the corresponding data available for each species?
e.g: the options under "Haddock" would be "Age 1 Length" and "Weight", where the options under "Squid" would be "Width" and "Weight", so that if someone selected "Haddock" and "Weight", the "Haddock_weight" column of the data frame would be called.
If possible, I would like to not reformat the dataframe as after selection, as I hope to render a plotly timeseries lineplot for the data selected.
Thanks!
Upvotes: 0
Views: 392
Reputation: 1280
Maybe something like this is what you are looking for? Two different selectInput
, first you select the species, then the second is any of the columns that include the species name:
library(shiny)
library(dplyr)
Haddock_age1length <- c("33", "34", "32")
Haddock_weight <- c("14", "16", "15")
squid_width <- c("30", "32", "34")
squid_weight <- c("10", "11", "15")
Year <- c("2000", "2001", "2002")
df <- data.frame(Haddock_age1length, Haddock_weight,squid_width, squid_weight,Year)
ui <- fluidPage(
selectInput("Species", "Species", choices = c("Haddock", "Squid")),
uiOutput("ColumnUI"),
tableOutput("TABLE")
)
server <- function(input, output, session) {
output$ColumnUI<-renderUI({
selectInput("COLUMN", "Column?", choices = names(df%>%select(grep(input$Species, names(.), ignore.case = T))))
})
output$TABLE<-renderTable({
req(input$COLUMN)
df[input$COLUMN]
})
}
shinyApp(ui, server)
Upvotes: 1