英文:
`R` equivalent to pythons `from src.file import function, class, variable`?
问题
在R中,你可以使用import
或importFrom
函数从其他脚本中导入所需的对象,而不必使用source()
导入整个脚本。这将帮助你避免导入不需要的函数、对象和变量。以下是如何在R中实现类似的功能:
# 在依赖的脚本中使用import或importFrom函数
# 这些函数来自于'roxygen2'包,所以确保你已经安装了该包
#' @importFrom yourPackageName class1 class2
# 或者
#' @import yourPackageName
# 你的脚本内容
setClass("c", slots = c(name = "character"))
# 如果需要从其他脚本中导入类,可以使用import或importFrom函数
在你的脚本中,使用@importFrom
标签或@import
标签指定你想导入的类或包,而不必使用source()
来导入整个脚本。这将使你的代码更模块化,并且不会导入不需要的函数、对象和变量。确保替换yourPackageName
为你的R包的名称,以及class1
和class2
为你想要导入的类的名称。
英文:
I have defined three different S4 classes in their own scripts in my R package. One of those classes uses the other two classes.
I see that devtools::load_all()
loads the scripts in alphabetical order, so that if a script depends on another that is later in alphabetical order, there may be problems. Observe:
Example script a.r
:
setClass("a", slots = c(name = "character"))
Example script b.r
setClass("b", slots = c(name = "character", a = "a", c = "c"))
Example script c.r
:
setClass("c", slots = c(name = "character"))
When I run devtools::load_all()
, the following warning appears:
Warning messages:
1: undefined slot classes in definition of "b": c(class "c")
I do not want to rename my scripts simply to put them in alphabetical order based on when I want them to be loaded.
I do not want to define those classes in a single script, because I want to keep the code more modular.
How do I ensure that the script defining the dependent class has access to the other classes:
- Regardless of the names of the scripts those classes are in?
- Without resorting to
source()
since this would import other functions, objects, variables, from that script that are not needed.
In python
, this is relatively trivial. One uses a syntax like:
from <relative path to .py file that defines those objects> import <desired objects>
In R
, I am spinning in circles trying to accomplish something similar.
答案1
得分: 0
我通过深入研究DESCRIPTION
文件中的Collate
字段找到了一个合适的解决方案。
当我将这部分内容追加到DESCRIPTION
文件中时,在运行devtools::load_all()
时,警告消息会消失,因为文件将按照我指定的顺序加载,而不是按照字母顺序加载:
Collate:
a.r
c.r
b.r
这并未回答更一般的问题,即如何通过仅导入另一个脚本中定义的特定对象来复制python
的行为... load_all()
功能似乎仍然会加载脚本中的所有内容,而Collate
部分仅指定了顺序... 但我仍然将这视为一种胜利。
英文:
I found a suitable solution by digging into the Collate
field in the DESCRIPTION
file.
When I append this to the DESCRIPTION
file, then the warning goes away when I run devtools::load_all()
because files will be loaded in the order I specified rather than alphabetical order:
Collate:
a.r
c.r
b.r
This does not answer the more general question about how to replicate the behavior of python
by only importing specific objects defined in another script... The load_all()
functionality still seems to load everything in the scripts, and the Collate
section simply specifies the order... But I'll still count this as a win.
答案2
得分: -1
鉴于你的 b.r
宏需要 class(c)
被定义,我强烈建议你在 b.r
宏的顶部明确包含 load(c.r)
命令。否则,当有人尝试使用 load(b.r)
而不是 "load.all()" 来设置某些内容时,将会出现故障。始终避免依赖用户执行“额外”的操作。
英文:
Seeing as your b.r
macro would like to have class(c)
defined, I strongly recommend you explicitly include the command load(c.r)
at the top of the b.r
macro. Otherwise when someone tries to set up something with load(b.r)
rather than "load.all()` there'll be a failure anyway. Always avoid depending on the user executing something "extra" .
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论