androidq获取文件正式路径_?R语言tip | 方便快捷的获取目标文件路径: rstudioapi与here包的应用小结...

本文总结了在R语言中如何便捷地获取文件路径,包括使用rstudioapi包获取正编辑的R script路径、选择文件夹或文件、保存文件的路径,以及通过here包在项目文件夹中获取路径。同时介绍了system.file()函数在查找R包内文件路径的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

b70b070ace223c6fb7a78eb6e9df3ae8.png

专注服务懒得打斜杠,懒得找路径的懒狗们。。。

部分内容总结自

Getting path of an R script​stackoverflow.com
781fe660b2563daa544060de0e4335ac.png

目录

如何更快捷的得到目的路径?

- 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()

运行后即会弹出对话框:

8275e36a590839f343baca773e3daf9b.png

选好文件夹后点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:

56bf6a2cb85f3b3d018f3bdf5e549bdd.png

选定后可得路径,然后用该路径进行存储等操作。

> 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],仅此而已。

举个例子,两个内容完全相同的文件分别位于不同盘的不同文件夹下面:

7ce8045fbae9eac826b089a669b8cb45.png

6a184ee3f8166f5860079a401da7d1bf.png

目的是把mtcars存到同文件夹下的"data"子文件夹中。

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

0666e3288390d7ac179bab6caa3a4d97.png

然而,假如你同时在不同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"

参考

  1. ^rstudioapi https://rstudio.github.io/rstudioapi/index.html
  2. ^rstudioapi: File Dialogs https://rstudio.github.io/rstudioapi/articles/dialogs.html
  3. ^Project-oriented workflow https://www.tidyverse.org/blog/2017/12/workflow-vs-script/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值