r - 将用户上传的文件存储到指定的本地文件夹
问题描述
我想创建一个应用程序,用户 A 将文件上传到服务器,用户 B 可以将其下载到本地文件夹。我首先实现上传文件的操作,然后立即将该文件存储到我自己指定的文件夹中(因为我是这里的唯一用户)。这是代码:
library(shiny)
ui <- fluidPage(
fileInput('file1', 'Choose csv File',
accept=c('text/csv'))
)
server <- function(input , output){
rootDir <- 'C:/RShiny/Dir'
inFile <- reactive({input$file1})
file.copy(inFile()$datapath,
file.path(rootDir, inFile()$name, fsep = .Platform$file.sep))
}
shinyApp(ui = ui , server = server)
但是,我不断收到此错误消息:
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.)
53: stop
52: .getReactiveEnvironment()$currentContext
51: getCurrentContext
50: .dependents$register
49: inFile
47: server [C:\RShiny\.../app.R#12]
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.)
该应用程序立即关闭。不知道这意味着什么以及如何解决它。如果有人可以帮助解释?
谢谢,
解决方案
请尝试以下方法:
library(shiny)
ui <- fluidPage(
fileInput('file1', 'Choose csv File',
accept=c('text/csv'))
)
server <- function(input , output){
rootDir <- 'C:/RShiny/Dir'
inFile <- reactive({input$file1})
observe({
file.copy(inFile()$datapath,
file.path(rootDir, inFile()$name, fsep = .Platform$file.sep))
})
}
shinyApp(ui = ui , server = server)
您需要将file.copy()
代码放在observe
(“反应式表达式或观察者”)中。
推荐阅读
- amazon-ec2 - 从 Windows 10 访问具有屏幕共享的 AWS EC2 Mac 实例会导致身份验证错误
- python - 如何在 kivymd 的单个屏幕中添加多个布局
- flutter - 颤振 - 'title' 或 'RaisedButton' 被交叉
- reactjs - 在 plesk 黑曜石问题上部署反应应用程序
- javascript - 如何在 Jimp 中打印表情符号 - Node Js | 吉普
- sql-server - 仅在 SQL Server 数据库中向具有主键列的表中插入记录
- architecture - RDBMS 可以像 noSQL 一样在读取上扩展吗?
- java - 我的 Spring Boot JAR 文件无法通过 Maven/Eclipse 部署,它总是关闭
- android - 如何拆分长文本以适合 Textview Android
- sql - Oracle SQL 中缺少 IN 或 OUT 参数