【问题标题】:Making if statements in my shiny app server part在我闪亮的应用服务器部分制作 if 语句
【发布时间】:2022-01-13 16:07:16
【问题描述】:

希望您能帮助我在 R 中使用 Shiny 应用程序。我是 Shiny 的新手,需要一些帮助来简化我的后端部分。

我正在尝试基于两个输入呈现表格,一个是医院输入,第二个输入是请求的表格类型。为了得到我想要的表,我做了几个 if 语句,但代码似乎很复杂,可以简化。

基本上,我需要根据医院和表格类型为 Shiny 应用程序显示表格,而不需要太长的 if 语句。

有没有人可以帮助我编写下面的代码以使其更简单。

library(shiny)
library(tidyverse)



UCSF <- list( ndc_ucsf_v2, brand_n_v2, ndc_tier2_ucsf_v2, ndc_tier5_ucsf_v2, ucsf_ldd_list_v2,
              brand_n_class_ucsf_v2, ndc_rest_of_tiers_ucsf_v2)

hospital_list <- c("UCSF", "VIDANTHEALTH", "CONH", "ERLR", "OSURX", "SUTTHEALTH",
                   "UHC", "WHT")
table_list <- c("By NDC", "By brand name", "By GPI class", "By tier 2", "By tier 5", "Other tiers",
                "LDD", "By hierarchy")
ui <- fluidPage(
    titlePanel("Hospital rebate claims"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            selectInput(inputId = "hospital", 
                        label = "Hostpital/clinic", 
                        choices = c("Choose hospital", hospital_list),
                        multiple = FALSE),
            selectInput(inputId = "table_type", 
                        label = "Type of analysis",
                        multiple = FALSE, choices = table_list)),
                mainPanel(
           tableOutput(outputId = "claimtable"),
           plotOutput(outputId = "hist")
)))

server <- function(input, output) {
      output$claimtable <- renderTable({
      if(input$hospital == "UCSF" & input$table_type == "By NDC"){
        output$claimtable <- return(UCSF[[1]])
        } 
        if(input$hospital == "UCSF" & input$table_type == "By brand name") {
        output$claimtable <- return(UCSF[[2]])
        }
        if(input$hospital == "UCSF" & input$table_type == "By tier 2") {
          output$claimtable <- return(UCSF[[3]])
        }
        if(input$hospital == "UCSF" & input$table_type == "By tier 5") {
          output$claimtable <- return(UCSF[[4]])
        }
        if(input$hospital == "UCSF" & input$table_type == "LDD") {
          output$claimtable <- return(UCSF[[5]])
        }
        if(input$hospital == "UCSF" & input$table_type == "By GPI class") {
          output$claimtable <- return(UCSF[[6]])
        }
        if(input$hospital == "UCSF" & input$table_type == "Other tiers") {
          output$claimtable <- return(UCSF[[7]])
        }})
}

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r function shiny


    【解决方案1】:

    您可以使用switch 而不是重复if 命令:

    server <- function(input, output) {
      output$claimtable <- renderTable({
        
        if (input$hospital == "UCSF") {
          switch (input$table_type,
                  "By NDC" = UCSF[[1]],
                  "By brand name" = UCSF[[2]],
                  "By tier 2" = UCSF[[3]],
                  "By tier 5" = UCSF[[4]],
                  "LDD" = UCSF[[5]],
                  "By GPI class" = UCSF[[6]],
                  "Other tiers" = UCSF[[7]],
          )
        } else {
          # What happens here? 
          # You example doesn't show it
        }
        
      }
    

    或者,您也可以正确订购 table_list 并根据它进行替换:

    table_list <- c("By NDC", "By brand name", "By tier 2", "By tier 5",
                      "LDD", "By GPI class", "Other tiers", "By hierarchy")
        
    server <- function(input, output) {
        output$claimtable <- renderTable({
          
          if (input$hospital == "UCSF") {
            UCSF[[which(table_list == input$table_type)]]
          } else {
            # What happens here? 
            # You example doesn't show it
          }
          
        }
    

    input$hospital 有什么变化?也许它改变了列表的第二级?

    server <- function(input, output) {
      output$claimtable <- renderTable({
        UCSF[which(table_list == input$table_type)][which(hospital_list == input$hospital_list)]
        
      }
    

    【讨论】:

    • 您好,感谢您的分析器,如果我有两个选项,它就可以工作,不幸的是,它只有在我有两个选项时才有效,我需要应用程序更改首先取决于“selectInput = 医院”中的医院和那么第二个是分析的类型。
    • @amer23 你试过最后一个选项吗?它会根据这两个输入而变化。
    • 我打错了,第一个解决方案有效,我需要用 else 完成每个 if 语句,然后重新开始 if。比你
    猜你喜欢
    • 1970-01-01
    • 2021-09-21
    • 2016-12-28
    • 1970-01-01
    • 2014-08-09
    • 2016-05-23
    • 2016-04-21
    • 2014-07-28
    • 1970-01-01
    相关资源
    最近更新 更多