4
votes

I am trying to include the following ggplot2 helper function in a package [it wraps labels in grid_facet(.~variable, labeller = "plot.label.wrap")]:

#' Label wrapper for ggplot
#'
#' Include in the facet_grid option of ggplot.
#' @param variable
#' @param value
#' @return wrapper
#' @export

plot.label.wrap <- function(variable, value) {

  lapply(strwrap(as.character(value), width=15, simplify=FALSE),
         paste, collapse="\n")
}

My DESCRIPTION file includes: Imports: ggplot2. The scripts that uses the function includes: library(ggplot2).

The package builds, reloads and gives the documentation upon ?plot.label.wrap. It can be found:

> getAnywhere(plot.label.wrap)
A single object matching ‘plot.label.wrap’ was found
It was found in the following places
  registered S3 method for plot from namespace mypackage
  namespace:mypackage
with value

function(variable, value) {

  lapply(strwrap(as.character(value), width=15, simplify=FALSE),
         paste, collapse="\n")
}
<environment: namespace:mypackage>

However:

> plot.label.wrap
Error: object 'plot.label.wrap' not found

So my question is, why is this function found in the namespace of mypackage but not on the command line itself?

1
what does the namespace line for this function look like? it seems to be treated as an s3 method when that doesn't look like your intent - rawr
S3method(plot,footprint.facet), but no export line for that function. - Henk
try #' @export plot.label.wrap - rawr
By the name of your function, it looks like it's the plot method for label.wrap objects. Just rename the function to e.g. plot_label_wrap and it should work. - shadow
@rawr thanks! this worked. - Henk

1 Answers

3
votes

the solution in the comments was to be more specific about the export like below

#' Label wrapper for ggplot
#'
#' Include in the facet_grid option of ggplot.
#' @param variable
#' @param value
#' @return wrapper
#' @export plot.label.wrap

plot.label.wrap <- function(variable, value) {

  lapply(strwrap(as.character(value), width=15, simplify=FALSE),
         paste, collapse="\n")
}

roxygen tries to be smart about things like the @usage, @details, and @exports to make things simplier, but sometimes, like in this example, it doesn't always work out.

plot is an S3 generic method which can be defined as plot.someclass which will create a plotting method for some object x with class "someclass" and can be called simply by plot(x).

roxygen thinks that someclass for your package was "label.wrap" and exported plot.label.wrap as an S3 method rather than a normal function, expecting you to use it as plot(x) where class(x) is "label.wrap" and not trying to use plot.label.wrap directly as you tried (and found it doesn't work).

Alternatively, like @shadow mentioned, you could avoid this confusion by not using periods in function names, eg plot_label_wrap, plot_label.wrap,plot_labelwrap, etc. You don't have to, but in a few rare circumstances, using periods causes unexpected behaviors.