当ssession关闭时,如何删除由Shiny应用程序创建的文件

时间:2018-03-08 16:40:32

标签: r shiny

我在Shiny应用程序中生成并显示flextable,并希望将其放在PDF中。唯一可用的方法是将flextable对象转换为PNG,然后将PNG放在PDF中。对于每个PNG文件,我分配一个包含日期时间戳的文件名,以使其在会话之间唯一。此文件名保存在reactiveValue中。

当用户完成并且会话关闭时,我该如何删除文件?如果我不这样做,我会堆积无关的文件。我不能使用onSessionEnded(),因为当浏览器关闭时,被动值都消失了。我无法概括使用模式,因为其他用户具有类似名称的文件。我必须专门删除这些PNG文件。

有什么想法吗?

onSessionEnded代码不起作用:

observe({
  session$onSessionEnded(function() {
    unlink(c(values$fnameSummary))
    unlink(c(values$fnameLike))
    unlink(c(values$fnameRisk1))
  })
})

这会产生以下错误:

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.)
Stack trace (innermost first):
    33: .getReactiveEnvironment()$currentContext
    32: .subset2(x, "impl")$get
    31: $.reactivevalues
    30: $
    29: unlink
    28: callback [C:\Users\jch1748\Documents\Projects\W2017010 - Combined Risk Tool\testing/server.R#2790]
     1: runApp

也许一个有用的例子会有帮助吗?

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)
tsts <- reactiveValues()
# Define UI for application that draws a histogram
ui <- fluidPage(

   # Application title
   titlePanel("Old Faithful Geyser Data"),

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30)
      ),

      # Show a plot of the generated distribution
      mainPanel(
         plotOutput("distPlot")
      )
   )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

   output$distPlot <- renderPlot({
      # generate bins based on input$bins from ui.R
      x    <- faithful[, 2] 
      bins <- seq(min(x), max(x), length.out = input$bins + 1)

      # draw the histogram with the specified number of bins
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
   })

   observe({
     tsts$fname <- "AAA.txt"
     write(input$bins, file=tsts$fname)
   })

    onSessionEnded(function() {
     cat("Session Ended\n")
     unlink(tsts$fname)
     }) 
}

# Run the application 
shinyApp(ui = ui, server = server)

2 个答案:

答案 0 :(得分:0)

我有一个类似的问题,我想动态提供图像和pdf文件,以便在闪亮的应用程序中下载。因此,需要将文件放置在 www 目录中,这使得无法使用 tempdir 。此外,在应用停止后,需要删除创建的文件。我用以下代码解决了这个问题:

session$onSessionEnded(function() {
  system(paste("rm -f", PathToFile))
})

答案 1 :(得分:0)

无需使用reactValues。请参阅:

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("Old Faithful Geyser Data"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {
  fname <- "AAA.txt"
  
  output$distPlot <- renderPlot({
    # generate bins based on input$bins from ui.R
    x    <- faithful[, 2] 
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    
    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'darkgray', border = 'white')
  })
  
  observe({
    write(input$bins, file=fname)
  })
  
  session$onSessionEnded(function() {
    cat("Session Ended\n")
    unlink(fname)
  }) 
}

# Run the application 
shinyApp(ui = ui, server = server)