【问题标题】:have Rshiny app user upload image to dropbox /shiny server / google drive让 Rshiny 应用程序用户将图像上传到保管箱/闪亮服务器/谷歌驱动器
【发布时间】:2018-08-10 08:01:45
【问题描述】:

我几乎完成了使用 Rshiny 创建调查。我想创建一个按钮,允许用户将图像(jpeg、tiff、png 等)上传到 R 闪亮服务器、Dropbox 或谷歌驱动器。 fileInput 方法似乎只接受 csv 之类的文档?任何帮助,将不胜感激!

【问题讨论】:

    标签: r shiny shinyjs


    【解决方案1】:

    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)
    

    如果“双重上传”对您来说是个问题,您可以使用包shinyFiles 来避免这种情况。 here 已发布了一个很好的答案,这是一种调整代码以解决您的特定问题的方法。

    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)
    

    【讨论】:

    • 感谢您的回复!在您提供的 shinyFiles 解决方案中,文件上传到哪里?我可以让它上传到应用程序代码中我想要的任何云吗?
    • 这个闪亮的应用程序适用于 Google Drive。当您第一次运行闪亮的应用程序时,它会打开浏览器并要求您对 Google Drive 进行身份验证。使用前面的代码会将您的文件上传到 Google Drive 存储库的主目录。如果你想修改这个,你应该在drive_upload函数中指定path参数,默认为NULL。
    • 效果很好,谢谢!最后一个问题:当新用户在不同的机器上使用应用程序时,oauth 令牌是否保存在应用程序或闪亮的服务器中?
    猜你喜欢
    • 2021-07-02
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多