将Shiny App与selectInput结合使用,并基于列中的每个唯一值进行计算以输出新表

时间:2018-10-31 20:56:19

标签: r shiny

我有一个大数据集,其中一个列中包含大约50个变量和大约40个独特的公司。

我需要使用数据做的工作基本上是找出每个公司的每一列中有多少个NA。

我的代码看起来像这样,现在的问题是,当我选择“ Table_1”时如何使用第二个selectInput显示low_check表,而当选择“ Table_2”时如何显示high_check表?

ui <- fluidPage(

headerPanel("Report"),

sidebarPanel(
           selectInput(inputId = "carriers", label = "Choose a company:", 
           choices = unique(T1Q3_std$CARRIER)),
           selectInput(inputId = "table", label = "Table #:", 
           choices = c("Table_1", "Table_2")
)


),
      mainPanel(

  tableOutput("table1"),
  tableOutput("table2")
            )
)


server <- function(input, output) {

low_check <- reactive({

    low_index_T1 <- c("T1.3", "T1.4", "T1.5", "T1.6", "T1.7 and T1.9", "T1.8", "T1.10",
       "T1.11", "T1.12")

    low_data_element_T1 <- c("element1", " element2",
                  "element3", "element4", "element5",
                  "element6", "element7", "element8", "element9")


    low_issue_T1 <- c("issue1", "issue2", "issue3", "issue4",
           "issue5", "issue6", "issue7", "issue8", "issue9")

    T1.3 <- T1Q3_std %>%
    filter(CARRIER == input$carriers) %>%
    summarise_at("element1", funs(sum(is.na(.))))

    T1.4 <- T1Q3_std %>%
    filter(CARRIER == input$carriers) %>%
    summarise_at("element2", funs(sum(is.na(.))))


    T1.5 <- T1Q3_std %>%
    filter(CARRIER == input$carriers) %>%
    summarise_at("element3", funs(sum(is.na(.))))

    T1.6 <- T1Q3_std %>%
    filter(CARRIER == input$carriers) %>%
    summarise_at("element4", funs(sum(is.na(.))))

    T1.7 <- T1Q3_std %>%
    filter(CARRIER == input$carriers) %>%
    summarise_at("element5", funs(sum(is.na(.))))

    T1.8 <- T1Q3_std %>%
    filter(CARRIER == input$carriers) %>%
    summarise_at("element6", funs(sum(is.na(.))))

    T1.10 <- T1Q3_std %>%
    filter(CARRIER == input$carriers) %>%
    summarise_at("element7", funs(sum(is.na(.))))
    count()

    T1.11 <- T1Q3_std %>%
    filter(CARRIER == input$carriers) %>%
    summarise_at("element8", funs(sum(is.na(.))))

    T1.12 <- T1Q3_std %>%
    filter(CARRIER == input$carriers) %>%
    summarise_at("element9", funs(sum(is.na(.))))

    low_num_of_violations_T1 <- as.integer(c(T1.3, T1.4, T1.5, T1.6,
                           T1.7, T1.8, T1.10, T1.11, T1.12))

    low_priority_check_T1 <- tibble(Index = low_index_T1, "Data Element" = low_data_element_T1,
                                    Issue = low_issue_T1, "# of Violations" = low_num_of_violations_T1)


})
high_check <- reactive({

    high_index_T1 <- c("T1.14", "T1.15", "T1.16", "T1.22")


    high_data_element_T1 <- c("e1", "e2", "e3", "e4")



    high_issue_T1 <- c("i1", "i2", "i3", "i4")

    T1.14 <- T1Q3_std %>%
                filter(CARRIER == input$carriers) %>%
                summarise_at("e1", funs(sum(is.na(.))))

    T1.15 <- T1Q3_std %>%
            filter(CARRIER == input$carriers) %>%
            summarise_at("e2", funs(sum(is.na(.))))

    T1.16 <- T1Q3_std %>%
            filter(CARRIER == input$carriers) %>%
            summarise_at("e3", funs(sum(is.na(.))))

    T1.22 <- T1Q3_std %>%
            filter(CARRIER == input$carriers) %>%
            summarise_at("e4", funs(sum(is.na(.))))

    high_num_of_violations_T1 <- as.integer(c(T1.14, T1.15, T1.16, T1.22))


    high_priority_check_T1 <- tibble(Index = high_index_T1, "Data Element" = high_data_element_T1,
                                  Issue = high_issue_T1, "# of Violations" = high_num_of_violations_T1)

})
output$table1 <- renderTable({
    low_check()
})
output$table2 <- renderTable({
    high_check()
})


}

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:0)

您可以使用conditionalPanel来做到这一点:

mainPanel(
      conditionalPanel(
        condition = "input.table == 'Table_1'", tableOutput("table1")
      ),
      conditionalPanel(
        condition = "input.table == 'Table_2'", tableOutput("table2")
      )
)

编辑:

显示多个输出:

mainPanel(
          conditionalPanel(
            condition = "input.table == 'Table_1'", 
            tableOutput("table1"), 
            tableOutput("table1new") # and so on....
          ),
          conditionalPanel(
            condition = "input.table == 'Table_2'", tableOutput("table2")
          )
    )