使用roxygen2时如何指定以何种顺序加载S4方法 [英] How to specify in which order to load S4 methods when using roxygen2

查看:249
本文介绍了使用roxygen2时如何指定以何种顺序加载S4方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



说你有两个类, classA classB 在以下文件中描述 classA.R

 #'class class A 
#'
#'这是一个类A blabla
#'\section {Slots} {\describe {\ item {\code {A}} {a Character}}}
#'@ name classA
#'@rdname classA
#'@exportClass classA
setClass(classA ,表示(A =character))

classB.R

 #'class class B 
#'
#'这是一个B类blabla
#'\section {Slots} {\describe {\item {\code {B}} {A类的对象}}}
#'@ name classB
#'@rdname classB
#'@exportClass classB
setClass(classB,表示(B =classA))
pre>

我相信这些文件按字母顺序按 roxygen 2 ,但情况并非如此。如果我尝试构建包,我可能会收到以下错误:

  roxygenize(./ myExample)
getClass中的错误(Class,where = topenv(parent.frame())):
ClassA不是定义的类

我如何确保 roxygenize()知道以哪种顺序读取文件,即哪个类定义应该在其他?






注意:我知道我回答了我自己的问题。这是因为我经常遇到这个问题,并且在查看 roxygen2 的代码后,意识到了正确的方法。所以为了参考,这里是我的发现。

解决方案

有两种方法可以实现:



如上所述在?collat​​e_roclet 中,您可以使用@include标记来指定哪个类应该在哪个类之前读取。在这种情况下,您可以在实际的R代码之前添加文件 classB.r 以下行:

 #'@include classA.r 

特别是更新 DESCRIPTION 文件中的分页字段,是处理该问题的推荐方法。 p>

在某些情况下,依赖关系可能非常复杂,您希望自己保持概述,而不完全依赖于添加 @include 代码库中的标签。在这种情况下,您可以直接在 DESCRIPTION 文件的末尾指定 Collat​​e 字段,如下所示:

 包装:myExample 
类型:包
...
整理:
'classA .R'
'classB.R'

函数 roxygenize ()首先检查 DESCRIPTION 文件,并按照它们指定的顺序加载指定的任何文件。只有这样,包装的其余部分才会被加载。


I ran into following problem already multiple times.

Say you have two classes, classA and classB described in the following files classA.R :

#' the class classA
#'
#' This is a class A blabla
#' \section{Slots}{\describe{\item{\code{A}}{a Character}}}
#' @ name classA
#' @rdname classA
#' @exportClass classA
setClass("classA",representation(A="character"))

And classB.R

#' the class classB
#'
#' This is a class B blabla
#' \section{Slots}{\describe{\item{\code{B}}{an object of class A}}}
#' @ name classB
#' @rdname classB
#' @exportClass classB
setClass("classB",representation(B="classA"))

I believed these files were read in alphabetical order by roxygen2, but that is not the case. If I try to build the package, I might get the following error:

roxygenize("./myExample")
Error in getClass(Class, where = topenv(parent.frame())) :
   "ClassA" is not a defined class

How can I make sure that roxygenize() knows in which order to read the files, i.e. which class definition should be read before the other?


Note : I know I answered my own question. That is because I ran into this problem rather often, and realized the proper way to do this after looking at the code of roxygen2. So for reference, here's my findings.

解决方案

There are two ways to achieve this:

As described in ?collate_roclet, you can use the @include tag to specify which class should be read before which. In this case, you can add to the file classB.r the following line right before the actual R code:

#' @include classA.r

These tags are read specifically to update the Collate field in the DESCRIPTION file, and are the recommended way of dealing with the problem.

In some cases the dependencies might be so complex you want to keep an overview yourself and not rely completely on adding @include tags in your codebase. In that case you can just specify the Collate field at the end of the DESCRIPTION file, like this:

Package: myExample
Type: Package
...
Collate:
    'classA.R'
    'classB.R'

The function roxygenize() first checks the DESCRIPTION file, and loads whatever files are specified there first in the order they're specified. Only then the rest of the package is loaded.

这篇关于使用roxygen2时如何指定以何种顺序加载S4方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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