
专注服务懒得打斜杠,懒得找路径的懒狗们。。。
部分内容总结自
Getting path of an R scriptstackoverflow.com
目录
如何更快捷的得到目的路径?
- scenario 1: 获取正编辑的R script路径 - rstudioapi包
- scenario 2: 获取其他路径 - rstudioapi包
- scenario 3: 获取正编辑的R script路径 - here包
- scenario 4: 目的路径在某个R包中 - system.file()
Scenario 1: 获取正编辑的R script路径 - rstudioapi包[1]
想象一下我们准备把跑好的数据存到和正编辑的R script同一个文件夹下。而当前默认working directory和该文件夹远了去了?
#简单但是枯燥:
#step1: 打开"我的电脑"
#step2: 找到目的文件夹
#step3: 复制路径
#step4: setwd("你复制的路径")
#step5: 改斜杠,或者用file.path()
省去以上步骤最简单的方法:
# install.packages("rstudioapi")
setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
例如我正在编辑test.R文件,getActiveDocumentContext() 会得到当前Rstudio editor中正在编辑的文件信息:
> getActiveDocumentContext()
Document Context:
- id: 'FD80D66A'
- path: 'C:/Users/MSI-NB/Desktop/test.R'
- contents: <2 rows>
Document Selection:
- [1, 13] -- [1, 39]: 'getActiveDocumentContext() <...>'
获取文件full path
> getActiveDocumentContext()$path
[1] "C:/Users/MSI-NB/Desktop/test.R"
最后用dirname()提取directory path即可
> dirname(getActiveDocumentContext()$path)
[1] "C:/Users/MSI-NB/Desktop"
顺便介绍一下rstudioapi:
The rstudioapi package is designed to make it easy to conditionally access the RStudio API from CRAN packages, avoiding any potential problems with R CMD check. This package contains a handful of useful wrapper functions to access the API. To see the functions that are currently available in the API, run help(package = "rstudioapi")
通过rstudioapi,可以与R project/RStudio Terminal/R session进行交互,还可以编辑文档、获取路径(见Part 1 Scenario 2)等等,详细介绍可以见以下参考文章部分的链接。
Scenario 2: 获取其他路径 - rstudioapi包[2]
a-选文件夹:
这里用到的是rstudioapi包的交互功能。假设我们想得到一个文件夹路径:
target.dir <- rstudioapi::selectDirectory()
运行后即会弹出对话框:

选好文件夹后点select,即可得到选定路径:
> target.dir
[1] "C:/Users/MSI-NB/Desktop"
b-选文件
与a类似。注意filter可以筛选文件类型
target.path <- rstudioapi::selectFile(filter = "All Files (*)")
#选取文件后可得对应文件路径:
> target.path
[1] "C:/Users/MSI-NB/Desktop/test.R"
c-存文件
用到的仍是selectFile(),但是要注明existing = FALSE,因为文件目前还不存在。
假设要把某个object存入桌面的test文件夹:
save.path <- rstudioapi::selectFile(existing = FALSE)
运行后弹出对话框,命名为testfile:

选定后可得路径,然后用该路径进行存储等操作。
> save.path
[1] "C:/Users/MSI-NB/Desktop/test/testfile.rds"
saveRDS(data, file = save.path)
Scenario 3: 获取正编辑的R script路径 - here包
file.path()一个不方便的地方在于,如果想要设置的路径和当前环境工作路径不一致的话,要手动加入完整地址。而用here包会直接给你完整路径:
library(here)
> here("dummyDir","dummyFile")
[1] "C:/Users/MSI-NB/Desktop/test/dummyDir/dummyFile"
!注意:看到这里你可能会问,R是怎么知道路径的前半部分是"C:/Users/MSI-NB/Desktop/test”的?难道要setwd()?
非也,因为如果用了setwd(),Rcode到了别人手里,其他人还要手动改路径,这对于多人合作的项目来说非常不便。
想要顺滑使用here,只需要到R script/R project所在文件夹启动R session[3],仅此而已。
举个例子,两个内容完全相同的文件分别位于不同盘的不同文件夹下面:


目的是把mtcars存到同文件夹下的"data"子文件夹中。
我们自然是希望当工作在rcode1中时得到的path只是D:dirD,而rcode2只对应E:dirEdirE_sub,普通做法setwd()手动改路径过于麻烦,而here只需要以下代码,无需任何手动操作:

然而,假如你同时在不同working directory的文件上工作,要注意here只能指定一次,如果要改的话需要重启R session.
Once established, the root directory doesn't change during the active R session. here() then appends the arguments to the root directory.
举个例子:
到D:dirD中打开rcode1,启动R session,此时path= "D:/dirD/data/mtcars.rds"。如果这时切到或是打开E:/dirE/dirE_sub下面的rcode1,mtcars并不会存到E盘下的data folder中,因为here()之后path仍然是"D:/dirD/data/mtcars.rds"
必须要关掉R,到E:/dirE/dirE_sub中打开rcode1启动R session,此时path才会变成"E:/dirE/dirE_sub/mtcars.rds"。
Scenario 4: 目的路径在某个R包中
直接用system.file()获取。例如我们想得到MASS.rdb的路径,又知道它位于子文件夹R之下:
> system.file("R", "MASS.rdb", package="MASS")
[1] "C:/Program Files/R/R-4.0.2/library/MASS/R/MASS.rdb"
参考
- ^rstudioapi https://rstudio.github.io/rstudioapi/index.html
- ^rstudioapi: File Dialogs https://rstudio.github.io/rstudioapi/articles/dialogs.html
- ^Project-oriented workflow https://www.tidyverse.org/blog/2017/12/workflow-vs-script/