为什么使用多个标签时代码只能部分工作?

问题描述 投票:0回答:1

我之前发布了一个关于应用创意here的问题。我决定在同一应用程序中添加类似的内容,以便每个选项卡具有相似的功能,但处理不同的药物说明集。我试图跟踪查询并发布解决方案here,但是以某种方式丢失了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)

如果删除名为tabPanel"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"
))
r shiny shinyapps
1个回答
0
投票

Shiny不支持具有相同名称的多个输出。看看this

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

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

然后在UI中,在选项卡中引用这些单独的输出ID。希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.