R: roxygen2, imported packages do not appear in namespace
Asked Answered
G

1

13

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?

Gynoecium answered 2/3, 2016 at 15:8 Comment(2)
sometimes it helps to remove the NAMESPACE file, to force to recreate the file again.Marvelmarvella
@Marvelmarvella did not help. the packages still do not appear in NAMESPACEGynoecium
G
20

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.

Gaultiero answered 2/3, 2016 at 16:2 Comment(2)
Also worth noting that fully importing many packages is not recommended practice.Godin
@Godin Thanks for the input. I'll add a few words about the better practices later.Gaultiero

© 2022 - 2024 — McMap. All rights reserved.