简体   繁体   English

如何设置“ path.expand”从我的工作目录开始?

[英]How can I set `path.expand` to begin at my working directory?

I'm using a Mac. 我正在使用Mac。 The path.expand function is several folders removed from my desired working directory. path.expand函数是从我所需的工作目录中删除的几个文件夹。 For example: 例如:

path.expand ('~') path.expand ('〜')

[1] "/Users/my.name" [1]“ /Users/my.name”

I'd like to change it to something like this: 我想将其更改为以下内容:

path.expand ('~') path.expand ('〜')

[1] "/Users/my.name/drive/R/project/sub.folder" [1]“ /用户/我的名称/驱动器/ R /项目/子文件夹”

How can I go about this? 我该怎么办?

Thank you. 谢谢。

The tilde is, in all unix-sen (including macos), special in that it refers to what the operating system considers the home directory (via the env var HOME ). 在所有unix-sen(包括macOS)中,代字号都特别之处在于它是指操作系统认为的主目录(通过env var HOME )。

There are two types of answers to this. 有两种类型的答案。 Can it be done? 能做到吗 Perhaps, sure even. 也许,甚至可以肯定。 Should it be done? 应该做吗? There will likely be unintended consequences (that may be hard to troubleshoot and/or workaround), so likely not. 可能会出现意想不到的后果(可能难以解决和/或解决),因此可能不会。

This works on my ubuntu box: 这适用于我的Ubuntu盒子:

me@mybox:/some/path$ Rscript -e 'Sys.getenv("HOME")'
[1] "/home/me"
me@mybox:/some/path$ HOME=/tmp/ Rscript -e 'Sys.getenv("HOME")'
[1] "/tmp/"
me@mybox:/some/path$ Rscript -e 'Sys.setenv(HOME="/tmp/");Sys.getenv("HOME")'
[1] "/tmp/"

(This notably does not work as well on Windows ... which is not very unix-y of it!) (这在Windows上显然效果不佳...这不是unix-y!)

So you can try overriding it with either: 因此,您可以尝试使用以下任一方法覆盖它:

  1. Sys.setenv(HOME = "/Users/my.name/drive/R/project/sub.folder") , or Sys.setenv(HOME = "/Users/my.name/drive/R/project/sub.folder") ,或
  2. Set the HOME variable in your working environment before starting R. 在启动R之前,请在工作环境中设置HOME变量。

This might have unintended consequences. 这可能会产生意想不到的后果。 For instance, R looks for ~/.Rprofile , and git and commands look for ~/.gitconfig and such. 例如,R查找~/.Rprofile ,而git和命令查找~/.gitconfig等。

My recommended way-ahead would be to define a variable and change there. 我建议的超前方法是定义一个变量并在那里进行更改。 If you use RStudio, then its "Projects" can always start you in the correct directory. 如果使用RStudio,则其“项目”始终可以在正确的目录中启动。 If not and you still want this "special directory" available to you, perhaps add this to your /Users/username/.Rprofile (in your "actual" homedir) 如果没有,并且您仍然希望此“特殊目录”可用,则可以将其添加到您的/Users/username/.Rprofile (在“实际” homedir中)

.specialdir <- "/Users/my.name/drive/R/project/sub.folder"

and, whenever you need to go there, use file.expand(.specialdir) . 并且,每当您需要去那里时,请使用file.expand(.specialdir) One side-effect of this is that any of your code, functions, reports, whatever that use this will no longer be reproducible. 这样做的副作用是,您的任何代码,函数,报告,无论使用该代码的任何内容,都将不再具有重现性。

A way to easily reference your files without needing to change the HOME directory is to use the here package. 无需更改HOME目录即可轻松引用文件的一种方法是使用here包。 This basically uses a heuristic to find the right working directory based on where your script is. 这基本上是使用启发式方法根据脚本的位置找到正确的工作目录。 Normally it looks for RStudio Project files ( .rproj ) or for a .git file if your working directory is a git repository. 通常,它会查找RStudio Project文件( .rproj )或.git文件(如果您的工作目录是git存储库)。 It's easy to use and robust to moving machines or accidental use of setwd , or even forgetting to set HOME on a different machine/profile. 它易于使用,对于移动的机器或意外使用setwd ,甚至忘记在其他机器/配置文件上设置HOME。

If your data file some_data.csv above is stored in /Users/my.name/drive/R/project/sub.folder/some_data.csv , where project is the root folder for the project: 如果上面的数据文件some_data.csv存储在/Users/my.name/drive/R/project/sub.folder/some_data.csv ,其中projectproject的根文件夹:

here::here()
[1] "/Users/my.name/drive/R/project"
here::here("sub.folder", "some_data.csv")
[1] "/Users/my.name/drive/R/project/sub.folder/some_data.csv"

and you can use it as a drop in replacement for the path, as in: 您可以将其用作路径的替代品,例如:

data <- read_csv(here::here("sub.folder", "some_data.csv"))

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

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