4

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’

2 Answers 2

2

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.

2
  • Thanks a lot for you explanation. I will try that and let you know if worked! Aug 3, 2015 at 6:36
  • 1
    Great this solved my problem! I used @importFrom ggplot2 aes geom_point etc. Now the notes are gone. The variables I solved with the NULL method. variable1 <- variable2 <- NULL. Aug 3, 2015 at 17:25
2

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.

1
  • Thanks for you reply, I will have a look at the thread and give you some feedback. Aug 3, 2015 at 6:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.