Gustavo B Paterno
Gustavo B Paterno

Reputation: 986

ggplot2 inside R packages: Notes during CRAN tests

I am writing an R package that uses many ggplot2 functions. The problem is that during CRAN tests I have many notes related to ggplot functions (see below). Given this situation, what is the best way to use ggplot2 inside my functions in order to avoid these notes from CRAN?

An example of my code:

s1 <- ggplot2::ggplot(result,aes(x=slope,y=..density..),
            environment = environment())+
            geom_histogram(fill="lightyellow", 
                           alpha=.9,colour="grey60", size=.2) +
            geom_density(size=.2) +
            geom_vline(xintercept = slope.0,color="red",linetype=2,size=.7)+
            xlab("Estimated slopes")+
            theme(axis.text = element_text(size=14),
                  axis.title = element_text(size=16))

CRAN notes:

plot_influ_phylolm: no visible global function definition for ‘aes’
plot_influ_phylolm: no visible binding for global variable ‘slope’
plot_influ_phylolm: no visible binding for global variable ‘..density..’
plot_influ_phylolm: no visible global function definition for ‘geom_histogram’
plot_influ_phylolm: no visible global function definition for ‘geom_density’
plot_influ_phylolm: no visible global function definition for ‘geom_vline’ plot_influ_phylolm: no visible global function definition for ‘xlab’
plot_influ_phylolm: no visible global function definition for ‘theme’

Upvotes: 4

Views: 982

Answers (2)

dardisco
dardisco

Reputation: 5274

Not to advocate avoiding rules, but while you're working out what where all the imports come from, you can always use the following for the global variables:

slope <- density <- NULL

Add the above to the .R file which where these variables occur, before the variables appear as such in the code. This question has been addressed in more detail on this site here and here.

As for the function definitions, it's tricky figure out exactly which functions you're going to be importing with a NAMESPACE as large as ggplot2, so perhaps easiest to import the whole package. I realize this is also not 'best practice' but it should get you past the CRAN checks. If using roxygen I typically use the description file for the package e.g. myPkg_package.R:

#' @name myPkg-package
#' @docType package
#' @keywords package
#'
#' @import ggplot2
#'
NULL

or, for selective import:

#' @importFrom ggplot2 theme

You will then want to edit the DESCRIPTION file to include something like:

Imports: ggplot2

You may even want to use Depends: for this, but dependencies on multiple packages (e.g. >4) are discouraged on CRAN.

You could of course specify the NAMESPACE every time e.g. change theme to ggplot2::theme every time you call the function. Arguably this makes the code clearer, although it can get a little tedious and does add some slight overhead to execution time. This still requires you to declare the imports, as above.

Upvotes: 2

Dirk is no longer here
Dirk is no longer here

Reputation: 368409

You need to add the relevant Imports: to DESCRIPTION, and importFrom(...) to NAMESPACE. This has been discussed quite a bit recently, see for example this thread on r-package-devel and its references to further discussion.

Upvotes: 2

Related Questions