如何使用 R 以编程方式定位我的 Google Drive 文件夹?

How do I programmatically locate my Google Drive folder using R?

这是与 here 相同的问题,只是我需要在 R 中这样做。

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

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

我很乐意阅读 better/simpler 解决方案,而不是我将在此处展示的解决方案。

此外,我的解决方案可能仅适用于 Windows 用户,因此欢迎提供适用于其他 OS 的解决方案或更通用的交叉 OS 解决方案。

想法是使用 RSQLite 库访问 google 驱动器文件数据库,并执行查询 return google 驱动器的根路径文件夹。以下代码执行此操作:

# 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"