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

Shiny应用关闭会话时清理上传临时文件失败求助

Fixing Shiny Session End File Cleanup Error

Hey there! I see you're running into a reactive context error when trying to delete uploaded files on session end—let's break this down and fix it.

Why the Error Happens

The error you're seeing:

Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

This occurs because input$file1 is a reactive value, and you're trying to access it outside of an active reactive context (like inside session$onSessionEnded or onStop callbacks). By the time the session ends, Shiny's reactive system is already shut down, so it blocks any attempts to access reactive inputs directly.

The Solution: Store the File Path First

Instead of accessing input$file1 directly when the session ends, we need to save the file path to a persistent storage (a reactive value we can safely access later) when the file is uploaded. Here's how to do it step by step:

  1. Create a reactive value to hold the file path
    Inside your server function, initialize a reactiveValues object to store the path of the uploaded file:

    server <- function(input, output, session) {
      # Store the uploaded file path for later cleanup
      rv <- reactiveValues(uploaded_file_path = NULL)
    
  2. Save the path when a file is uploaded
    Use an observeEvent to listen for file upload events and save the file's datapath to our reactive value:

    observeEvent(input$file1, {
        # Save the file path as soon as it's uploaded
        rv$uploaded_file_path <- input$file1$datapath
        
        # Your existing file processing/visualization code goes here
        # ...
      })
    
  3. Clean up the file when the session ends
    Now, use the stored path in your session$onSessionEnded callback—no need to access input anymore:

    session$onSessionEnded(function() {
        # Double-check the file exists before trying to delete it
        if (!is.null(rv$uploaded_file_path) && file.exists(rv$uploaded_file_path)) {
          file.remove(rv$uploaded_file_path)
          message("Uploaded file deleted successfully.")
        }
      })
    }
    

Full Working Example

Putting it all together, here's a minimal reproducible example you can test:

library(shiny)

ui <- fluidPage(
  fileInput("file1", "Upload a CSV File"),
  plotOutput("data_plot")
)

server <- function(input, output, session) {
  rv <- reactiveValues(uploaded_file_path = NULL)
  
  observeEvent(input$file1, {
    rv$uploaded_file_path <- input$file1$datapath
    
    # Example: Process and plot the uploaded data
    uploaded_data <- read.csv(rv$uploaded_file_path)
    output$data_plot <- renderPlot({
      hist(uploaded_data[[1]], main = "Histogram of Uploaded Data")
    })
  })
  
  session$onSessionEnded(function() {
    if (!is.null(rv$uploaded_file_path) && file.exists(rv$uploaded_file_path)) {
      file.remove(rv$uploaded_file_path)
      message("Uploaded file cleaned up.")
    }
  })
}

shinyApp(ui, server)

Key Notes

  • The file.exists() check prevents errors if the file was already deleted for some unexpected reason.
  • This approach works with onStop() too—just replace session$onSessionEnded with onStop and use the same stored path.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 09:30:12