【问题标题】:problems with close session in shiny and cleaned temp files闪亮和清洁的临时文件中的关闭会话问题
【发布时间】:2019-02-09 10:11:59
【问题描述】:

我是闪亮的新手,我创建了一个具有用户交互的应用程序,他们可以上传他们的文件进行处理。 可视化和创建图表一切正常,但我的问题是:为什么我可以创建一个函数来在会话关闭时删除上传的文件?

我尝试了这些代码行:(在服务器函数内部)

session$onSessionEnded(function() {
   if (!is.null(input$file1)) {
file.remove(input$file1$datapath)
  }
})

和:

onStop(function() {
   if (!is.null(input$file1)) {
file.remove(input$file1$datapath) }
})

带有此错误消息:

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.)
  41: stop
  40: .getReactiveEnvironment()$currentContext
  39: .subset2(x, "impl")$get
  38: $.reactivevalues

我非常感谢所有类型的帮助 非常感谢!

【问题讨论】:

  • 可能是if(!is.null(isolate(input$file1)))file.remove(isolate(input$file1$datapath))
  • 对我不起作用...但感谢您的帮助!

标签: r shiny


【解决方案1】:

欢迎堆栈溢出!

input 变量只能通过响应式上下文访问,因此会出现错误。响应式上下文是任何 renderobservereactiveobserveEventeventReactive 调用。

此外,临时文件会在会话结束时自动删除。以下是来自https://shiny.rstudio.com/gallery/file-upload.html 的稍微修改的示例。

library(shiny)

# Define UI for data upload app ----
ui <- fluidPage(

  # App title ----
  titlePanel("Uploading Files"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Select a file ----
      fileInput("file1", "Choose CSV File",
                multiple = FALSE,
                accept = c("text/csv",
                           "text/comma-separated-values,text/plain",
                           ".csv")),

      # Horizontal line ----
      tags$hr(),

      # Input: Checkbox if file has header ----
      checkboxInput("header", "Header", TRUE),

      # Input: Select separator ----
      radioButtons("sep", "Separator",
                   choices = c(Comma = ",",
                               Semicolon = ";",
                               Tab = "\t"),
                   selected = ","),

      # Input: Select quotes ----
      radioButtons("quote", "Quote",
                   choices = c(None = "",
                               "Double Quote" = '"',
                               "Single Quote" = "'"),
                   selected = '"'),

      # Horizontal line ----
      tags$hr(),

      # Input: Select number of rows to display ----
      radioButtons("disp", "Display",
                   choices = c(Head = "head",
                               All = "all"),
                   selected = "head")

    ),

    # Main panel for displaying outputs ----
    mainPanel(

      # Output: Data file ----
      tableOutput("contents")

    )

  )
)

# Define server logic to read selected file ----
server <- function(input, output) {

  output$contents <- renderTable({

    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, head of that data file by default,
    # or all rows if selected, will be shown.

    req(input$file1)

    # when reading semicolon separated files,
    # having a comma separator causes `read.csv` to error
    tryCatch(
      {
        df <- read.csv(input$file1$datapath,
                       header = input$header,
                       sep = input$sep,
                       quote = input$quote)

        cat(input$file1$datapath)
      },
      error = function(e) {
        # return a safeError if a parsing error occurs
        stop(safeError(e))
      }
    )

    if(input$disp == "head") {
      return(head(df))
    }
    else {
      return(df)
    }

  })

}

# Create Shiny app ----
shinyApp(ui, server)

运行应用程序,并在它运行时在控制台中显示临时文件路径。关闭应用程序并结束会话后,请尝试再次遵循该路径,该路径将被删除。

【讨论】:

  • 感谢@Sada93 的帮助,您对输入和反应值问题的解释非常有用...感谢您的代码,我更好地理解了如何删除临时文件。最后我用这行代码解决了我的问题:session$onSessionEnded(function() { file.remove(file.path(dir(pattern = ".csv"))) })
  • @LuisCano 但闪亮会自动删除文件。如果你想强制删除director,你可以使用tempdir()访问临时目录
猜你喜欢
  • 1970-01-01
  • 2018-09-22
  • 2011-07-30
  • 1970-01-01
  • 2017-02-25
  • 1970-01-01
  • 1970-01-01
  • 2011-07-02
  • 2015-07-09
相关资源
最近更新 更多