我正在关注闪亮画廊中的simple file upload example,但略有修改。我需要在本地修改csv文件,并查看UI上反映的更改。但是,我认为除非我们对来源的任何变化进行调查,否则这是不可能的。
因此,我通过允许重新上传文件来简化问题。但是,Shiny也没有发生这种情况。一旦文件" file1.csv"上传,我无法再次上传相同的文件。我必须上传一个不同的文件" file2.csv"然后再次原始文件" file1.csv"。
这只是耗费时间,我想知道是否有人遇到过这样的问题,并且可能找到了解决方案。
答案 0 :(得分:3)
添加jquery来清除fileInput的值就可以了。
...
fileInput("csvInput", "Upload CSV file", multiple = TRUE,
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
tags$script('$( "#csvInput" ).on( "click", function() { this.value = null; });'),
...
答案 1 :(得分:1)
实际上这是Shiny的fileInput
模块的缺点之一:如果再次选择相同的文件,并且内置没有force-upload
选项,它会保持静止。
要强制重新上传,基本理念是:
reactiveValues
存储。fileInput
替换原始fileInput
下显示成功上传的消息,以获得更好的用户体验。在ui.R
中,使用
uiOutput('fileImport')
在server.R
:
# reactive storage
v <- reactiveValues()
# fileInput index
v$fileInputIndex <- 1
updateFileInput <- function (name = NULL){
# update output with a new fileInput module
output$fileImport <- renderUI({
index <- isolate(v$fileInputIndex)
result <- div()
result <- tagAppendChild(
result,
fileInput(paste0('file', index), 'Choose CSV File',accept=c('text/csv','text/comma-separated-values,text/plain','.csv'))
)
# show a message of successful uploading
if(!is.null(name)){
result <- tagAppendChild(
result,
div(name," upload complete")
)
}
result
})
}
dataInputRaw <- reactive({
# equals to `input$file1` when initialized
inFile <- input[[paste0('file', v$fileInputIndex)]]
# TICKY PART:
# 1. If initialized, `inFile` and `v$data` are both `NULL`
# 2. After each uploading, new `fileInput` is applied and
# we want to keep previous updated data.
# It also prevent recursive creation of new `fileInput`s.
if (is.null(inFile)){
return(v$data)
}
# load as data frame
v$data <- data.frame(read.csv(inFile$datapath))
# if file successfuly uploaded, increate the index
# then upload `fileInput` with successful message
if (!is.null(v$data)){
v$fileInputIndex <- v$fileInputIndex + 1
updateFileInput(name = inFile$name)
}
# return data
v$data
})
# init
updateFileInput()
我已经测试了这个片段,但它确实有用。