gpt4 book ai didi

r - 在R中创建包时链接多个文件

转载 作者:行者123 更新时间:2023-12-04 04:18:16 25 4
gpt4 key购买 nike

我试图在R中创建一个包,其中创建了许多新的自定义类。每个类都在不同的文件中。这些类从父类继承,并继承到其他类。

在运行我的代码时,我这样称呼每个人

source("package/father.R")
source("package/son.R")
source("package/grandson.R")

Son类中孙子类所需的某些方法的定义。我使用package.skeleton()来调用它们中的每一个并创建一个包,它似乎可以正常工作。但是在运行R CMD Check(并尝试安装到R)时,它会引发错误,因为该函数尝试按字母顺序调用文件,因此文件sonson.R在son.R之前被调用,并且显示并显示错误提示该方法尚未定义。如果我将名称更改为zgrandson.R,则R会在最后一次调用该文件,并且一切正常,但这显然不是解决问题的方法。

我已经阅读了有关创建程序包的教程,但是它们似乎都处理了简单的情况,即在R中没有继承/调用其他文件。希望我已经清楚了。

最佳答案

据我了解,您可以使用Collate文件中的DESCRIPTION字段进行控制。

引用Writing R Extensions manual:

An ‘Collate’ field can be used for controlling the collation order for the R code files in a package when these are processed for package installation. The default is to collate according to the ‘C’ locale. If present, the collate specification must list all R code files in the package (taking possible OS-specific subdirectories into account, see Package subdirectories) as a whitespace separated list of file paths relative to the R subdirectory. Paths containing white space or quotes need to be quoted. An OS-specific collation field (‘Collate.unix’ or ‘Collate.windows’) will be used instead of ‘Collate’.



因此,您可以指定:
Collate:
father.r
son.R
grandson.r

或只是简单地重命名文件,这样就可以按照您在问题中指出的顺序,按字典顺序排序将得出正确的排序顺序。

但也请参阅@DirkEddelbuettel的 this answer关于类似问题。

关于r - 在R中创建包时链接多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12312440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com