【问题标题】:RShiny: Display a single table with multiple inputs using select boxR Shiny:使用选择框显示具有多个输入的单个表
【发布时间】:2016-02-05 16:39:55
【问题描述】:

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

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

enter image description here

但是当我编写两个output$table 代码时,闪亮的应用程序仅对月输出或季度输出有反应。

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

所以我尝试的是写 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)

还有一个额外的问题是可以在选择框中分配级别吗? 例如,高级选择框有月份,或季度,当我选择月份时,我可以在低级选择框中选择一月到十二月。也就是说,当我在高级选择框中选择季度时,我可以在低级选择框中选择Q1~Q4。

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

【问题讨论】:

    标签: r shiny-server shiny


    【解决方案1】:

    我有一个关于如何改变你的 UI 设计的建议。与其拥有两个 selectInput,不如让一个 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)  
    

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

    【讨论】:

    • 这是我过去几天试图弄清楚的!非常非常感谢!!
    猜你喜欢
    • 1970-01-01
    • 2017-05-03
    • 2021-06-06
    • 2014-12-06
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    相关资源
    最近更新 更多