Setzer.Woodrow@epamail.epa.gov
2001-Mar-08 18:27 UTC
[Rd] surprising behavior of match.arg() (PR#872)
If a function needs to be passed as an argument to another function, default arguments to the function being passed are lost. Consider this example: fun1 <- function(x, A=c("power","constant")) { arg <- match.arg(A) cat(paste("A is:",paste(A,collapse=", "),"\narg is:",arg,"\n")) cat("formals:\n") print(formals()) 2*x } topfun <- function(x, Fun=fun1) { cat("\n>>>Called as fun1:\n") fun1(x) cat("\n>>>Called as Fun:\n") Fun(x,A="power") Fun2 <- fun1 cat("\n>>>Called as Fun2:\n") Fun2(x,A="power") } cat("\n>>>>>>>>>>fun1\n") print(topfun(2,fun1)) When run, this gives:> source("C:/home/tasks/OPModels/TestBugs/demo.R")>>>>>>>>>>fun1>>>Called as fun1:A is: power, constant arg is: power formals: $x $A c("power", "constant")>>>Called as Fun:A is: power arg is: formals: NULL>>>Called as Fun2:A is: power arg is: power formals: $x $A c("power", "constant") [1] 4>I included the assignment within topfun() because I originally thought the problem would apply to any assignment of a function as a value. This is apparently incorrect. I've sent this to r-bugs because it seems that this behavior would surely not have been intended. I am running (actually, I'm still normally running 1.2.1, but I figured if I didn't check on 1.2.2, I'd find out this had been fixed :>) --please do not edit the information below-- Version: platform = i386-pc-mingw32 arch = x86 os = Win32 system = x86, Win32 status major = 1 minor = 2.2 year = 2001 month = 02 day = 26 language = R Windows 9x 4.10 (build 2222) A Search Path: .GlobalEnv, package:ctest, Autoloads, package:base R. Woodrow Setzer, Jr. Phone: (919) 541-0128 Experimental Toxicology Division Fax: (919) 541-5394 Pharmacokinetics Branch NHEERL MD-74; US EPA; RTP, NC 27711 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Setzer.Woodrow@epamail.epa.gov writes:> If a function needs to be passed as an argument to another function, > default arguments to the function being passed are lost. Consider this > example:Interesting... Apparently, sys.function() is still a promise after the function has been called (and the promise obviously forced). Minimal example of same effect:> f<-function()sys.function() > f()function()sys.function()> g<-delay(f) > g()<promise: 0x8b248b4>> g<-g > g()function()sys.function() -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Setzer.Woodrow@epamail.epa.gov
2001-Mar-08 19:30 UTC
[Rd] surprising behavior of match.arg() (PR#872)
So, in my toy example, when I add the line Fun <- Fun after the first invocation of Fun(), Fun finds the default args when invoked a second time. That gives me an easy work around for the real problem where I encountered this. Thanks. R. Woodrow Setzer, Jr. Phone: (919) 541-0128 Experimental Toxicology Division Fax: (919) 541-5394 Pharmacokinetics Branch NHEERL MD-74; US EPA; RTP, NC 27711 Peter Dalgaard BSA To: Woodrow Setzer/RTP/USEPA/US@EPA <p.dalgaard@bios cc: r-devel@stat.math.ethz.ch, R-bugs@biostat.ku.dk tat.ku.dk> Subject: Re: [Rd] surprising behavior of match.arg() (PR#872) Sent by: pd@pubhealth.ku. dk 03/08/01 01:43 PM Setzer.Woodrow@epamail.epa.gov writes:> If a function needs to be passed as an argument to another function, > default arguments to the function being passed are lost. Consider this > example:Interesting... Apparently, sys.function() is still a promise after the function has been called (and the promise obviously forced). Minimal example of same effect:> f<-function()sys.function() > f()function()sys.function()> g<-delay(f) > g()<promise: 0x8b248b4>> g<-g > g()function()sys.function() -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._