Reputation: 13
I have two sets of inputs: month(Jan ~ Dec), quarter(Q1 ~ Q4) and they are saved as dataframe in R environment. (12 months + 4 quarters, total 16 dataframes)
and I have created two select boxes that we can choose display months or quarters as picture below.
but when I write two output$table
code, shiny app is only reactive with either month output or quarter output.
I have successfully created two tables in single main panel. However, rather than display two tables(looks ugly), I would like to display a single table in the main panel that reacting with both select box. (Two input and single output).
So what I have tried was write if statement, but it does not work.
Here are the server.R and ui.R I have been testing with
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("dataset", "Choose Month:",
choices = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct","Nov","Dec")),
selectInput("dataset1", "Choose Quatar:",
choices = c("Q1", "Q2", "Q3", "Q4"))
),
mainPanel(
DT::dataTableOutput("table")
)
)
))
server <- shinyServer(function(input, output) {
monthInput <- reactive({
data = switch(input$dataset,
"Jan" = Jan, "Feb" = Feb, "Mar" = Mar,
"Apr" = Apr, "May" = May, "Jun" = Jun,
"Jul" = Jul, "Aug" = Aug, "Sep" = Sep,
"Oct" = Oct, "Nov" = Nov, "Dec" = Dec
)
})
quatarInput <- reactive({
data2 = switch(input$dataset,
"Q1" = Q1, "Q2" = Q2, "Q3" = Q3, "Q4" = Q4
)
})
#Test: Add if statement here -> If bla bla it is Month, else it is Quatar(Because both switch must work for same location where table is displayed)
output$table <- DT::renderDataTable({
if (input$monthInput){
DT::datatable(monthInput())
} else {
if(input$quatarInput){
DT::datatable(quatarInput())}
}
})
})
shinyApp(ui=ui,server=server)
And one extra question is that is it possible to assign levels in select boxes? For example, a high level select box has Month, or quarter, and when I choose Month, I can choose January to December in low level select box. In other words, when I choose quarter in high level select box, I can choose Q1 ~Q4 in the low level select box.
Could someone help me to figure this out?
Upvotes: 1
Views: 1524
Reputation: 927
I have a suggestion for how to change your UI design. Rather than having two selectInputs, have one selectInput that switches between the two styles (month and quarter) according to another input.
# I don't have your data, so here I'm just going to create some. Delete these lines and replace them with your actual data
Jan = overflow::sorandf()
Feb = overflow::sorandf()
Mar = overflow::sorandf()
Apr = overflow::sorandf()
May = overflow::sorandf()
Jun = overflow::sorandf()
Jul = overflow::sorandf()
Aug = overflow::sorandf()
Sep = overflow::sorandf()
Oct = overflow::sorandf()
Nov = overflow::sorandf()
Dec = overflow::sorandf()
Q1 = overflow::sorandf()
Q2 = overflow::sorandf()
Q3 = overflow::sorandf()
Q4 = overflow::sorandf()
library(shiny)
library(DT)
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
radioButtons("viewdataradio","View data by:", choices = c("month", "quarter"), inline = TRUE, selected = "month"),
selectInput("dataset", "Choose Month:",
choices = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct","Nov","Dec"))
),
mainPanel(
DT::dataTableOutput("table")
)
)
))
server <- shinyServer(function(input, output,session) {
observe({
if(input$viewdataradio == "month"){
choices = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep","Oct","Nov","Dec")
firstchoice = "Jan"
label = "Choose Month:"
}else{
choices = c("Q1","Q2","Q3","Q4")
firstchoice = "Q1"
label = "Choose Quarter:"
}
updateSelectInput(session, "dataset", label = label, choices = choices, selected = firstchoice)
})
data <- reactive({
data = switch(input$dataset,
"Jan" = Jan, "Feb" = Feb, "Mar" = Mar,
"Apr" = Apr, "May" = May, "Jun" = Jun,
"Jul" = Jul, "Aug" = Aug, "Sep" = Sep,
"Oct" = Oct, "Nov" = Nov, "Dec" = Dec,
"Q1" = Q1, "Q2" = Q2, "Q3" = Q3, "Q4" = Q4
)
})
output$table <- DT::renderDataTable({
datatable(data())
})
})
shinyApp(ui=ui,server=server)
This looks neater, and I think answers your question too.
Upvotes: 2