Andreas Borg
2011-Mar-11 10:07 UTC
[Rd] Using missing() in a S4 method with extra arguments
Hi all,
I have a function which makes use of missing() to determine which
arguments are provided in the call - basically, there are two sets of
arguments that map to different strategies the function uses to fulfill
its task. After conversion to an S4 generic I've run into the problem
that if a method uses extra arguments that are not in the signature of
the generic, usage of missing() fails. The following example exemplifies
this:
setGeneric("fun", function(x=0, y=0, ...)
standardGeneric("fun"))
# both methods should output if the second argument is missing
setMethod("fun", "character", function(x=0, y=0, ...)
missing(y))
setMethod("fun", "numeric", function(x=0, y=0, z=0, ...)
missing(y))
fun("a") # this works fine
fun(1) # this gives "FALSE
I've understood so far that this is due to the fact that the
"numeric"
method in this example is rewritten to:
function (x = 0, y = 0, ...)
{
.local <- function (x = 0, y = 0, z = 0, ...)
missing(y)
.local(x, y, ...)
}
The call to .local evaluates y and it is no more missing.
Is there any alternative that works in this case? Or is there a chance
that missing() might be changed to work in this case in the near future?
Of course I know I could set NA or NULL as default values and check for
these, but there are reasons I want to have legal default values for all
arguments.
Best regards,
Andreas
Andreas Borg
Medizinische Informatik
UNIVERSIT?TSMEDIZIN
der Johannes Gutenberg-Universit?t
Institut f?r Medizinische Biometrie, Epidemiologie und Informatik
Obere Zahlbacher Stra?e 69, 55131 Mainz
www.imbei.uni-mainz.de
Telefon +49 (0) 6131 175062
E-Mail: borg at imbei.uni-mainz.de
Diese E-Mail enth?lt vertrauliche und/oder rechtlich gesch?tzte Informationen.
Wenn Sie nicht der
richtige Adressat sind oder diese E-Mail irrt?mlich erhalten haben, informieren
Sie bitte sofort den
Absender und l?schen Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte
Weitergabe
dieser Mail und der darin enthaltenen Informationen ist nicht gestattet.
Martin Morgan
2011-Mar-11 14:00 UTC
[Rd] Using missing() in a S4 method with extra arguments
On 03/11/2011 02:07 AM, Andreas Borg wrote:> Hi all, > > I have a function which makes use of missing() to determine which > arguments are provided in the call - basically, there are two sets of > arguments that map to different strategies the function uses to fulfill > its task. After conversion to an S4 generic I've run into the problem > that if a method uses extra arguments that are not in the signature of > the generic, usage of missing() fails. The following example exemplifies > this: > > setGeneric("fun", function(x=0, y=0, ...) standardGeneric("fun")) > # both methods should output if the second argument is missing > setMethod("fun", "character", function(x=0, y=0, ...) missing(y)) > setMethod("fun", "numeric", function(x=0, y=0, z=0, ...) missing(y)) > > fun("a") # this works fine > fun(1) # this gives "FALSEHi Andreas -- if you're testing for the missing-ness of y, and y is in the function signature, then use that for dispatch setMethod(fun, c("character", "missing"), function(x=0, y=0, z=0, ...) "missing") setMethod(fun, c("character", "ANY"), function(x=0, y=0, z=0, ...) "not missing") Since you're dispatching on x and y, it doesn't really make sense (to me ;) to assign default values to them. Testing for missing-ness of z would I think have to rely on NA / NULL or other sentinel. Martin> > I've understood so far that this is due to the fact that the "numeric" > method in this example is rewritten to: > > function (x = 0, y = 0, ...) > { > .local <- function (x = 0, y = 0, z = 0, ...) > missing(y) > .local(x, y, ...) > } > > The call to .local evaluates y and it is no more missing. > > Is there any alternative that works in this case? Or is there a chance > that missing() might be changed to work in this case in the near future? > > Of course I know I could set NA or NULL as default values and check for > these, but there are reasons I want to have legal default values for all > arguments. > > Best regards, > > Andreas > > Andreas Borg > Medizinische Informatik > > UNIVERSIT?TSMEDIZIN > der Johannes Gutenberg-Universit?t > Institut f?r Medizinische Biometrie, Epidemiologie und Informatik > Obere Zahlbacher Stra?e 69, 55131 Mainz > www.imbei.uni-mainz.de > > Telefon +49 (0) 6131 175062 > E-Mail: borg at imbei.uni-mainz.de > > Diese E-Mail enth?lt vertrauliche und/oder rechtlich gesch?tzte > Informationen. Wenn Sie nicht der > richtige Adressat sind oder diese E-Mail irrt?mlich erhalten haben, > informieren Sie bitte sofort den > Absender und l?schen Sie diese Mail. Das unerlaubte Kopieren sowie die > unbefugte Weitergabe > dieser Mail und der darin enthaltenen Informationen ist nicht gestattet. > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel-- Computational Biology Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: M1-B861 Telephone: 206 667-2793
Andreas Borg
2011-Mar-11 15:22 UTC
[Rd] Using missing() in a S4 method with extra arguments
Hi Martin, in the real function, I am not dispatching on the argument for which I test missingness, but it might be a good idea to do so - this way I could make the function tidier by relocating different branches to seperate methods. Thanks for the suggestion! Andreas> if you're testing for the missing-ness of y, and y is in the function > signature, then use that for dispatch > > setMethod(fun, c("character", "missing"), > function(x=0, y=0, z=0, ...) "missing") > setMethod(fun, c("character", "ANY"), > function(x=0, y=0, z=0, ...) "not missing") > > Since you're dispatching on x and y, it doesn't really make sense (to me > ;) to assign default values to them. Testing for missing-ness of z would > I think have to rely on NA / NULL or other sentinel. > > Martin >-- Andreas Borg Medizinische Informatik UNIVERSIT?TSMEDIZIN der Johannes Gutenberg-Universit?t Institut f?r Medizinische Biometrie, Epidemiologie und Informatik Obere Zahlbacher Stra?e 69, 55131 Mainz www.imbei.uni-mainz.de Telefon +49 (0) 6131 175062 E-Mail: borg at imbei.uni-mainz.de Diese E-Mail enth?lt vertrauliche und/oder rechtlich gesch?tzte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrt?mlich erhalten haben, informieren Sie bitte sofort den Absender und l?schen Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail und der darin enthaltenen Informationen ist nicht gestattet.
Seemingly Similar Threads
- Problem with documentation of user-defined operator (S4 method)
- How to find out if a data frame has automatic row names?
- [R] Sweave stops when opening X11 device fails
- Package with multiple shared libraries
- Feature request: txtProgressBar with ability to write to arbitrary stream