简体   繁体   English

描述顺序导入:和R 2.14.0包检查中的NAMESPACE import()

[英]Order of DESCRIPTION Imports: and NAMESPACE import() in R 2.14.0 package checking

I'm trying to chase down what seems to be a conflict between function names as I check a package. 我试图在检查包时追逐功能名称之间的冲突。 I may eventually ask directly about the problem, but first, I am wondering about three things, none of which seem to be mentioned in R-exts: 我最终可能会直接询问这个问题,但首先,我想知道三件事情,R-exts中似乎都没有提及:

  1. The packages listed in DESCRIPTION: Imports and NAMESPACE imports() should be the same, right? DESCRIPTION:Imports和NAMESPACE导入()中列出的包应该是相同的,对吧?
  2. Within either list, does the order of importing matter? 在任何一个列表中,导入的顺序是否重要? If so, is there any general advice about how to order them? 如果是的话,是否有关于如何订购的一般建议?
  3. I use R --vanilla CMD check pkg_name to avoid having my .Rprofile interfere. 我使用R --vanilla CMD检查pkg_name以避免我的.Rprofile干扰。 When my .Rprofile is active and contains library(pkg_name) statements, does the order of those matter? 当我的.Rprofile处于活动状态并包含库(pkg_name)语句时,这些语句的顺序是否重要?

You asked three questions. 你问了三个问题。

1. List packages in DESCRIPTION as well as NAMESPACE 1.列出DESCRIPTIONNAMESPACE

Each package listed in DESCRIPTION Imports: must have a matching entry NAMESPACE import(...) . DESCRIPTION Imports:列出的每个包都必须具有匹配的条目NAMESPACE import(...) The entry in DESCRIPTION may contain version information, but in NAMESPACE you only name the package. DESCRIPTION的条目可能包含版本信息,但在NAMESPACE您只能命名包。

Note for the unwary: Spell Imports with capital I and trailing s in DESCRIPTION 注意粗心:拼写Imports资本I和尾s的说明

For example: 例如:

DESCRIPTION 描述

Imports:
    stringr (>= 0.5)

NAMESPACE NAMESPACE

import(stringr)

2. Order matters 2.订单事宜

Packages that you load or import later masks packages that were loaded or imported earlier. 您稍后loadimport软件包会屏蔽先前加载或导入的软件包。 This only matters if you import packages that have export a function with identical name. 这仅在导入导出具有相同名称的函数的包时才有意义。

For example, both lattice and ggplot2 export a layer function. 例如, latticeggplot2导出layer函数。 Thus if you first import lattice and then ggplot2 , it means that ggplot2::layer will mask lattice::layer . 因此,如果您首先导入lattice然后导入ggplot2 ,则意味着ggplot2::layer将屏蔽lattice::layer

In other words, using layer will refer to ggplot2::layer . 换句话说,使用layer将引用ggplot2::layer If you want to refer to the lattice version you need to explicitly refer to lattice::layer in your function. 如果要引用lattice版本,则需要在函数中显式引用lattice::layer

3. The order of loading packages also matter 3.加载包的顺序也很重要

For the same reason, the order of loading packages (either in a script or in .Rprofile) matters. 出于同样的原因,加载包的顺序(在脚本或.Rprofile中)很重要。 Any new package that you load will mask functions with the same name in previously loaded packages. 您加载的任何新包将在先前加载的包中屏蔽具有相同名称的函数。

When this happens, R does the sensible thing and tells you about it in a console message. 当发生这种情况时,R会做出明智的事情并在控制台消息中告诉您。

Here is an example of masking that occurs when loading the reshape package, which depends on plyr but also masks some functions in plyr : 下面是加载reshape包时发生的屏蔽示例,它取决于plyr但也会屏蔽plyr某些函数:

library(reshape)
Loading required package: plyr

Attaching package: 'plyr'

The following object(s) are masked from 'package:braidppt':

    .


Attaching package: 'reshape'

The following object(s) are masked from 'package:plyr':

    rename, round_any

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM