Configuring NAMESPACE and DESCRIPTION when using a Suggested Package

I have written an R package which uses the rstudioapi::viewer() function. Obviously, not everyone uses RStudio. I am now running in circles trying to correctly configure the NAMESPACE and DESCRIPTION .

So as not to force users to install a package which they don't need (and/or would be useless on their system), I tried putting rstudioapi in the Suggests section and call it conditional on its availability:

if(.Platform$GUI == "RStudio") {
  if ("rstudioapi" %in% rownames(installed.packages())) {
    rstudioapi::viewer(outfile_path)
  } else {
    message("To view html content in RStudio, run install.packages('rstudioapi').")
    message("Switching method to 'browser'")
    method <- "browser"
  }

But on R CMD CHECK , I get:

checking dependencies in R code ... WARNING
'::' or ':::' import not declared from: 'rstudioapi'

So I go and declare it, adding importFrom(rstudioapi, viewer) to my NAMESPACE . Results:

checking package dependencies ... ERROR
Namespace dependency not required: 'rstudioapi'

Returning to the official docs, I also tried the following:

if (requireNamespace("rstudioapi", quietly = TRUE)) {
    rstudioapi::viewer(outfile_path)
  } else { ...

To no avail:

checking dependencies in R code ... WARNING
'::' or ':::' import not declared from: 'rstudioapi'
'loadNamespace' or 'requireNamespace' call not declared from: 'rstudioapi'

So I'm either getting a warning for not declaring it, or an error for declaring it. Damned if you don't, and even more damned if you do kinda thing. Any help appreciated.


Another option that occurred to me is to simply not list rstudioapi anywhere in your DESCRIPTION file. RStudio has tips for using the viewer that might be helpful and don't require a dependency on rstudioapi .

Their recommendation is to use

viewer <- getOption("viewer")
if (!is.null(viewer))
  viewer("http://localhost:8100")
else
  utils::browseURL("http://localhost:8100")

This works because, on startup, RStudio creates an option called viewer and populates it with a function. If you are not using RStudio, getOption("viewer") will return NULL and you can redirect it to use the system's default browser. This is essentially what you've already done, but with no additional dependencies required.

This also has the benefit of being about 1000 times faster than checking against installed.packages , but that's still measured in nanoseconds, so probably not a big concern (caveat, I only have 192 packages installed. It could take longer on a system that has downloaded everything on CRAN).


I figured out why I had those warnings / errors. When I added rstudioapi to the Suggests: list, I inadvertently created a second Suggests: list. Only the second one was taken into account (and yeah, the existing one was at the bottom of the DESCRIPTION file, totally I missed it. I'm not erasing this in case it happens to someone else...


I have a few ideas that might help.

It might be useful to refer to Hadley Wickham's online R packages book: http://r-pkgs.had.co.nz/description.html. I learned most of what (little) I know about packages from his book.

I think that you can add to your DESCRIPTION file, under the "Suggests" field the package rstudioapi with the following code:

   devtools::use_package("rstudioapi", "Suggests")

I've always used the Roxygen2 package (and more recently devtools ) for interacting with the NAMESPACE file, so I don't manually edit NAMESPACE.

Here is what my DESCRIPTION file looks like:

    Package: stack3
    Type: Package
    Title: What the Package Does (Title Case)
    Version: 0.1.0
    Author: Who wrote it
    Maintainer: The package maintainer <yourself@somewhere.net>
    Description: More about what it does (maybe more than one line)
               Use four spaces when indenting paragraphs within the Description.
    License: What license is it under?
    Encoding: UTF-8
    LazyData: true
    Suggests:
         rstudioapi
    RoxygenNote: 5.0.1

I then ran

    devtools::build()

to get the stack3_0.1.0.tar.gz file. Note that I named my package stack3 . In running R CMD CHECK stack3_0.1.0.tar.gz, I see that I get no errors and one warning. The warning is due to the text that by default follows License: in the DESCRIPTION file.

    R CMD CHECK stack3_0.1.0.tar.gz
    * using log directory ‘/Users/frederickboehm/Box Sync/stack3.Rcheck’
    * using R version 3.3.3 (2017-03-06)
    * using platform: x86_64-apple-darwin13.4.0 (64-bit)
    * using session charset: UTF-8
    * checking for file ‘stack3/DESCRIPTION’ ... OK
    * checking extension type ... Package
    * this is package ‘stack3’ version ‘0.1.0’
    * package encoding: UTF-8
    * checking package namespace information ... OK
    * checking package dependencies ... OK
    * checking if this is a source package ... OK
    * checking if there is a namespace ... OK
    * checking for executable files ... OK
    * checking for hidden files and directories ... OK
    * checking for portable file names ... OK
    * checking for sufficient/correct file permissions ... OK
    * checking whether package ‘stack3’ can be installed ... OK
    * checking installed package size ... OK
    * checking package directory ... OK
    * checking DESCRIPTION meta-information ... WARNING
    Non-standard license specification:
      What license is it under?
    Standardizable: FALSE
    * checking top-level files ... OK
    * checking for left-over files ... OK
    * checking index information ... OK
    * checking package subdirectories ... OK
    * checking whether the package can be loaded ... OK
    * checking whether the package can be loaded with stated dependencies ... OK
    * checking whether the package can be unloaded cleanly ... OK
    * checking whether the namespace can be loaded with stated dependencies ... OK
    * checking whether the namespace can be unloaded cleanly ... OK
    * checking loading without being on the library search path ... OK
    * checking examples ... NONE
    * checking PDF version of manual ... OK
    * DONE

    Status: 1 WARNING
    See
      ‘/Users/frederickboehm/Box Sync/stack3.Rcheck/00check.log’ for details.

I hope that I understood your question and that this response is helpful.

链接地址: http://www.djcxy.com/p/71158.html

上一篇: form2中的datagridview在从form2插入后自动刷新

下一篇: 使用建议包时配置NAMESPACE和说明