我用 shinyDirChoose
保存了用户定义文件夹的路径 . 现在我想从该用户的文件夹上传文件,但我无法弄清楚如何做到这一点 . 1)所有在服务器端? 2)以某种方式将文件路径提供给 fileInput
?
这是我为应该上传的三个文件构建文件路径的方法 .
### ui end, to browse to desired folder
ui = fluidPage(shinyDirButton('directory', 'Folder select', 'Please select a folder'))
### extracting the folder path
server = function(input, output, session) {
volumes <- getVolumes()
shinyDirChoose(input, 'directory', roots=volumes, session=session)
path1 <- reactive({
return(print(parseDirPath(volumes, input$directory)))
})
### constructing the 3 file paths
datpat <- renderText({
req(nchar(path1())>0)
datpat <- paste0(path1(),"/data.csv")
})
vispat <- renderText({
req(nchar(path1())>0)
vispat <- paste0(path1(),"/visit.csv")
})
statpat <- renderText({
req(nchar(path1())>0)
statpat <- paste0(path1(),"/statvisit.csv")
})
所以现在我有这些路径,但是如何使用它们将相关内容上传到服务器?遗憾的是,一个简单的 read.csv
没有做到这一点 .
EDIT - but not there yet...
在@SBista提供的帮助下进一步工作,我想我正在接近我的目标,但是看下面的代码......
volumes <- getVolumes()
shinyDirChoose(input, 'directory', roots=volumes, session=session)
path1 <- reactive({
return(print(parseDirPath(volumes, input$directory)))
})
observe({
if(!is.null(path1)){
### vis1
vis1 <- reactive({
datpat <- paste0(path1(),"/visit.csv")
vis <- read.csv(datpat, header = input$header, sep = input$sep, quote = input$quote,
stringsAsFactors = FALSE)
vis
})
### dataruw1
dataruw1 <- reactive({
datpat <- paste0(path1(),"/data.csv")
dataruw <- read.csv(datpat, header = input$header, sep = input$sep, quote = input$quote,
stringsAsFactors = FALSE)
dataruw
})
}
})
不幸的是,似乎没有生成 dataruw1
和 vis1
,因为我在尝试使用 dataruw1()
和 vis1()
的实际数据时遇到了'could not find function'错误 . 我错过了什么?任何的想法?非常感谢提前!
2 回答
很高兴找到了解决方案!非常感谢SBista,尽管我做的有点不同 . 如果您浏览到包含 data.csv 文件的文件夹,下面的代码不仅仅是一个块(如我原来的帖子中所示),而是功能齐全的 .
我修改了您的应用以证明您可以使用
read.csv
上传文件 . 为了演示,我只从文件夹中读取一个文件,并在数据表中显示读取数据帧 .希望能帮助到你!