Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R using s3 and s4 methods of simulate in the same package

I'm puzzled by the error

found an S4 version of 'simulate' so it has not been imported correctly 

I have written an R package that includes a definition for a simulate() method as an S3 method. Because the generic for simulate is already defined, I simply define a simulate.myclass (simulate.fitContinuous in my case).

The package also depends on another package that has an S4 version of simulate. When loading my package, I get the S4 version error above. I'm not sure what is producing the error.

Reproducible example by grabbing the package from github, or do

require(devtools) 
install_github("pmc", "cboettig")
require(pmc)

To reproduce this error from scratch: Create a new package with minimal DESCRIPTION file. include the DESCRIPTION imports: ouch. Create a NAMESPACE and add imports(ouch) and S3method(simulate, test). Create the R directory, add the a trivial R script (I've included roxygen documentation that will generate the NAMESPACE I've just mentioned, but this error can also be created without devtools/roxygen):

#' simulate
#' 
#' a test for s3/s4 conflicts
#' @param object who cares?
#' @param nsim guess.
#' @param seed yup
#' @param ... other parameters we will just ignore
#' @return something
#' @method simulate test
#' @S3method simulate test
#' @import ouch
simulate.test <- function(object, nsim = 1, seed = NULL, ...){
  message("This test worked")
}

Install the package (document with devtools first if you like), and you get the error.

My best solution so far is to eliminate the S3method line from the NAMESPACE, and export the full function simulate.test instead. This will pass check and install without warnings, but is clearly an inferior solution.

A different solution is to have ouch in depends as well as imports, and document the S3 method properly (as above). Then everything works as expected, but the warning message remains.

like image 330
cboettig Avatar asked Oct 08 '22 23:10

cboettig


1 Answers

simulate is an S3 generic defined in stats, so according to section 1.6.2 of "Writing R Extensions" (the example isn't clear -- there are exceptions for generics defined in base) your NAMESPACE file should have

importFrom(stats, simulate)
S3method(simulate, fitContinuous)

The business about "found an S4 method" seems to reflect when the problem was discovered -- trying to add S4 methods to an S3 generic that wasn't visible (the "it" I guess refers the the generic simulate).

like image 155
Martin Morgan Avatar answered Oct 18 '22 15:10

Martin Morgan