对于工作目录设置为源文件的位置R指令 [英] R command for setting working directory to source file location
问题描述
我的工作出R.一些教程各R code被包含在一个特定的文件夹。有在那里的数据文件和其他文件。我想打开 .R
文件和源是这样的,我没有改变Rstudio工作目录,如下所示:
I am working out some tutorials in R. Each R code is contained in a specific folder. There are data files and other files in there. I want to open the .r
file and source it such that I do not have to change the working directory in Rstudio as shown below:
有没有R中自动指定我的工作目录的方式。
Is there a way to specify my working directory automatically in R.
推荐答案
我知道这个问题已经过时,但是我正在寻找的一个解决方案,以及和谷歌在列出了顶部:
I know this question is outdated, but I was searching for a solution for that as well and Google lists this at the very top:
this.dir <- dirname(parent.frame(2)$ofile)
setwd(this.dir)
把某处到文件(最好是开始时,虽然),从而使WD变为根据该文件
put that somewhere into the file (best would be the beginning, though), so that the wd is changed according to that file.
这篇关于对于工作目录设置为源文件的位置R指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!