让 R Shiny 应用程序用户将图像上传到保管箱/闪亮服务器/google 驱动器
have Rshiny app user upload image to dropbox /shiny server / google drive
我几乎完成了使用 Rshiny 创建的调查。我想创建一个按钮,允许用户将图像(jpeg、tiff、png 等)上传到 R shiny 服务器、Dropbox 或 google 驱动器。 fileInput 方法似乎只接受像 csv 这样的文件?任何帮助,将不胜感激!
fileInput
实际上允许导入任何类型的文件。您只需将参数 accept
设置为 NULL
或您接受的任何文件扩展名。你首先需要明白的是,如果你使用fileInput
,它实际上会将你的文件上传到tmp文件夹(上传数据的路径存储在input$file$datapath
中),然后你才会能够将您的文件上传到 googledrive 或任何云。使用 fileInput
的解决方案可能如下所示:
library(googledrive)
ui <- fluidPage(
fileInput(inputId = "file",
label = "Choose file to upload",
accept = NULL)
)
server <- function(input, output) {
observeEvent(input$file, {
drive_upload(media = input$file$datapath,
name = input$file$name)
})
}
shinyApp(ui, server)
如果 "double upload" 对您来说是个问题,您可以使用包 shinyFiles
来避免这个问题。已经发布了一个很好的答案 ,这里有一种调整代码以解决您的特定问题的方法。
library(googledrive)
library(shinyFiles)
ui <- fluidPage(
shinyFilesButton("Btn_GetFile", "Choose file to upload" ,
title = "Please select a file:", multiple = FALSE,
buttonType = "default", class = NULL)
)
server <- function(input, output, session) {
volumes = getVolumes()
observe({
shinyFileChoose(input, "Btn_GetFile", roots = volumes, session = session)
if (!is.null(input$Btn_GetFile)){
file_selected <- parseFilePaths(volumes, input$Btn_GetFile)
drive_upload(media = as.character(file_selected$datapath),
name = as.character(file_selected$name))
}
})
}
shinyApp(ui = ui, server = server)
我几乎完成了使用 Rshiny 创建的调查。我想创建一个按钮,允许用户将图像(jpeg、tiff、png 等)上传到 R shiny 服务器、Dropbox 或 google 驱动器。 fileInput 方法似乎只接受像 csv 这样的文件?任何帮助,将不胜感激!
fileInput
实际上允许导入任何类型的文件。您只需将参数 accept
设置为 NULL
或您接受的任何文件扩展名。你首先需要明白的是,如果你使用fileInput
,它实际上会将你的文件上传到tmp文件夹(上传数据的路径存储在input$file$datapath
中),然后你才会能够将您的文件上传到 googledrive 或任何云。使用 fileInput
的解决方案可能如下所示:
library(googledrive)
ui <- fluidPage(
fileInput(inputId = "file",
label = "Choose file to upload",
accept = NULL)
)
server <- function(input, output) {
observeEvent(input$file, {
drive_upload(media = input$file$datapath,
name = input$file$name)
})
}
shinyApp(ui, server)
如果 "double upload" 对您来说是个问题,您可以使用包 shinyFiles
来避免这个问题。已经发布了一个很好的答案
library(googledrive)
library(shinyFiles)
ui <- fluidPage(
shinyFilesButton("Btn_GetFile", "Choose file to upload" ,
title = "Please select a file:", multiple = FALSE,
buttonType = "default", class = NULL)
)
server <- function(input, output, session) {
volumes = getVolumes()
observe({
shinyFileChoose(input, "Btn_GetFile", roots = volumes, session = session)
if (!is.null(input$Btn_GetFile)){
file_selected <- parseFilePaths(volumes, input$Btn_GetFile)
drive_upload(media = as.character(file_selected$datapath),
name = as.character(file_selected$name))
}
})
}
shinyApp(ui = ui, server = server)