Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: roxygen2, imported packages do not appear in namespace

I have a file: import_packages.r in my project, containing following:

#' @import reshape2
#' @import ggplot2
#' @import DESeq2
#' @import geneplotter
#' @import survcomp
#' @import gplots
#' @import pheatmap
#' @import RColorBrewer

When I do devtools:document() those packages are not shown in NAMESPACE file and they are not imported in fact. Am I doing something wrong?

like image 952
Alina Avatar asked Mar 02 '16 15:03

Alina


People also ask

What is roxygen2 namespace?

The package NAMESPACE is one of the most confusing parts of building a package. Roxygen2 aims to make it as easy as possible to build a package that is a well-behaved member of the R ecosystem. This is a little frustrating at first, but soon becomes second-nature.

How do I export an object from a roxygen2 package?

By default roxygen2 doesn’t export anything from your package. If you want an object to be publicly available, you must explicitly tag it with @export. Use the following guidelines to decide what to export: Functions: export functions that you want to make available.

What happens when a R package is installed?

When a package is installed, all R code of the package is lumped together in a single file, executed, and the result is stored in the installed package. Of course, the scope of the import isn't actually limited to the functions foo and bar, they're imported globally for the package I'm developing.

How do I convert roxygenise comments to Rd?

When you roxygenise () (or devtools::document ()) your package these comments will be automatically transformed to the .Rd that R uses to generate the documentation you see when you type ?str_length. To get started, first read vignette ("roxygen2").


1 Answers

If your file only contains the lines that you provided, it is ignored by roxygen2. You should add a line after the roxygen code that contains just NULL. So the following should work:

#' @import reshape2 ggplot2 DESeq2 geneplotter
#' @import survcomp gplots pheatmap RColorBrewer
NULL

I have also reduced the number of lines to show you, how several packages can be imported with a single use of @import. But it does not matter for roxygen, on how many lines you distribute the packages.

I think the reason for this is that the roxygen sections must be associated with some R object. For example, the documentation of a function is associated with the corresponding function object. Since you don't want to have the imports associated with a function, you can associate them with NULL, which is an R object as well.

As hadley rightly points out, it is not recommended to fully import that many packages because you might end up with name conflicts. The following two alternatives are usually better:

  • Reference function with their explicit package name and the :: operator: reshape2::melt() This has the added advantage that you immediately see, from which package a function comes.

  • Import only the functions that you need from a package using @importFrom:

    #' @importFrom reshape2 melt cast
    

You can find more information here.

like image 129
Stibu Avatar answered Sep 29 '22 11:09

Stibu