Reputation: 2189
Im using the following dataframe to build a shiny application:
listIDs <- c(100,100,100,100,200,200,200,200),
values <- c(2.12, 2.43, 2.12, 4.45, 3.23, 4.23, 3.23, 4.23),
horses <- c(2.1, 3.2, 4.1, 4.2, 5.4, 4.7, 2.8, 2.0),
month <- c("JAN", "FEB", "JAN", "FEB","MAY","APRIL","MAY", "APRIL"),
df <- data.frame(listIDs, values, horses, month),
Im building a shiny application with the following tabs:
shinyUI(fluidPage(
#outlinen title
titlePanel(title = "This is the title"),
sidebarLayout(
sidebarPanel(
selectInput("var1", "Select the eventID", choices = listIDs),
selectInput("var2", "Select the eventID", choices = month),
br()
),
mainPanel(("Personal information"),
plotOutput("myhist"))
)
))
library(shiny)
library(ggplot2)
shinyServer(function(input, output){
output$myhist <- renderPlot({
df_graph <- df[df$listIDs == input$var1,]
df_graph <- df_graph[df_graph$month == input$var2,]
ggplot(data=df_graph, aes(x=month, y=values, group = horses)) +
geom_line() + geom_point() + theme(axis.text.x = element_text(angle = 90, hjust = 1))
})
})
It all works but the thing is that when I select 100 in my first selection box I stil get the options "JAN", "FEB", "MRT", "APRIL" (while I only should get JAN and FEB). Any thoughts on how I can make this dynamic?
Upvotes: 2
Views: 1027
Reputation: 18612
Your selectInput
element corresponding to the month selection should be rendered dynamically based on the value of input$var1
. Here's a simple example:
shinyApp(
ui = fluidPage(
titlePanel(title = "This is the title"),
sidebarLayout(
sidebarPanel(
selectInput("var1", "Select the eventID",
choices = listIDs
),
uiOutput("select_month_ui"),
br()
),
mainPanel(
"Personal information",
plotOutput("myhist")
)
)
),
server = function(input, output) {
output$select_month_ui <- renderUI({
selectInput("var2", "Select the eventID",
choices = df[df$listIDs %in% input$var1,"month"]
)
})
output$myhist <- renderPlot({
df_graph <- df[df$listIDs == input$var1,]
df_graph <- df_graph[df_graph$month == input$var2,]
ggplot(data = df_graph,
aes(x = month, y = values, group = horses)) +
geom_line() + geom_point() +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
})
}
)
The selectInput
object is moved from your UI code into the server code as
output$select_month_ui <- renderUI({
selectInput("var2", "Select the eventID",
choices = df[df$listIDs %in% input$var1,"month"]
)
})
and replaced with uiOutput("select_month_ui")
.
Upvotes: 2