闪亮和清洁的临时文件中关闭会话的问题

时间:2019-02-08 23:08:46

标签: r shiny

我是个新手,我通过用户交互创建了一个应用程序,他们可以上传其文件进行处理。 可视化和创建图形都可以,但是我的问题是:为什么我可以创建一个在会话关闭时删除上传文件的功能?

我尝试了以下几行代码:(在服务器功能内部)

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

我非常感谢所有帮助 非常感谢你!

1 个答案:

答案 0 :(得分:0)

欢迎堆栈溢出!

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)

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