简体   繁体   English

R包开发如何抑制依赖包生成的消息?

[英]R package development how to suppress messages generated from dependency package?

I am developing an R package called VSHunter and need NMF package as a dependency, however, every time load NMF will throw many message, I don't know how to suppress them.我正在开发一个名为VSHunter的 R 包,需要NMF包作为依赖项,但是,每次加载 NMF 时都会抛出很多消息,我不知道如何抑制它们。

> devtools::load_all(".")
Loading VSHunter
Loading required package: NMF
Loading required package: pkgmaker
Loading required package: registry

Attaching package: ‘pkgmaker’

The following object is masked from ‘package:base’:

    isFALSE

Loading required package: rngtools
Loading required package: cluster
NMF - BioConductor layer [OK] | Shared memory capabilities [NO: 
bigmemory] | Cores 7/8
  To enable shared memory capabilities, try: install.extras('
NMF
')

I don't want to bother user and expect the result我不想打扰用户并期待结果

> devtools::load_all(".")
Loading VSHunter

and

> library(VSHunter)
Loading VSHunter

Here are some things you can do to reduce the noise when loading packages with devtools::load_all :在使用devtools::load_all加载包时,您可以采取以下措施来减少噪音:

  • devtools::load_all(..., quiet = TRUE) handles messages for this single package, but not necessarily dependent packages devtools::load_all(..., quiet = TRUE)处理这个单个包的消息,但不一定是依赖包
  • try explicitly loading required packages in ./R/zzz.R in the onLoad function.尝试在onLoad函数的./R/zzz.R中显式加载所需的包。 For example:例如:

     .onLoad <- function(libname, pkgname) { invisible(suppressPackageStartupMessages( sapply(c("tibble", "purrr", "dplyr", "tidyr", "ggplot2", "data.table"), requireNamespace, quietly = TRUE) )) }

    (BTW: I used sapply here for laziness, not that it adds much to things. It could easily have been a for loop with no consequence.) (顺便说一句:我在这里使用sapply为了懒惰,并不是说它增加了很多东西。它很容易成为一个没有后果的for循环。)

    For a discussion about the use of requireNamespace in place of library , see "library vs require" , and "Writing R Extensions" where it states有关使用requireNamespace代替library的讨论,请参阅“library vs require”“Writing R Extensions” ,其中说明

    R code in the package should call library or require only exceptionally.包中的 R 代码应调用库或仅在例外情况下才需要。 Such calls are never needed for packages listed in 'Depends' as they will already be on the search path. 'Depends' 中列出的包永远不需要这样的调用,因为它们已经在搜索路径上。 It used to be common practice to use require calls for packages listed in 'Suggests' in functions which used their functionality, but nowadays it is better to access such functionality via :: calls.过去通常的做法是在使用其功能的函数中对“建议”中列出的包使用 require 调用,但现在最好通过 :: 调用访问此类功能。

    What we are doing is technically not required, but I think by forcing doing it this way, it is encouraging more-silent operation.我们正在做的事情在技术上不是必需的,但我认为通过强制这样做,它鼓励了更安静的操作。 (This rides on the coat-tails of (这取决于

    Notice that I used suppressPackageStartupMessages .请注意,我使用了suppressPackageStartupMessages "Courteous" package maintainers use packageStartupMessage instead of message for their loading messages: the latter takes a bit more work and is much less discriminant than the former, which is very easily suppressed without unintended consequences. “有礼貌”的包维护者使用packageStartupMessage而不是message来加载他们的消息:后者需要更多的工作,并且比前者的判别性要小得多,后者很容易被抑制而不会产生意外后果。 There are many packages that do not do this, for which I think it's fair to submit a PR to fix.有很多包没有这样做,我认为提交 PR 来修复是公平的。

    Another comment about requireNamespace : this means that the functions in those packages will not be in the search path of the R sessions.关于requireNamespace另一个评论:这意味着这些包中的函数不会在 R 会话的搜索路径中。 If the user will always be using certain packages (eg, data.table or dplyr ), then you might want to explicitly load them with library .如果用户将始终使用某些包(例如, data.tabledplyr ),那么您可能希望使用library显式加载它们。 From "Writing R Extensions" again:再次从“编写 R 扩展”

    Field 'Depends' should nowadays be used rarely, only for packages which are intended to be put on the search path to make their facilities available to the end user (and not to the package itself): for example it makes sense that a user of package latticeExtra would want the functions of package lattice made available.字段“依赖”现在应该很少使用,仅用于旨在放置在搜索路径上以使最终用户(而不是包本身)可以使用其设施的包:例如,用户包点阵Extra 想要包点阵的功能可用。

    However, if you're being good about your package, then you are using :: notation for all non-base packages anyway.但是,如果你对你的包很好,那么你无论如何都会对所有非基础包使用::符号。 There are certainly ways you can get around using :: , but (1) CRAN checks are rather intense at times, (2) explicit is usually "A Good Thing (tm)", and (3) it can actually make maintainability much easier (such as when a dependent package changes their API/ABI and you need to check all calls to their package, where searching for pkgname:: is much easier than searching for each of their functions individually).您当然可以使用::绕过一些方法,但是(1)CRAN 检查有时相当激烈,(2)显式通常是“一件好事(tm)”,并且(3)它实际上可以使可维护性更容易(例如,当依赖包更改其 API/ABI 并且您需要检查对其包的所有调用时,搜索pkgname::比单独搜索它们的每个函数要容易得多)。

  • Some packages use .onLoad too liberally, doing things that are not strictly necessary and/or have unneeded side-effect.一些包过于自由地使用.onLoad ,做一些不是绝对必要的和/或有不需要的副作用的事情。 For this, you can always write a function such as load_deppkgs_silently(updatesearchpath=TRUE) that can be called manually or on-load with the presence of an option.为此,您始终可以编写诸如load_deppkgs_silently(updatesearchpath=TRUE)类的函数,该函数可以在存在选项的情况下手动调用或在加载时调用。 (I'm thinking about your end users here, I'm a big fan of providing flexibility and the ability to not load things they way I do.) (我在这里考虑的是您的最终用户,我非常喜欢提供灵活性以及按照我的方式加载内容的能力。)

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

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