RShiny:使用选择框显示包含多个输入的单个表

时间:2016-02-05 16:39:55

标签: r shiny-server shiny

我有两组输入:月(1月〜12月),季度(Q1~Q4),它们在R环境中保存为数据帧。 (12个月+ 4个季度,共16个数据帧)

我创建了两个选择框,我们可以选择显示月份或季度,如下图所示。

enter image description here

但是当我编写两个output$table代码时,闪亮的app只会对月输出或季度输出产生反应。

我已在单个主面板中成功创建了两个表。 但是,我想在主面板中显示一个与两个选择框都有反应的表,而不是显示两个表(看起来很难看)。 (两个输入和单个输出)。

所以我尝试过写if语句,但它不起作用。

这是server.R和ui.R我一直在测试

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)

另外一个问题是,是否可以在选择框中分配级别? 例如,高级选择框具有月份或季度,当我选择月份时,我可以在低级别选择框中选择1月到12月。换句话说,当我在高级选择框中选择四分之一时,我可以在低级选择框中选择Q1~Q4。

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

我建议如何更改您的UI设计。而不是有两个selectInputs,有一个selectInput根据另一个输入在两个样式(月和季度)之间切换。

# 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)  

这看起来更整洁,我想也回答你的问题。