首页 > 解决方案 > 如何使用 R 以编程方式定位我的 Google Drive 文件夹?

问题描述

这与此处的问题相同,但我需要在 R 中执行此操作。

当您需要访问 google drive 文件夹中的文件时,在计算机上找到 google drive 文件夹很有用,但是这个 google driver 文件夹在您的不同计算机上具有不同的路径,这使得脚本共享变得困难。

我还没有找到关于如何在 R 中做到这一点的解决方案,所以我认为对于有同样问题的其他人来说,在这里打开一个线程可能很有用。我已经根据上面的链接为 R 准备了一个解决方案。

我很乐意阅读比我将在此处介绍的解决方案更好/更简单的解决方案。

此外,我的解决方案可能仅适用于 Windows 用户,因此欢迎使用其他操作系统的解决方案或更通用的跨操作系统解决方案。

标签: rgoogle-drive-api

解决方案


这个想法是使用库访问谷歌驱动器文件数据库RSQLite,并执行一个查询,该查询将返回谷歌驱动器文件夹的根路径。以下代码执行此操作:

# Get the path to the google drive database
googledrivedb <- paste0(Sys.getenv("LOCALAPPDATA"), "/Google\\Drive\\user_default\\sync_config.db")
# Correct backslashes to slashes so that R can interact with the folders
googledrivedb <- gsub("\\\\", "/", googledrivedb)

# Load SQL interface libraries
library(DBI)
library(RSQLite)

# Connect to the google drive file database
con <-dbConnect(RSQLite::SQLite(), dbname = googledrivedb)

# Make a query which will return the path to the root folder of google drive
path <- dbGetQuery(con, "select * from data where entry_key='local_sync_root_path'")$data_value

# Remove unnecessary characters in the path (I had "\\\?\" in my path - I don't know how it will look like on other computers)
path <- substring(path,
                  5)
# Correct backslashes to slashes so that R can interact with the folders
path <- gsub("\\\\", "/", path)
# Close the connection to the database
dbDisconnect(con)

# Show the obtained path
path

就我而言,结果是: "C:/Google Drive"


推荐阅读