英文:
How to clear fileInput data and corresponding objects in R Shiny?
问题
After looking at some existing solutions, I still could not solve the problem.
我在查看了一些现有的解决方案后,仍然无法解决这个问题。
I want to delete every data that was uploaded in my shiny app after pressing the sign_out button (which of course also logs out the users).
我想在按下“sign_out”按钮后删除在我的shiny应用程序中上传的所有数据(当然也会注销用户)。
To upload data I am using the fileInput() command in Shiny.
为了上传数据,我在Shiny中使用了fileInput()命令。
The corresponding data object based on this input is called df:
基于此输入的相应数据对象称为df:
My current approach to remove this is the following:
我当前的方法来删除它如下:
The output$test object shows that after re-login, the data of "df" is still there.
output$test对象显示,在重新登录后,“df”的数据仍然存在。
Hope you could help me and thank you in advance.
希望你能帮助我,提前谢谢。
英文:
After looking at some exisiting solutions I still could not solve the problem.
I want to delete every data that was uploaded in my shiny app after pressing the sign_out button (which of course also logs out the users).
To upload data I am using the fileInput() command in Shiny.
fileInput("file1", "Choose CSV File",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv", "space"))
The corresponding data object based on this input is called df:
output$contents <- renderTable({
req(input$file1)
df <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
})
My current approach to remove this is the following:
observeEvent(input$sign_out, {
df <- NULL
reset(id = "") # from shinyjs package
f$sign_out()
output$test <- renderTable({
df()})
})
The output$test object shows that after relogin the data of "df" is still there.
Hope you could help me and thank you in advance.
答案1
得分: 1
所以我认为我可以解决这个问题。不是将任何响应值设置为NULL,而是删除上传数据时创建的csv文件。
完成此操作的关键函数是unlink
。
observeEvent(input$sign_out, {
unlink(input$file1$datapath)
reset(id = "")
})
英文:
so I think I could solve the issue. Instead of setting any reactive Value to NULL I deleted the csv. file which is created when uploading data.
The key function for that is unlink
.
observeEvent(input$sign_out, {
unlink(input$file1$datapath)
reset(id = "") # from shinyjs package
})
答案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 })
最后,当用户登出时,清空 uploadedData,请通过将 values$uploadedData()
设置为 NULL 来修改 observeEvent():
observeEvent(input$sign_out, {
values$uploadedData <- NULL
reset(id = "")
f$sign_out()
})
尝试一下,看看这是否对你有效。
英文:
what you could do is, first, at the beginning of your code, just add this line of code:
values <- reactiveValues(uploadedData = NULL)
Secondly, you should modify the renderTable()
function to use values$uploadedData
instead of df
:
renderTable({
req(input$file1)
values$uploadedData <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
And also,you must replace the line
output$test <- renderTable({ df() })
With this line of code:
output$test <- renderTable({ values$uploaded_data })
Lastly, when the user logs out, to empty the uploadedData, modify the observeEvent() by setting the values$uploadedData() to NULL :
observeEvent(input$sign_out, {
values$uploadedData <- NULL
reset(id = "") # from shinyjs package
f$sign_out()
})
Try this, and let me know if this works for you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论