You need to enable JavaScript to run this app. R Shiny应用如何根据选中的Domain选项仅执行对应R脚本
优惠活动
大模型
产品
解决方案
定价
更多

R Shiny应用如何根据选中的Domain选项仅执行对应R脚本

问题根因

你当前的代码存在3个核心错误导致脚本同时触发:

  • UI元素ID全局重复:2个下拉选框都用了pdfExtract1、2个提取按钮都用了pdfExtract,Shiny识别到重复ID后会触发所有绑定的监听事件
  • 服务端给同一个input$pdfExtract1绑定了2个observeEvent,只要该值发生变化,两个脚本都会被执行
  • 误将下拉选框作为事件触发器,实际应该监听「提取按钮」的点击事件,再根据当前选中的下拉值决定运行对应脚本

修复后的完整代码

library(shiny)
options(shiny.maxRequestSize=30*1024^2)
shinyApp(
  ui = tagList(
    navbarPage(
      theme = "spacelab",
      "Dataset",
      tabPanel("Study report extracting",
               sidebarPanel(
                 fileInput("file1", "Select datasets:",
                           accept = c(
                             "text/csv",
                             "text/comma-separated-values,text/plain",
                             ".csv")
                 ),
                 tags$hr(),
                 checkboxInput("header", "Header", TRUE),
                 textInput("txt", "Study info:", "Study name read"),
                 sliderInput("slider", "Tables to read:", 1, 100, 30),
                 tags$h5("Prepare extraction"),
                 actionButton("dataset", "Extract", class = "btn-primary")
               ),
               mainPanel(
                 tableOutput("contents"),
                 tabsetPanel(
                   
                   tabPanel("PDF File select",
                            h4("Domains"),
                            tableOutput("table"),
                            h3("Extracting..."),
                            # 合并为单个下拉选,包含两个Domain选项,使用唯一ID
                            selectInput("select_domain", "Pick a PDF", choices = c('Domain1','Domain2')),
                            tableOutput("preview"),
                            # 提取按钮使用唯一ID
                            actionButton("pdf_extract_btn", "Extract", class = "btn-primary")
                   ),
                   tabPanel("Raw data", "TBD"),
                   tabPanel("calculation", "TBD")
                 )
               )
      ),
      tabPanel("Calculation",
               sidebarPanel(
                 fileInput("file2", "Select datasets:",
                           accept = c(
                             "text/csv",
                             "text/comma-separated-values,text/plain",
                             ".csv")
                 ),
                 tags$hr(),
                 checkboxInput("header", "Header", TRUE),
                 textInput("txt2", "Study info:", "Study name read"),
                 # 修复slider的重复ID
                 sliderInput("slider2", "Tables to read:", 1, 100, 30),
                 tags$h5("Preparing the calculation"),
                 actionButton("dataset2", "Extract", class = "btn-primary")
               ),
               mainPanel(
                 tableOutput("contents2"),
                 
                 tabsetPanel(
                   tabPanel("Datasets",
                            h4("Domains"),
                            tableOutput("table2"),
                            h3("Calculating...")
                   )
                 )
               )
      ),
      tabPanel("Comparision",
               sidebarPanel(
                 fileInput("file3", "Select study report and datasets:"),
                 textInput("txt3", "Study info:", "Study name read"),
                 tags$h5("Prepare comparison"),
                 actionButton("action2", "Compare", class = "btn-primary")
               ),
               mainPanel(
                 tabsetPanel(
                   tabPanel("PDF File select",
                            h4("Domains"),
                            tableOutput("table3"),
                            h3("Comparing..."),
                   ),
                   tabPanel("calculation data", "TBD")
                 )
               )
      )
    )
  ),
  server = function(input, output, session) {
    output$contents <- renderTable({
      inFile <- input$file1
      
      if (is.null(inFile))
        return(NULL)
      
      read.csv(inFile$datapath, header = input$header)
    })
    output$contents2 <- renderTable({
      inFile <- input$file2
      
      if (is.null(inFile))
        return(NULL)
      
      read.csv(inFile$datapath, header = input$header)
    })
    output$txtout <- renderText({
      paste(input$txt, input$slider, sep = ", ")
    })
    output$table <- renderTable({
      data.frame(Domains = c("Domain1","Domain2","Domain3","Domain4"))
    })
    output$table2 <- renderTable({
      data.frame(Domains = c("Domain1","Domain2","Domain3","Domain4"))
    })
    output$table3 <- renderTable({
      data.frame(Domains = c("Domain1","Domain2","Domain3","Domain4"))
    })
    observeEvent(input$dataset, {
      source("Domain1.R", local = TRUE)
    })
    observeEvent(input$dataset2, {
      source("calculation.R", local = TRUE)
    })
    # 仅监听提取按钮点击事件,根据选中值执行对应脚本
    observeEvent(input$pdf_extract_btn, {
      if(input$select_domain == "Domain1"){
        source("Domain1.R", local = TRUE)
      } else if(input$select_domain == "Domain2"){
        source("Domain2.R", local = TRUE)
      }
    })
  }
)

关键修改说明

  • 合并两个重复的下拉选框为单个选框,使用唯一ID select_domain,同时包含Domain1和Domain2两个可选值
  • 提取按钮ID改为唯一值pdf_extract_btn,彻底解决重复ID导致的事件冲突问题
  • 服务端删除绑定到下拉选框的两个重复observeEvent,改为仅监听提取按钮的点击事件,通过判断选中的下拉值执行对应脚本
  • 同步修复了slider控件重复ID、table输出结构错误的小问题,消除运行警告

内容的提问来源于stack exchange,提问作者Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 01:54:03