首页 > 解决方案 > 为什么 R 中的 googledrive 库识别多个文件,而 Drive 文件夹中只有一个文件?

问题描述

我正在尝试使用googledrive库从我的 Google Drive 文件夹(通过 Google Earth Engine 导出到它)中下载一个 .tif 文件。但是,在调用该map函数时,出现以下错误:

Error: 'file' identifies more than one Drive file.

我已经设法使用此代码下载了其他 .tif 文件,该代码可以正常工作。为什么我会收到此错误,我该如何解决?正如您在云端硬盘文件夹(它是公开的)中看到的,该文件夹仅包含一个文件,那么为什么“文件”会标识多个云端硬盘文件?

代码:

library(googledrive)
library(purrr)

## Store the URL to the folder 
folder_url <- "https://drive.google.com/drive/folders/1Qdp0GN7_BZoU70OrpbEL-vIBBxBa1_Db"

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

## Identify files in the folder
files <- drive_ls(folder, pattern = "*.tif")

# Download all files in folder
map(files$name, overwrite = T, drive_download)

标签: rgoogle-drive-apitiffgoogle-earth-engine

解决方案


Google Drive API 的方法Files: list默认返回一个数组

即使结果只包含一个文件,或者根本没有文件 - 它仍然是一个数组。

您需要做的就是检索0该数组的第一个 ( ) 元素。您可以通过使用Try this API进行测试来验证它。

我希望正确的语法R类似于 files[0]$name检索第一个(即使它是唯一的)文件的名称。

另外:您应该在检索文件名之前执行一些条件语句以验证文件列表不为空。


推荐阅读