You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shiny App未上传文件及未点击Process的下载异常处理问题

解决方案:修复Shiny应用的下载验证逻辑

以下是修正后的完整代码,解决了未上传文件点击下载无弹窗、未判断Process按钮状态的问题,同时完善了流程逻辑:

library(shiny)
library(readxl)
library(tidyverse)
library(writexl)
library(bsplus)
library(shinyalert)
library(shinyjs)
library(lubridate)

ui <- fluidPage(
  useShinyjs(),
  useShinyalert(),
  titlePanel("Regularizer 15 min"),
  tags$div(id = "sidebar",
           sidebarPanel(
             bs_accordion(
               id = "accordion"
             ) %>%  
               bs_set_opts(
                 use_heading_link = TRUE
               ) %>%  
               bs_append(
                 title = tags$div(tags$i(class = "fa-solid fa-upload"),"Upload"),
                 content = fileInput("upload", "Upload a Excel File",  
                                     accept = c(".xlsx",".csv"))
               ),
             actionButton("act_button1", "Process"),
             use_bs_accordion_sidebar()
           )),
  mainPanel(id = "mainPanel",
            tags$div(class = "download-btn",
                     downloadButton("downloadData_all", "Download Regularized ")
            )
  )
)

server <- function(input, output, session) {
  
  options(shiny.maxRequestSize = 100*1024^2)
  
  session$onSessionEnded(function() {
    stopApp()
  })
  
  # 反应式变量:跟踪文件是否已处理
  processed <- reactiveVal(FALSE)
  # 反应式变量:存储处理后的结果数据
  processed_data <- reactiveVal(NULL)
  
  # Process按钮逻辑:处理上传文件并标记状态
  observeEvent(input$act_button1, {
    req(input$upload)
    
    # 替换为你的实际文件处理逻辑
    raw_data <- readxl::read_excel(input$upload$datapath, sheet = 1)
    data_processed <- raw_data %>% 
      mutate(processed_timestamp = now())
    
    processed_data(data_processed)
    processed(TRUE)
    shinyalert("完成", "文件已处理完毕", type = "success")
  })
  
  # 下载按钮逻辑:添加完整验证
  output$downloadData_all <- downloadHandler(
    filename = function() {
      req(input$upload)
      paste0("regularised_", input$upload$name)
    },
    content = function(file) {
      # 验证1:是否上传文件
      if (is.null(input$upload)) {
        shinyalert("错误", "请先上传文件", type = "error")
        return()
      }
      
      # 验证2:是否已点击Process处理文件
      if (!processed()) {
        shinyalert("错误", "请先点击Process按钮处理文件", type = "error")
        return()
      }
      
      # 写入处理后的数据
      writexl::write_xlsx(processed_data(), path = file)
    }
  )
}

shinyApp(ui, server)

关键修改说明

  1. 添加状态跟踪变量

    • processed <- reactiveVal(FALSE):标记文件是否已完成处理
    • processed_data <- reactiveVal(NULL):存储处理后的结果,避免重复处理文件
  2. 完善Process按钮功能

    • 用req(input$upload)确保只有上传文件后才能触发处理
    • 加入示例处理逻辑(可替换为你的业务代码),处理完成后标记processed(TRUE)并弹出成功提示
  3. 修复下载验证逻辑

    • 在downloadHandler的content函数中添加两层验证:
      • 未上传文件时弹出错误提示并终止下载
      • 已上传但未处理时弹出错误提示并终止下载
    • 仅当两个条件都满足时,才写入处理后的数据到下载文件
  4. 修复文件名生成的潜在错误

    • 在filename函数中加入req(input$upload),避免未上传文件时调用input$upload$name导致报错

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 05:30:07