r - 如何使用 R 以编程方式定位我的 Google Drive 文件夹?
问题描述
这与此处的问题相同,但我需要在 R 中执行此操作。
当您需要访问 google drive 文件夹中的文件时,在计算机上找到 google drive 文件夹很有用,但是这个 google driver 文件夹在您的不同计算机上具有不同的路径,这使得脚本共享变得困难。
我还没有找到关于如何在 R 中做到这一点的解决方案,所以我认为对于有同样问题的其他人来说,在这里打开一个线程可能很有用。我已经根据上面的链接为 R 准备了一个解决方案。
我很乐意阅读比我将在此处介绍的解决方案更好/更简单的解决方案。
此外,我的解决方案可能仅适用于 Windows 用户,因此欢迎使用其他操作系统的解决方案或更通用的跨操作系统解决方案。
解决方案
这个想法是使用库访问谷歌驱动器文件数据库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"
推荐阅读
- istio - Istio-Service 到服务的通信没有按预期进行
- javascript - 如何将此代码从 Jquery 转换为 Vanilla Javascript?
- java - 如何减少 Docker 中的 Spring Boot 应用程序内存使用量?
- c# - 使用 nhibernate 将两个表映射到一个对象
- asp.net-mvc - 身份无法连接到远程数据库,继续连接到本地数据库
- pyspark - 计算日期在每个 ID 的日期范围内的行数
- javascript - 如何隐藏未选中的表单的输入字段并仅显示已选中的输入字段
- c# - 显示 RAZOR 页面而不是 JSON 格式(C#、WebService)
- python - 结帐或下订单后如何清空购物车?Django
- ios - 通过 Jenkins 执行 shell 获取 CFBundleShortVersionString 版本值