将用户上传的文件存储到指定的本地文件夹

Store user-upload file to specified local folder

我想创建一个应用程序,用户 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 ("a reactive expression or observer") 中。