多标签页下Shiny应用部分功能失效问题求助
问题原因分析与修复方案:Shiny双标签页输出丢失问题
问题概述
你在Shiny应用中新增"Long-term Medications"标签页后,发现mainPanel的大部分输出丢失;移除该标签页后,"Insulin"标签页功能恢复正常。下面是具体的原因分析和修复方案。
核心原因分析
- 输入/输出ID不匹配
- "Long-term Medications"标签页的UI中定义了
tableOutput("drugs_table"),但server中对应的输出是output$drug_table <- renderTable(...)(少了一个s),导致表格无法渲染。 - 两个标签页都试图使用
px_name和dob的文本输出,但UI中完全没有定义这两个输入控件(比如textInput),当双标签页共存时,Shiny无法找到这些输入的来源,导致反应式上下文混乱,影响其他输出的渲染。
- "Long-term Medications"标签页的UI中定义了
- 拼写错误
drugs_sel中的choices = lsit(...)是list的拼写错误,会导致选择框无法正常生成。my_drugs_table中的select(..., Recommnedations)是Recommendations的拼写错误,会导致表格列缺失。
- 未初始化的数据对象
- server中使用了
drugsUI,但代码开头没有加载该数据,需要确保它被正确读取或初始化。
- server中使用了
修复后的完整代码
library(tidyverse) library(shiny) library(shinythemes) library(xtable) # 初始化数据(用你提供的dput结果) insulin <- 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" )) drugsUI <- 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" )) # 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"), # 添加缺失的患者信息输入 textInput("px_name_drugs", "Patient Name", ""), textInput("dob_drugs", "Date of Birth", "") ), mainPanel( br(), uiOutput("drugs_sel"), h3(textOutput(outputId = "px_name_drugs")), br(), h4(textOutput(outputId = "dob_drugs")), br(), tableOutput("drugs_table") # 修正ID为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")), # 添加缺失的患者信息输入 textInput("px_name_insulin", "Patient Name", ""), textInput("dob_insulin", "Date of Birth", "") ), 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_insulin")), br(), h4(textOutput(outputId = "dob_insulin")), br(), tableOutput("insulin_table"), radioButtons('format', 'Document format', c('PDF'), inline = TRUE), downloadButton('downloadReport') ) ) ) ) ) server <- function(input, output){ my_insulin_table <- reactive({ req(input$class, input$name) # 添加req确保输入存在后再计算 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({ req(input$specialty, input$drug) # 添加req确保输入存在后再计算 drugs_subset <- drugsUI %>% filter(Specialty == input$specialty) drug_tab <- drugs_subset %>% filter(Name %in% input$drug) %>% select(Name, Recommendations) # 修正拼写错误 return(drug_tab) }) # 修正为对应标签页的输入ID output$px_name_drugs <- renderText({input$px_name_drugs}) output$dob_drugs <- renderText({input$dob_drugs}) output$px_name_insulin <- renderText({input$px_name_insulin}) output$dob_insulin <- renderText({input$dob_insulin}) output$drugs_sel <- renderUI({ drugs_subset <- drugsUI %>% filter(Specialty == input$specialty) selectizeInput("drug", "Type in name of drug", choices = list("Type in name of drug" = "", "Names" = drugs_subset$Name), # 修正拼写错误lsit->list selected = NULL, multiple = TRUE, options = NULL) }) output$drugs_table <- renderTable({ xtable(my_drugs_table()) }) # 修正ID为drugs_table output$insulin_sel <- renderUI({ req(input$class) # 添加req确保class被选择后再生成选择框 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"},
相关产品推荐
相关产品推荐

