0

I'm creating a new package in R and I'd like to use ggplot2 as the plotting system. I already declared ggplot2 in my DESCRIPTION file. There are several functions in ggplot2 so, although it is not recommended, I'm using @import ggplot2 to access all of them. However, when I run the code, I get the following message:

Function ggplot could not be found.

Is there a specific way to make use of ggplot2 when creating a new package?

jroberayalas
  • 969
  • 7
  • 23
  • @Keon it's a new package. The library() command is [forbidden](http://r-pkgs.had.co.nz/namespace.html). – jroberayalas Apr 01 '15 at 21:51
  • how did you declare it in the description file. and it's fine to use require/library in your functions for a personal package, but technically it is bad practice – rawr Apr 01 '15 at 21:55
  • @rawr I've the following: Imports: ggplot2 The idea is to eventually release this to CRAN, that's why I do not want to use the library function – jroberayalas Apr 01 '15 at 22:00
  • imports loads the package but doesnt attach it to the `search()` path, so you still need to `library(ggplot2)`, so like hadley said, if your package is not useful without ggplot2, then you should depend it.. also see [here](http://stackoverflow.com/questions/14988722/in-r-what-does-loaded-via-a-namespace-and-not-attached-mean) – rawr Apr 01 '15 at 22:03
  • Is it just me, or did you reference it once as `ggplot` and everywhere as as `ggplot2`? – r2evans Apr 01 '15 at 22:09
  • @r2evans no, I did it on purpose. ggplot2 is the name of the package, and ggplot is the name of the function is not found. – jroberayalas Apr 01 '15 at 22:28
  • Sorry, got it. Of course, I should have seen the word "Function" at the beginning of the error. So much for powers of observation. On further inspection, I have nothing more to contribute over @rawr's *"import loads the package but doesn't attach it ... still need to `library(ggplot2)`"*. – r2evans Apr 01 '15 at 22:29
  • @rawr thanks for your comment. I move the package to Depends and my code is working. – jroberayalas Apr 01 '15 at 22:31
  • @r2evans no worries! Thanks also. I still have a question: what do you mean by 'attach'? And, if I include the package in the Depends section, do I still need to call it with @import? – jroberayalas Apr 01 '15 at 22:35
  • In my mind, `Import:` is similar to `install.packages()`: just because it is installed does not mean you can use its functions or data within using `require` or `library`. By `attach`, I think about inserting a package's namespace into the search path. For your question on `@import`, I will redirect you to hadley's [namespace](http://r-pkgs.had.co.nz/namespace.html) section titled "R functions" where he discusses tradeoffs. – r2evans Apr 01 '15 at 22:41
  • Thanks @r2evans, I'll have a look at the link – jroberayalas Apr 01 '15 at 22:50
  • 2
    Importing usually is preferable to depending. Have you declared ggplot2 under `Imports` in your DESCRIPTION file and do you see `import(ggplot2)` in your NAMESPACE file? If you look at the reverse imports at the [ggplot2 CRAN page](http://cran.r-project.org/web/packages/ggplot2/index.html) you'll find many packages that do this. No calls to `require`, `library` or `attach` should be necessary when importing correctly. – Roland Apr 02 '15 at 06:57
  • @Roland I declared `ggplot2` under `Imports` in my DESCRIPTION file, and then used `@import ggplot2` in the R script. This was not working properly, so I move `ggplot2` to `Depends` in the DESCRIPTION file and it worked. I'm still trying to understand if this is the right way to build a package. – jroberayalas Apr 02 '15 at 16:23

0 Answers0