Rowe, Brian - Eqty NY
2010-Sep-23 22:44 UTC
[Rd] Behavior of R CMD build and library() w.r.t. setGeneric-like functions
Hello developeRs, Apologies in advance for a rather long email, but to describe the problem, I need to step through many details. I have been working on a new dispatching system (futile.paradigm on CRAN) based on functional programming concepts that is an alternative to S3 and S4 dispatching. I use a declarative syntax using guard statements to control the dispatching between function variants. I also provide post assertions via an 'ensure' command that programatically behave similar to the 'guard' command. In a sense, these work like setGeneric/setMethod in S4. As a simple example, I write code like this: guard(month.date, isa(Date,date)) ensure(month.date, as.numeric(result) < 13) month.date <- function(date) format(date, '%m') guard(month.int, is.numeric(date)) month.int <- function(date) month(i2date(date)) and call it like this: > month(20100913) [1] "09" Behind the scenes, the guard command is using 'assign' to create a parent function with the name 'month'. This can be done explicitly as well, although it is typically not necessary. The parent definition looks like this: month <- function(...) UseFunction('month',...) The package works fine when I'm writing scripts, but all heck breaks loose when I try to write a package that depends on futile.paradigm. When I run R CMD check, it doesn't seem that the guard commands are being executed. A less likely hypothesis is that they are being deleted by the cleanEx() function. * checking examples ... ERROR Running examples in 'pars.core-Ex.R' failed. The error most likely occurred in:> set_date(20100921)Error: could not find function "set_date" Execution halted If I add the explicit function definition for 'month' as shown above, I get one of my own error messages indicating that no guard statements were executed.> set_date(20100921)Error in UseFunction("set_date", ...) : Function must have guards for functional dispatching Calls: set_date -> UseFunction Execution halted I decided to bypass the check process and just install the built package to see if it is an issue with check as opposed to the package. Unfortunately, it seems that when the package is built, the guard commands get stripped from the package.> library(futile.paradigm)Loading required package: futile.options> debug(guard) > library(pars.core)Loading required package: futile.logger It's unclear to me why this happens when setClass, setGeneric, etc work under seemingly similar conditions. Is this because of the export* declarations in the NAMESPACE? What happens in the R CMD build process that strips these function calls and their side-effects? Finally, what do I need to do to make my 'guard' and 'ensure' commands operate properly in a package context? Warm Regards, Brian Rowe ---------------------------------------------------------------------- This message w/attachments (message) is intended solely ...{{dropped:7}}
Rowe, Brian - Eqty NY
2010-Sep-24 18:24 UTC
[Rd] Behavior of R CMD build and library() w.r.t. setGeneric-likefunctions
FYI I resolved this with a combination of explicit variable declarations and tweaking namespace environment visibility. I'd still like to get a better understanding of how setGeneric is able to define functions that are visible without the explicit variable declarations. Any insight into this is appreciated. Regards, Brian -----Original Message----- From: r-devel-bounces at r-project.org [mailto:r-devel-bounces at r-project.org] On Behalf Of Rowe, Brian - Eqty NY Sent: Thursday, September 23, 2010 6:44 PM To: r-devel at r-project.org Subject: [Rd] Behavior of R CMD build and library() w.r.t. setGeneric-likefunctions Hello developeRs, Apologies in advance for a rather long email, but to describe the problem, I need to step through many details. I have been working on a new dispatching system (futile.paradigm on CRAN) based on functional programming concepts that is an alternative to S3 and S4 dispatching. I use a declarative syntax using guard statements to control the dispatching between function variants. I also provide post assertions via an 'ensure' command that programatically behave similar to the 'guard' command. In a sense, these work like setGeneric/setMethod in S4. As a simple example, I write code like this: guard(month.date, isa(Date,date)) ensure(month.date, as.numeric(result) < 13) month.date <- function(date) format(date, '%m') guard(month.int, is.numeric(date)) month.int <- function(date) month(i2date(date)) and call it like this: > month(20100913) [1] "09" Behind the scenes, the guard command is using 'assign' to create a parent function with the name 'month'. This can be done explicitly as well, although it is typically not necessary. The parent definition looks like this: month <- function(...) UseFunction('month',...) The package works fine when I'm writing scripts, but all heck breaks loose when I try to write a package that depends on futile.paradigm. When I run R CMD check, it doesn't seem that the guard commands are being executed. A less likely hypothesis is that they are being deleted by the cleanEx() function. * checking examples ... ERROR Running examples in 'pars.core-Ex.R' failed. The error most likely occurred in:> set_date(20100921)Error: could not find function "set_date" Execution halted If I add the explicit function definition for 'month' as shown above, I get one of my own error messages indicating that no guard statements were executed.> set_date(20100921)Error in UseFunction("set_date", ...) : Function must have guards for functional dispatching Calls: set_date -> UseFunction Execution halted I decided to bypass the check process and just install the built package to see if it is an issue with check as opposed to the package. Unfortunately, it seems that when the package is built, the guard commands get stripped from the package.> library(futile.paradigm)Loading required package: futile.options> debug(guard) > library(pars.core)Loading required package: futile.logger It's unclear to me why this happens when setClass, setGeneric, etc work under seemingly similar conditions. Is this because of the export* declarations in the NAMESPACE? What happens in the R CMD build process that strips these function calls and their side-effects? Finally, what do I need to do to make my 'guard' and 'ensure' commands operate properly in a package context? Warm Regards, Brian Rowe ---------------------------------------------------------------------- This message w/attachments (message) is intended solely ...{{dropped:7}} ______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel ---------------------------------------------------------------------- This message w/attachments (message) is intended solely ...{{dropped:7}}
Reasonably Related Threads
- Introducing the futile.paradigm, a package for functional dispatching in R
- Introducing the futile.paradigm, a package for functional dispatching in R
- Users list and the date the password will expire
- Users list and the date the password will expire
- Problems with S4 methods dispatching on `...` (aka dotsMethods)