R:source()和源文件的路径 [英] R: source() and path to source files

查看:2318
本文介绍了R:source()和源文件的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

必须有一些我不明白在R中的 source()命令。我仍然对它不熟悉,但是我不能我明白它是如何得到它的目录!我的问题是这样的:



我有一个包装脚本, wrapper.R 和一个包含一些函数的源文件, functions.R 。这两个都在同一个目录中。如果我在包装器脚本中调用 source('functions.R'),而站在两个文件所在的目录内,一切都很好。但是,我希望能够从某个其他目录运行我的 wrapper.R 脚本, 不是那个这些脚本所在的位置。如果我为其他目录运行我的包装,它不起作用,并且我得到一个无法打开文件错误。



我使用Google搜索并找到很多不同的主题,但这个问题似乎很清楚。我理解它的方式,我做它的方式应该可以工作。显然,我误解了一些东西。我对该线程的阅读让我相信 source()可用于调用 source()位于。我的阅读也让我相信我不应该使用 chdir = TRUE ,因为我想保留广告的相对目录。



看到它不起作用...我误解了什么?如何从我的包装器脚本获取与我的包装器脚本相同的目录中的文件? 解决方案

将脚本分发给同事,你应该不会编写一个脚本来获取其他脚本。如果你想在未来重命名或移动 functions.R ?如果您需要修改 functions.R 中的函数,但是 wrapper.R 则依赖于旧版本功能?这是一个会导致头痛的脆弱解决方案。我会推荐以下任何一种。


  1. 将所有需要的东西放到一个单独的自包含脚本中并分发。

  2. 如果您确实想将代码分离到不同的文件中,请编写一个包。可能听起来像是矫枉过正,但包装实际上可以非常简单和轻便。最简单的形式是一个包含描述 NAMESPACE 文件的目录以及 R / 目录。哈德利很好地解决了这个问题: http://r-pkgs.had.co.nz/


There must be something that I don't understand about the source() command in R. I'm still new to it, but I cannot for the life of me understand how it gets its directories from! My problem is this:

I have a wrapper script, wrapper.R, and a source file containing some functions, functions.R. Both of these are in the same directory. If I call source('functions.R') inside the wrapper script, while standing inside the directory where both files are located, everything is fine. However, I want to be able to run my wrapper.R script from some other directory, i.e. not the one where these script are located. If I run my wrapper for another directory, it doesn't work, and I get a cannot open the file error.

I googled and found lots of different threads, but this question seemed to be very clear. The way I understand it, the way I'm doing it should work. Clearly, I'm misunderstanding something. My reading of that thread leads me to believe that source() works on the directory in which the file that calls source() is located in. My reading also leads me to believe that I should not be using chdir = TRUE, as I want to keep the advertised relative directory.

Seeing as it doesn't work... what am I misunderstanding? How can I source files in the same directory as my wrapper script when called from somewhere else?

解决方案

If you are distributing a script to colleagues, you should really not be writing a script that sources other scripts. What if you want to rename or move functions.R in the future? What if you need to modify a function in functions.R, but wrapper.R relies on the older version of that function? It's a flimsy solution that will cause headache. I would recommend either of the following instead.

  1. Put everything needed into a single, self-contained script and distribute that.

  2. If you really want to separate code into different files, write a package. Might sound like overkill, but packages can actually be very simple and lightweight. In the simplest form a package is just a directory with a DESCRIPTION and NAMESPACE file along with an R/ directory. Hadley breaks this down nicely: http://r-pkgs.had.co.nz/.

这篇关于R:source()和源文件的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆