r - 为什么 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)
解决方案
Google Drive API 的方法Files: list
默认返回一个数组
即使结果只包含一个文件,或者根本没有文件 - 它仍然是一个数组。
您需要做的就是检索0
该数组的第一个 ( ) 元素。您可以通过使用Try this API进行测试来验证它。
我希望正确的语法R
类似于
files[0]$name
检索第一个(即使它是唯一的)文件的名称。
另外:您应该在检索文件名之前执行一些条件语句以验证文件列表不为空。
推荐阅读
- python-3.x - 如何在 Python 中读取 CSV 文件,同时仍保持单一责任原则?
- python - PyQt5 QWebEngineView - 从文件中制作 Markdown - Python
- xml - xslt:排序、比较和返回值
- python - 无法使用 GPU 来拟合使用 Keras 的模型
- tensorboard - 平滑曲线始终低于 Tensorboard 上的非平滑曲线
- python - 在 PySpark 中使用正则表达式创建 N-Gram
- swift - SwiftUI - 致命错误:从数组中删除元素时索引超出范围
- assembly - 为什么我不能在 Assembly 中声明一个采用取消引用变量的值的变量?
- ruby-on-rails - 使用 devise 和 omniauth-facebook 时,为什么 facebook 显示“提供的应用 ID 看起来不像有效的应用 ID。”?
- ffmpeg - Apple Automator、Shell 脚本和批处理文件 - 视频字幕