首页 > 解决方案 > 为什么在使用多个选项卡时代码只能部分工作?

问题描述

我之前在这里发布了一个关于应用创意的问题。我决定在同一个应用程序中添加类似的东西,这样每个标签都有类似的功能,但会处理不同的药物说明集。我尝试按照此处发布的查询和解决方案进行操作,但不知何故丢失了mainPanel. 这是包含两个选项卡的代码

library(tidyverse)
library(shiny)
library(shinythemes)
library(xtable)


insulin <- readRDS("insulin.rda")

# User Interface

ui <- fluidPage(
  titlePanel("Pre-operative Advice on Long-term Medications for Patients Undergoing Elective Surgery - version 0.1"),



  tabsetPanel(
    tabPanel("Long-term Medications", fluid = TRUE,
            sidebarLayout(
              sidebarPanel(
                selectInput("specialty", "Choose which surgical subspecialty",
                            c("Major Orthopaedic Arthroplasty or Revision" = "ortho",
                              "All other specialties" = "other"),
                            selected = "All other specialties"),

              ),
              mainPanel(
                br(),

                uiOutput("drugs_sel"),

                h3(textOutput(outputId = "px_name")),

                br(),

                h4(textOutput(outputId = "dob")),

                br(),

                tableOutput("drugs_table")

              )
            )

    ),

    tabPanel("Insulin", fluid = TRUE,
      sidebarLayout(
        sidebarPanel(


          selectInput("DM", "What type of diabetes does patient have?",
                      c("Type One" = "Type 1",
                        "Type Two on Insulin" = "Type 2"),
                      selected = "Type One"),

          selectInput("time", "Is patient on morning or afternoon list?",
                      c("Morning List" = "AM",
                        "Afternoon List" = "PM"),
                      selected = "Morning"),
          checkboxGroupInput("class", "Which type(s) of insulin is patient on?",
                             c("Long and Intermediate acting",
                               "Pre-Mixed",
                               "Rapid or Short acting"))
        ),
        mainPanel(
          br(),

          helpText("Choose from the type(s) of insulin from left panel before typing",
                   "Please ensure the type(s) of insulin are correct before proceeding"),

          uiOutput("insulin_sel"),

          h3(textOutput(outputId = "px_name")),

          br(),

          h4(textOutput(outputId = "dob")),

          br(),

          tableOutput("insulin_table"),

          radioButtons('format', 'Document format', c('PDF'),
                       inline = TRUE),
          downloadButton('downloadReport')
        )

      )

    )


  )
)



server <- function(input, output){
  my_insulin_table <- reactive({
    insulin_subset <- insulin %>% filter(DM == input$DM,
                                         Time == input$time,
                                         Class %in% input$class)
    tab <- insulin_subset %>% filter(Name %in% input$name) %>% select(Class, Name, Plan)
    return(tab)
  })

  my_drugs_table <- reactive({
    drugs_subset <- drugsUI %>% filter(Specialty == input$specialty)

    drug_tab <- drugs_subset %>% select(Name, Recommnedations)
    return(drug_tab)
  })

  output$px_name <- renderText({input$px_name})

  output$dob <- renderText({input$dob})

  output$drugs_sel <- renderUI({
    drugs_subset <- drugsUI %>% filter(Specialty == input$specialty)

    selectizeInput("drug", "Type in name of drug",
                   choices = lsit("Type in name of drug" = "",
                                  "Names" = drugs_subset$Name),
                   selected = NULL,
                   multiple = TRUE,
                   options = NULL)

  })

  output$drug_table <- renderTable({
    xtable(my_drugs_table())
  })

  output$insulin_sel <- renderUI({

    insulin_subset <- insulin %>% filter(DM == input$DM, 
                                         Time == input$time, 
                                         Class %in% input$class)

    selectizeInput("name", "Type in name of insulin",
                   choices = list("Begin typing name of insulin" = "", 
                                  "Names" = insulin_subset$Name), 
                                  selected = NULL, 
                                  multiple = TRUE,
                                  options = NULL)
  })

  output$insulin_table <- renderTable({
    xtable(my_insulin_table())
  })

  output$downloadReport <- downloadHandler(
    filename = function() {("insulin-instructions.pdf")
      # paste('my-report', sep = '.', switch(
      #   input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
      # ))
    },

    content = function(file) {
      src <- normalizePath('report_insulin.Rmd')

      # temporarily switch to the temp dir, in case you do not have write
      # permission to the current working directory
      owd <- setwd(tempdir())
      on.exit(setwd(owd))
      file.copy(src, 'report_insulin.Rmd', overwrite = TRUE)

      library(rmarkdown)
      out <- render('report_insulin.Rmd', 
                    params = list(name = input$px_name, dob = input$dob),
                    'pdf_document')
      file.rename(out, file)
    }
  )

}

shinyApp(ui = ui, server = server)

如果我删除了tabPanelnamed "Long-term Medications",另一个 tabPanel 似乎又可以工作了。任何人都能够阐明为什么会这样吗?我对此很陌生,所以一些解释将对我自己的教育有所帮助。

这些是dput()它的基础:

dput(tail(drugsUI, 20))
structure(list(Specialty = c("other", "other", "other", "other", 
"other", "other", "other", "other", "other", "other", "other", 
"other", "other", "other", "other", "other", "ortho", "ortho", 
"ortho", "ortho"), Name = c("Ulipristal", "Ursodeoxycholic acid", 
"Valproic acid", "Valsartan", "Varenicline", "Venlafaxine", "Verapamil", 
"Vigabatrin", "Vildagliptin", "Warfarin", "Zafirlukast", "Zolendronate", 
"Zolpidem", "Zopiclone", "Zotepine", "Zuclopenthixol", "Aspirin only", 
"Clopidogrel or other -grels only", "NSAIDS for pain (e.g. ibuprofen, naproxen, diclofenac)", 
"Dual Antiplatelet (Aspirin AND Clopidogrel)"), Recommendations = c("Continue –may not be needed post op if removing uterine fibroids", 
"Continue", "Continue", "Omit on day of surgery if used for high blood pressure, continue for congestive heart failure", 
"Continue", "Continue – but avoid pethidine use", "Continue", 
"Continue", "See Diabetes Guideline. Usually omitted morning of surgery", 
"See Anticoagulation/Antiplatelet Guideline", "Continue", "Continue - but may be safely omitted if due day of procedure", 
"Continue", "Continue", "Continue", "Continue", "Stop 7 days before surgery", 
"See below graphic", "Stop 2 days before surgery", "Review medical indication - Surgery should be deferred until patient on single antiplatelet (i.e. Aspirin OR Clopidogrel; if not possible discuss with sugeon, anaesthetist and cardiologist about plan.  Ideally continue at least aspirin"
)), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"
))
structure(list(DM = c("Type 2", "Type 2", "Type 2", "Type 2", 
"Type 2", "Type 2", "Type 2", "Type 2", "Type 2", "Type 2", "Type 2", 
"Type 2", "Type 2", "Type 2", "Type 1", "Type 1", "Type 1", "Type 1", 
"Type 1", "Type 1", "Type 1", "Type 1", "Type 1", "Type 1", "Type 1", 
"Type 1", "Type 1", "Type 1", "Type 2", "Type 2", "Type 2", "Type 2", 
"Type 2", "Type 2", "Type 2", "Type 2", "Type 1", "Type 1", "Type 1", 
"Type 1", "Type 1", "Type 1", "Type 1", "Type 1", "Type 2", "Type 2", 
"Type 2", "Type 2", "Type 2", "Type 2", "Type 2", "Type 2", "Type 2", 
"Type 2", "Type 1", "Type 1", "Type 1", "Type 1", "Type 1", "Type 1", 
"Type 1", "Type 1", "Type 1", "Type 1"), Time = c("AM", "AM", 
"AM", "AM", "AM", "AM", "AM", "PM", "PM", "PM", "PM", "PM", "PM", 
"PM", "AM", "AM", "AM", "AM", "AM", "AM", "AM", "PM", "PM", "PM", 
"PM", "PM", "PM", "PM", "AM", "AM", "AM", "AM", "PM", "PM", "PM", 
"PM", "AM", "AM", "AM", "AM", "PM", "PM", "PM", "PM", "AM", "AM", 
"AM", "AM", "AM", "PM", "PM", "PM", "PM", "PM", "AM", "AM", "AM", 
"AM", "AM", "PM", "PM", "PM", "PM", "PM"), Class = c("Long and Intermediate acting", 
"Long and Intermediate acting", "Long and Intermediate acting", 
"Long and Intermediate acting", "Long and Intermediate acting", 
"Long and Intermediate acting", "Long and Intermediate acting", 
"Long and Intermediate acting", "Long and Intermediate acting", 
"Long and Intermediate acting", "Long and Intermediate acting", 
"Long and Intermediate acting", "Long and Intermediate acting", 
"Long and Intermediate acting", "Long and Intermediate acting", 
"Long and Intermediate acting", "Long and Intermediate acting", 
"Long and Intermediate acting", "Long and Intermediate acting", 
"Long and Intermediate acting", "Long and Intermediate acting", 
"Long and Intermediate acting", "Long and Intermediate acting", 
"Long and Intermediate acting", "Long and Intermediate acting", 
"Long and Intermediate acting", "Long and Intermediate acting", 
"Long and Intermediate acting", "Pre-Mixed", "Pre-Mixed", "Pre-Mixed", 
"Pre-Mixed", "Pre-Mixed", "Pre-Mixed", "Pre-Mixed", "Pre-Mixed", 
"Pre-Mixed", "Pre-Mixed", "Pre-Mixed", "Pre-Mixed", "Pre-Mixed", 
"Pre-Mixed", "Pre-Mixed", "Pre-Mixed", "Rapid or Short acting", 
"Rapid or Short acting", "Rapid or Short acting", "Rapid or Short acting", 
"Rapid or Short acting", "Rapid or Short acting", "Rapid or Short acting", 
"Rapid or Short acting", "Rapid or Short acting", "Rapid or Short acting", 
"Rapid or Short acting", "Rapid or Short acting", "Rapid or Short acting", 
"Rapid or Short acting", "Rapid or Short acting", "Rapid or Short acting", 
"Rapid or Short acting", "Rapid or Short acting", "Rapid or Short acting", 
"Rapid or Short acting"), Name = c("Abasaglar", "Lantus", "Levemir", 
"Toujeo", "Tresiba", "Insulatard", "Humulin I", "Abasaglar", 
"Lantus", "Levemir", "Toujeo", "Tresiba", "Insulatard", "Humulin I", 
"Abasaglar", "Lantus", "Levemir", "Toujeo", "Tresiba", "Insulatard", 
"Humulin I", "Abasaglar", "Lantus", "Levemir", "Toujeo", "Tresiba", 
"Insulatard", "Humulin I", "Humulin M3", "Novomix 30", "Insuman Comb 15/25/50", 
"Humalog Mix 25/50", "Humulin M3", "Novomix 30", "Insuman Comb 15/25/50", 
"Humalog Mix 25/50", "Humulin M3", "Novomix 30", "Insuman Comb 15/25/50", 
"Humalog Mix 25/50", "Humulin M3", "Novomix 30", "Insuman Comb 15/25/50", 
"Humalog Mix 25/50", "Novorapid/Fiasp", "Humalog", "Apidra", 
"Humulin S", "Actrapid", "Novorapid/Fiasp", "Humalog", "Apidra", 
"Humulin S", "Actrapid", "Novorapid/Fiasp", "Humalog", "Apidra", 
"Humulin S", "Actrapid", "Novorapid/Fiasp", "Humalog", "Apidra", 
"Humulin S", "Actrapid"), Plan = c("Usual dose at usual time", 
"Usual dose at usual time", "Usual dose at usual time", "Usual dose at usual time", 
"Usual dose at usual time", "Usual dose at usual time", "Usual dose at usual time", 
"Usual dose at usual time", "Usual dose at usual time", "Usual dose at usual time", 
"Usual dose at usual time", "Usual dose at usual time", "Usual dose at usual time", 
"Usual dose at usual time", "Usual dose at usual time", "Usual dose at usual time", 
"Usual dose at usual time", "Usual dose at usual time", "Usual dose at usual time", 
"Usual dose at usual time", "Usual dose at usual time", "Usual dose at usual time", 
"Usual dose at usual time", "Usual dose at usual time", "Usual dose at usual time", 
"Usual dose at usual time", "Usual dose at usual time", "Usual dose at usual time", 
"Half usual morning dose taken with a sugary drink at 7am", "Half usual morning dose taken with a sugary drink at 7am", 
"Half usual morning dose taken with a sugary drink at 7am", "Half usual morning dose taken with a sugary drink at 7am", 
"Half usual morning dose taken with a light breakfast at 7am", 
"Half usual morning dose taken with a light breakfast at 7am", 
"Half usual morning dose taken with a light breakfast at 7am", 
"Half usual morning dose taken with a light breakfast at 7am", 
"Half usual morning dose taken with a sugary drink at 7am", "Half usual morning dose taken with a sugary drink at 7am", 
"Half usual morning dose taken with a sugary drink at 7am", "Half usual morning dose taken with a sugary drink at 7am", 
"Half usual morning dose taken with a light breakfast at 7am", 
"Half usual morning dose taken with a light breakfast at 7am", 
"Half usual morning dose taken with a light breakfast at 7am", 
"Half usual morning dose taken with a light breakfast at 7am", 
"Omit breakfast dose", "Omit breakfast dose", "Omit breakfast dose", 
"Omit breakfast dose", "Omit breakfast dose", "Half usual morning dose taken with a light breakfast at 7am", 
"Half usual morning dose taken with a light breakfast at 7am", 
"Half usual morning dose taken with a light breakfast at 7am", 
"Half usual morning dose taken with a light breakfast at 7am", 
"Half usual morning dose taken with a light breakfast at 7am", 
"Omit breakfast dose", "Omit breakfast dose", "Omit breakfast dose", 
"Omit breakfast dose", "Omit breakfast dose", "Usual morning dose taken with a light breakfast at 7am, oral fluids until 11am, omit lunchtime dose", 
"Usual morning dose taken with a light breakfast at 7am, oral fluids until 11am, omit lunchtime dose", 
"Usual morning dose taken with a light breakfast at 7am, oral fluids until 11am, omit lunchtime dose", 
"Usual morning dose taken with a light breakfast at 7am, oral fluids until 11am, omit lunchtime dose", 
"Usual morning dose taken with a light breakfast at 7am, oral fluids until 11am, omit lunchtime dose"
)), row.names = c(NA, -64L), class = c("tbl_df", "tbl", "data.frame"
))

标签: rshinyshinyapps

解决方案


Shiny 不支持同名的多个输出。看看这个

在您的情况下,两个选项卡都使用px_nameand dob。在您的服务器代码中,您可以尝试以下操作:

output$dob1 <- output$dob2 <- renderText({input$dob})

然后在 UI 中引用选项卡中的这些单独的输出 ID。希望这可以帮助。


推荐阅读