简体   繁体   English

我如何获得R中的当前文件目录

[英]how can i get the current file directory in R

I have seen many related answers here,but i didn't get a proper way to solve my problem under windows system... I know the link the similar question I got that setwd() can locate the directory what i want,however,my R script may move to another directory without any modification,so I want to know the current file directory,becase there are expression like source(...) ,this called source file and the execution file under the same parent directory in a R project,how I can do? 我在这里看到了许多相关的答案,但是我没有在Windows系统下解决问题的正确方法。我知道类似的问题,我得到的链接是setwd()可以找到我想要的目录,但是,我的R脚本可能未经修改就移动到了另一个目录,所以我想知道当前文件目录,因为在R的同一父目录下有诸如source(...)这样的表达式,这称为源文件和执行文件。项目,我该怎么办? any help appreciated. 任何帮助表示赞赏。

You can get your current directory using the getwd() function and give it a name, say: 您可以使用getwd()函数获取当前目录并为其命名,例如:

cpath = getwd()

Another useful function is the file.path , which can help you specify new directories with simple syntax. 另一个有用的功能是file.path ,它可以帮助您使用简单的语法指定新目录。 For example, you want to get the directory that is one level "above" the current directory, you can use: 例如,要获取当前目录“上”一层的目录,可以使用:

upp.dir = file.path("..", "cpath")

This gives upp.dir as "../Your_Current_Dir" . 这给出upp.dir"../Your_Current_Dir" How about changing to another folder (called Folder_A) in current directory? 如何更改到当前目录中的另一个文件夹(称为Folder_A)? Use: 采用:

folderA = file.path("cpath", "Folder_A")

These may help easy navigate the file system. 这些可能有助于轻松浏览文件系统。

Basically, if you write scripts and those scripts depend on where they are, then you are Doing It Wrong. 基本上,如果您编写脚本并且这些脚本取决于脚本的位置,那么您做错了。

Write code in packages. 用程序包编写代码。 Parameterise functions to make them generally applicable. 参数化功能使其普遍适用。 If you have folders with data in, then make one of those parameters a folder. 如果您的文件夹中有数据,则将其中一个参数设置为文件夹。

A script called with source() cannot reliably locate itself, but that shouldn't be a problem, because WHATEVER CALLED THE SCRIPT knows where the script is (it has to, or how else can it call it?) so it could pass that as a parameter. 调用脚本source()不能可靠地定位自己,但是这不应该是一个问题,因为无论调用的脚本知道在哪里的脚本(它,或者它还能怎么称呼呢?),所以它可以传递作为参数。 Something like: 就像是:

 > youarehere = "C:\foo\"
 > source("C:\foo\bar.R")

and now bar.R can do setwd(youarehere) and it will work, even if it is badly written such that it relies on sourcing other code in its containing folder. 现在bar.R可以执行setwd(youarehere) ,即使它写得很差,它依赖于在其包含文件夹中获取其他代码,它也可以工作。

Or you can do: 或者,您可以执行以下操作:

> setwd(youarehere)
> source("bar.R")

in your calling function. 在您的调用函数中。

But really, its a fail, its a sign of badly written code. 但是实际上,这是一个失败,这是代码编写错误的标志。 Use functions, write packages, use devtools, its really not that hard, then your code will work anywhere and you wont be writing stupid scripts that are a twisty turny maze of source() calls. 使用函数,编写程序包,使用devtools,实际上并不是那么难,那么您的代码将可以在任何地方使用,并且您不会编写愚蠢的脚本,这些脚本是对source()调用的曲折迷宫。

Stay classy. 保持优雅。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM