何时使用“source()”或“attach()”
我的项目目录结构的一部分如下所示:
\projects\project\main.R
\projects\project\src
其中 \src
包含一堆每个文件一个函数、特定于项目的函数。
问:将这些函数添加到工作目录 projects\project
的最佳实践方法是什么?
我看到了一些解决方案:
attach("./src")
。我试图避免这种情况,因为 (1) Google 样式指南建议避免使用attach()
以及 (2) 我收到警告消息:
1:从 Windows 上的 Splus 读取 Unix 风格数据库目录 (./tmp):可以 查找某些数据集时遇到问题,尤其是那些名称 仅大小写不同(文件 tmp-script1.ssc 不应由 Windows 上的 Splus)位于:exists(name, where = db)
执行此操作时。lapply(paste("./src/",list.files("./src/"),sep=""),source)
。这工作得很好,只是看起来很笨重。一定有更好的方法,对吧?通过全名
./src/myfunc
引用我的函数。这很快就会变得丑陋。我确信有更好的方法。删除目录中的
接近的目录结构,./src
部分,并将所有函数扔到主工作目录中。这样做的问题是,我更愿意保留与 John Myles White 的ProjectTemplate
将所有函数放入一个文件
./src /func.R
并获取它。我想这种方法避免了“2”的丑陋。上面,但我真的很想每个文件都有一个函数。这样看起来更干净。
Part of my project directory structure looks like:
\projects\project\main.R
\projects\project\src
where \src
contains a bunch of 1-function-per-file, project-specific functions.
Q: What's the best practice way to add these functions to the working directory projects\project
?
There are a few solutions I see:
attach("./src")
. I'm trying to avoid this because (1) the Google Styleguide recommends avoiding the use ofattach()
and (2) I receive theWarning messages:
1: Reading Unix style database directory (./tmp) from Splus on Windows: may
have problems finding some datasets, especially those whose names
differ only by case (file tmp-script1.ssc should not have been made by
Splus on Windows) in: exists(name, where = db)
when doing this.lapply(paste("./src/",list.files("./src/"),sep=""),source)
. This works perfectly fine, it just seems clunky. There has to be a better way, right?Refer to my functions by their full name
./src/myfunc
. This will get ugly very quick. I'm sure there's a better way.Get rid of the
./src
part of my directory and just throw all the functions in the main working directory. The problem with this is that I'd prefer to keep with a directory structure that is close to that of John Myles White'sProjectTemplate
Throw all the functions in one file,
./src/func.R
and source that. I guess this approach avoids the ugliness of "2." above, but I'd really like to have one function per file. Just seems cleaner that way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
编辑
或
Try
EDIT
or
如果您不想将所有内容都放入本地包中,那么我会选择选项 2。
If you don't want to put everything into a local package, then I'd go for option 2.