[英]How can I automatically load and attach imported functions on project open in package dev?
When I'm opening a R package project on my computer, I'd like to automatically load and attach library()
all the packages which are listed in the respective NAMESPACE
of the given package as import()
s (not importFrom()
s). 当我在我的电脑上打开A R 包装项目,我想自动加载并连接
library()
所有这一切都在各自列出的软件包NAMESPACE
给定的包作为import()
S(未importFrom()
小号)。
This is helpful, because it allows me to more quickly interactively evaluate code. 这很有用,因为它使我可以更快地交互式评估代码。 Otherwise, I waste time with dealing, with, say,
否则,我会浪费时间处理诸如
glue("foo")
# Error in glue("foo") : could not find function "glue"
even though I have glue in my NAMESPACE
. 即使我的
NAMESPACE
有胶水。
Not a huge thing, bit shaves off some seconds and quite a bit of frustration. 这不是什么大不了的事,几秒钟就刮光了一些挫败感。
Please notice that this only applies in the context of package development. 请注意,这仅适用于程序包开发。 In non-package projects, I can just place
library()
into my .Rprofile
, but this would be redundant in the case of packages, where said packages are already declared in NAMESPACE
. 在非包装项目,我可以只需将
library()
到我.Rprofile
,但这将是在封装,其中所述包中已经声明的情况下,多余的NAMESPACE
。
My approach would be to add the following to the .Rprofile
at my project (and package) root: 我的方法是在项目(和程序包)根目录下的
.Rprofile
中添加以下内容:
if (Sys.getenv()["LOGNAME"] %in% c("foo_user", "bar_user")) {
message("I seem to be running on a dev machine, so I am doing some preparatory steps.")
imports <- devtools::parse_ns_file()$imports # capture all imports from namespace file
imports <- purrr::discard(.x = imports, .p = is.list) # only take the import(), not the importFrom() declarations.
purrr::walk(.x = imports, .f = library, character.only = TRUE)
}
Does this seem like an ok way to go about it? 这似乎是可行的方法吗?
Update (already included in the above) 更新 (已包含在上面)
It seems that some CI systems (Appveyor) don't like devtools::...
) and other calls in .Rprofile
, perhaps because they don't have this and other packages available at whatever stage in the build cycle .Rprofile
is called (pretty early?). 这似乎有些CI系统(Appveyor)不喜欢
devtools::...
)和其他呼叫.Rprofile
,或许是因为他们没有这个套装等可在任何阶段的建造周期.Rprofile
被称为(还早吗?)。 So it might seem wise to wrap the whole shebang in an if clause to call it only on the local dev machines. 因此,将整个shebang包裹在if子句中以仅在本地dev机器上调用它似乎是明智的。
On macOS, Sys.getenv()["LOGNAME"]
gives the short user name, so I'm using that to identify my dev machine. 在macOS上,
Sys.getenv()["LOGNAME"]
给出了简短的用户名,因此我使用它来标识我的开发机器。
Let me know if anyone has better ideas. 让我知道是否有人有更好的主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.