【问题标题】:How do I download and save multiple files from google drive to a particular folder on my laptop using R and googledrive package如何使用 R 和 googledrive 包从 google drive 下载多个文件并将其保存到笔记本电脑上的特定文件夹
【发布时间】:2021-08-20 20:28:38
【问题描述】:

我正在按照this 帖子中的说明将 2 个 csv 文件从我的谷歌驱动器下载到我的计算机。这是提供的代码-

library(googledrive)
library(purrr)

## store the URL you have
folder_url <- "https://drive.google.com/drive/folders/0B7tJg2i5HAo2c0VzVFVhLUdQcnM"

## identify this folder on Drive
## let googledrive know this is a file ID or URL, as opposed to file name
folder <- drive_get(as_id(folder_url))

## identify the csv files in that folder
csv_files <- drive_ls(folder, type = "csv")

## download them
walk(csv_files$id, ~ drive_download(as_id(.x)))

上述说明将 csv 文件下载到我的“文档”文件夹。我正在尝试将文件下载到我笔记本电脑上的特定文件夹,只需对最后一段代码进行轻微修改

walk(csv_files$id, ~ drive_download(as_id(.x),
                                    path = "../Desktop/data_folder/,
                                    overwrite = TRUE))

不幸的是,这会保存一个不包含任何数据且无法打开的 .xlsx 文件。如何更正代码以将两个文件保存到特定文件夹?

【问题讨论】:

    标签: r purrr


    【解决方案1】:

    问题在于path 参数需要完整的文件名,而不仅仅是目录的路径。因此,默认情况下,它使用 goodledrive 方法计算文件名,并使用它在您的计算机上的当前工作目录中创建一个文件。但是,如果您不想要这种默认行为,您还应该提供文件名。所以基本上有两种选择:

    1. 在运行您提供的代码之前,将您的工作目录设置为您希望将文件下载到的文件夹。 (然后很可能将其改回来是个好主意)

    2. 重写脚本,以便使用 googledrive 文件名构建 path 参数。像这样的:

    path <- "~/Documents/tmp/data_folder/"
    for (i in seq_along(csv_files$name)) {
      drive_download(
        as_id(csv_files$id[[i]]),
        path = file.path(path, csv_files$name[[i]]),
        overwrite = TRUE
      )
    }
    

    或者,如果您更喜欢使用walk

    csv_files %>% 
      split(csv_files$id) %>% 
      walk(~drive_download(.$id, path = file.path(path, .$name), overwrite = TRUE))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-31
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      • 2013-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多