如何清除 R Shiny 中的 fileInput 数据和相应的对象?

问题描述 投票:0回答:2

看了一些现有的解决方案,还是没能解决问题。

我想在按下注销按钮(当然也会注销用户)后删除我闪亮的应用程序中上传的所有数据。

要上传数据,我在 Shiny 中使用 fileInput() 命令。

        fileInput("file1", "Choose CSV File",
                  multiple = FALSE,
                  accept = c("text/csv",
                             "text/comma-separated-values,text/plain",
                             ".csv", "space"))

根据这个输入对应的数据对象叫做df:


output$contents <- renderTable({
req(input$file1)

        df <- read.csv(input$file1$datapath,
                       header = input$header,
                       sep = input$sep,
                       quote = input$quote)
})

我目前删除它的方法如下:


  observeEvent(input$sign_out, {
    df <- NULL 
    reset(id = "") # from shinyjs package 
    f$sign_out()
    output$test <- renderTable({
      df()})
    
  })

output$test对象显示重新登录后“df”的数据还在。

希望你能帮助我,提前谢谢你。

shiny shinyjs
2个回答
0
投票

你可以做的是,首先,在代码的开头,添加这行代码:

values <- reactiveValues(uploadedData = NULL)

其次,您应该修改

renderTable()
函数以使用
values$uploadedData 
而不是
df
:

renderTable({
req(input$file1)

        values$uploadedData <- read.csv(input$file1$datapath,
                             header = input$header,
                             sep = input$sep,
                             quote = input$quote)

还有,你必须更换线

output$test <- renderTable({ df() })

使用这行代码:

output$test <- renderTable({ values$uploaded_data }) 

最后,当用户注销时,要清空上传数据,通过将 values$uploadedData() 设置为 NULL 来修改 observeEvent():

observeEvent(input$sign_out, {
  values$uploadedData <- NULL 
  reset(id = "") # from shinyjs package 
  f$sign_out()
})

试试这个,让我知道这是否适合你。


0
投票

所以我想我可以解决这个问题。我没有将任何反应值设置为 NULL,而是删除了 csv。上传数据时创建的文件。

关键功能是

unlink
.

  observeEvent(input$sign_out, {
    unlink(input$file1$datapath)
    reset(id = "") # from shinyjs package
  })
© www.soinside.com 2019 - 2024. All rights reserved.