简体   繁体   English

导入NAMESPACE和DESCRIPTION问题吗?

[英]import NAMESPACE and DESCRIPTION question?

When installing a package R will look at the 'Depends' and 'Imports' in the DESCRIPTION file to see which additional packages need installing. 在安装软件包时,R将查看DESCRIPTION文件中的“ Depends”和“ Imports”,以查看需要安装哪些其他软件包。

When attaching 'library()' the package to an R session then R will load any packages listed under 'Imports' and attach any listed under 'Depends'. 将软件包“ library()”附加到R会话时,R将加载“导入”下列出的所有软件包,并附加“依赖”下列出的任何软件包。

My question is regarding importing functions in the NAMESPACE file? 我的问题是关于在NAMESPACE文件中导入函数? I'm confused as to the purpose of importing functions in that file and exactly what it's doing. 我对于在该文件中导入函数的目的以及它的作用感到困惑。

Are imported functions in the NAMESPACE file attached to the R session when the main package is attached? 附加主软件包时,是否将NAMESPACE文件中的导入函数附加到R会话?

If the answer is yes then is this not what 'Depends' is already doing? 如果答案是肯定的,那么这不是“ Depends”已经在做什么吗? The only difference i can see is that individual functions can be attached to the session using NAMESPACE where as 'Depends' attaches the entire package. 我能看到的唯一区别是,可以使用NAMESPACE将各个函数附加到会话上,其中“ Depends”附加了整个程序包。

I would suggest reading the Namespaces chapter of Hadley's R Packages book. 我建议阅读Hadley的R Packages书中的Namespaces一章 But in short, the answer is No. 简而言之,答案是否定的。

Are imported functions in the NAMESPACE file attached to the R session when the main package is attached? 附加主软件包时,是否将NAMESPACE文件中的导入函数附加到R会话?

No, they are not. 不,他们不是。 Imported functions are available for use in the package internals, but not attached to the user's search tree. 导入的功能可在包内部使用,但未附加到用户的搜索树中。

Another source for info is, of course, Writing R Extensions . 当然,另一个信息来源是Writing R Extensions They describe IMPORTS as: 他们将IMPORTS描述为:

The 'Imports' field lists packages whose namespaces are imported from (as specified in the NAMESPACE file) but which do not need to be attached. “导入”字段列出了其名称空间是从中导入的软件包(如NAMESPACE文件中指定的),但无需附加软件包。


As a demonstration, the current version of ggplot2 , v 3.2.1, has import(scales) in its NAMESPACE file. 作为演示,当前版本的ggplot2 v 3.2.1在其NAMESPACE文件中具有import(scales) In a fresh R session, we can load ggplot2 and observe that the scales package is not attached: 在新的R会话中,我们可以加载ggplot2并观察到scales包未附加:

library(ggplot2)
percent(1)
# Error in percent(1) : could not find function "percent"
scales::percent(1)
# [1] "100%"

ggplot2 uses functions from scales internally, and can do so without using the package::function notation. ggplot2在内部使用scales函数,无需使用package::function表示法就可以这样做。 This is what the import(scales) accomplishes. 这就是import(scales)完成的工作。 However, unlike with Depends , scales is not attached for the user. 但是,与Depends不同, scales没有为用户附加。

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

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