r - RShiny: Display a single table with multiple inputs using select box -
i have 2 sets of inputs: month(jan ~ dec), quarter(q1 ~ q4) , saved dataframe in r environment. (12 months + 4 quarters, total 16 dataframes)
and have created 2 select boxes can choose display months or quarters picture below.
but when write 2 output$table
code, shiny app reactive either month output or quarter output.
i have created 2 tables in single main panel. however, rather display 2 tables(looks ugly), display single table in main panel reacting both select box. (two input , single output).
so have tried write if statement, not work.
here server.r , ui.r 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 month, else quatar(because both switch must work same location table displayed) output$table <- dt::renderdatatable({ if (input$monthinput){ dt::datatable(monthinput()) } else { if(input$quatarinput){ dt::datatable(quatarinput())} } }) }) shinyapp(ui=ui,server=server)
and 1 question is possible assign levels in select boxes? example, high level select box has month, or quarter, , when choose month, can choose january december in low level select box. in other words, when choose quarter in high level select box, can choose q1 ~q4 in low level select box.
could me figure out?
i have suggestion how change ui design. rather having 2 selectinputs, have 1 selectinput switches between 2 styles (month , quarter) according input.
# don't have data, here i'm going create some. delete these lines , replace them 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, , think answers question too.
Comments
Post a Comment