In many variants, similar questions have been asked many times ... but i do not find a clear advice about:
"exporting S3 methods as functions"
I wrote a custom S3 class with roxygen2, call it 'my_item'. This is the constructor function:
my_item <- function(n) structure(list(n=n),class='my_item')
What I need is a way to define a "list of my_items => data.frame"
cast function:
#' @method as.data.frame my_item
#' @export
as.data.frame.my_item <- function(x) ...
As soon as I call it with a my_item in this way, it is fine:
as.data.frame(my_item('a'))
But applying the same call to a list of objects cannot work, because class(list)
is empty:
as.data.frame(list(my_item('a'),my_item('b')))
This cannot work also because the function/method is not exported
as.data.frame.my_item(list(my_item('a'),my_item('b')))
This does not work with explicit namespace qualification:
my_pkg::as.data.frame.my_item(...)
Error: 'as.data.frame.my_item' is not an exported object from 'namespace:my_pkg'
In the package zoo, this is possible for plot
generic function.
See plot.zoo
, an S3 method, exported as a function:
In zoo::NAMESPACE
export(
...
"plot.zoo"
...
)
S3method("plot", "zoo")
S3method("as.data.frame", "zoo")
The resulting package scoping is:
library(zoo)
methods('as.data.frame')
[1] as.data.frame.aovproj* as.data.frame.array as.data.frame.AsIs
[4] as.data.frame.character as.data.frame.chron* as.data.frame.complex
[7] as.data.frame.data.frame as.data.frame.data.table* as.data.frame.Date
[10] as.data.frame.dates* as.data.frame.default as.data.frame.difftime
[13] as.data.frame.factor as.data.frame.ftable* as.data.frame.integer
[16] as.data.frame.ITime* as.data.frame.list as.data.frame.logical
[19] as.data.frame.logLik* as.data.frame.matrix as.data.frame.model.matrix
[22] as.data.frame.noquote as.data.frame.numeric as.data.frame.numeric_version
[25] as.data.frame.ordered as.data.frame.POSIXct as.data.frame.POSIXlt
[28] as.data.frame.raw as.data.frame.shingle* as.data.frame.table
[31] as.data.frame.times* as.data.frame.ts as.data.frame.vector
[34] as.data.frame.yearmon* as.data.frame.yearqtr* as.data.frame.zoo*
see '?methods' for accessing help and source code
methods('plot')
[1] plot.acf* plot.data.frame* plot.decomposed.ts* plot.default plot.dendrogram*
[6] plot.density* plot.ecdf plot.factor* plot.formula* plot.function
[11] plot.hclust* plot.histogram* plot.HoltWinters* plot.isoreg* plot.lm*
[16] plot.medpolish* plot.mlm* plot.ppr* plot.prcomp* plot.princomp*
[21] plot.profile.nls* plot.raster* plot.shingle* plot.spec* plot.stepfun
[26] plot.stl* plot.table* plot.times* plot.trellis* plot.ts
[31] plot.tskernel* plot.TukeyHSD* plot.zoo
see '?methods' for accessing help and source code
This shows that plot.zoo
is exported, as.data.frame.zoo*
is not exported
So probably the question is wrong.
A better one would be:
"How implement cast-protocol ('as-...') when using 'lists-of-list-based-S3-objects'?
I've taken a different approach:
'my_item'+'_list'
class to a generic list via anas.my_item_list()
functionas.data.frame.my_item_list
method working on my "custom" listsas.data.frame(as.my_item_list(some_list))
In the following example, I substituted
my_item
class name with a shorterob
DEFINITION
TEST
It looks good!