我正在尝试制作一个非常简单的应用程序,将用户选择的文件传输到固定位置。然后打印传输结果(True / False)。复制的语法在闪亮之外工作
library(shiny)
ui <- fluidPage(
fileInput('file1', 'Choose 1st File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.fastq' , '.fasta')) ,
renderText('result')
)
server <- function(input , output){
output$result <- renderPrint({
file.copy(from = input$file1$datapath,
to = 'H:/Shiny/FileTransfer/TestLocation',
recursive = FALSE,
copy.mode = TRUE)
})
}
shinyApp(ui = ui , server = server)
Run Code Online (Sandbox Code Playgroud)
您的代码有 2 个问题。1)该renderPrint函数永远不会被执行,verbatimTextOutput而是使用。2) 中的文件名$datapath与原始文件名不同,应指明 中的原始文件名$name,否则目标文件将具有类似名称0或一些随机数字。
下面是您的代码,其中包含一些可能对您有用的额外信息。该函数file.path用于除了原始文件名之外还指示目标目录。
library(shiny)
ui <- fluidPage(
fileInput('file1', 'Choose 1st File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.fastq' , '.fasta')) ,
verbatimTextOutput('result')
)
server <- function(input , output){
destDir <- 'H:/Shiny/FileTransfer/TestLocation'
output$result <- renderPrint({
inFile <- input$file1
if (is.null(inFile)) {
cat("NOT FILE\n")
return(FALSE)
}
cat("Reading file:", inFile$name, "\n")
cat("size:", inFile$size, " Bytes, type:", inFile$type, "\n")
if (dir.exists(destDir)){
cat("Copying file to:", destDir,"\n")
result <- file.copy( inFile$datapath,
file.path(destDir, inFile$name) )
} else {
result <- FALSE
}
result
})
}
shinyApp(ui = ui , server = server)
Run Code Online (Sandbox Code Playgroud)