2

My package uses ggplot2 extensively to make custom plots. I import ggplot2 in the NAMESPACE file. I can build, install, and load the package into R without errors. But when I call a function from my package, it complains it can't find a function in that imported package. The function that I called had absolute nothing to do with plotting and didn't make use of anything from ggplot2. How can I make function calls from my package work without these types of errors?

Error in theme_beata() (from plot.R#14) : could not find function "theme_classic"

The first function erroring is:

#' A custom ggplot2 theme for Beata
#' @import ggplot2
#' @family themes
#' inheritParams ggplot2::theme_classic
#' @export
theme_beata <- function(base_size = 14, base_family="serif", ticks = TRUE) {

    ret <- theme_classic() +
      theme(axis.line = element_line(size=0.6),
            axis.text = element_text(size = base_size),
            axis.title = element_text(size= base_size + 2),
            title = element_text(size=base_size + 4))
    if (!ticks) {
        ret <- ret + theme(axis.ticks = element_blank())
    }
    ret
}

I am using devtools and roxygen2 to document my package. I also discovered I am now getting this error when running document().

> document()
Updating pkg documentation
Loading pkg
Error in theme_beata() (from plot.R#11) : could not find function "theme_classic"
> traceback()
13: theme_beata()
12: as.vector(y)
11: setdiff(names(theme_gray()), names(new))
10: ggplot2::theme_set(theme_beata()) at plot.R#25
9: eval(expr, envir, enclos)
8: eval(exprs[i], envir)
7: source_one(file, envir = envir)
6: source_many(paths, env)
5: force(code)
4: in_dir(file.path(pkg$path, "R"), source_many(paths, env))
3: load_code(pkg)
2: load_all(pkg)
1: document()

From reading Hadley's book on packaging, I don't think the DESCRIPTION matters in terms of exports, but below is the bottom part:

Depends: R (>= 3.1.0)
License: file LICENSE
LazyData: false
Imports:
    dplyr (>= 0.4.0),
    tidyr (>= 0.1.0),
    XLConnect,
    pryr,
    stringr,
    devEMF,
    assertthat,
    grid,
    ggplot2,
    ggthemes,
    GGally,
    broom,
    scales
Suggests:
    lattice

My NAMESPACE has the following imports:

import(XLConnect)
import(dplyr)
import(ggplot2)
import(ggthemes)
import(stringr)
import(tidyr)
wdkrnls
  • 4,548
  • 7
  • 36
  • 64
  • For the sake of completeness, could you show the relevant lines of your NAMESPACE and the Imports line from your DESCRIPTION? – Gregor Thomas Apr 01 '15 at 18:13
  • 1
    I tried your code and it worked well... I included your function in my package and could easily modify a theme by adding `+ theme_beata()` to the ggplot call. – Daniel Apr 01 '15 at 18:27
  • Fair enough, now `XLConnect` is breaking (perhaps first, perhaps theme_beata works?). I don't really understand why. – wdkrnls Apr 01 '15 at 18:38
  • 2
    I've had trouble with packages where I accidentally have old versions hanging around. Try restarting R, completely removing any installed versions of the package from all libraries, and re-documenting and re-installing your package. – Gregor Thomas Apr 01 '15 at 18:39
  • And also, when you restart R, make sure you're starting with a clean workspace not [loading and old workspace by default](http://stackoverflow.com/q/29392539/903061). – Gregor Thomas Apr 01 '15 at 21:43

0 Answers0