Why does x in "assign(x)" correctly evaluate to "rank" where UseMethod(func) does not get correctly evaluated?
Can we use as.call(list(UseMethod,func))? >> assign(paste(func,".default",sep=""),get(func),pos=env) >> >> assign(func,function(x,...) UseMethod(func),pos=env) On Wed, Dec 22, 2010 at 9:03 PM, William Dunlap [via R] <ml-node+3161542-1898476570-206...@n4.nabble.com> wrote: > Try the following, which I haven't tested much > and needs more error checking (e.g., to see that > the function is not already generic and that > its argument list is compatible with (x,...)). > I put in the print statements to show what > the calls to substitute() do. > > toGeneric <- function (funcName) { > Â Â stopifnot(is.character(funcName)) > Â Â funcItself <- get(funcName) > Â Â stopifnot(is.function(funcItself)) > Â Â envir <- environment(funcItself) > Â Â tmp <- substitute(funcSymbol <- function(x, ...) > UseMethod(funcName), > Â Â Â Â list(funcSymbol = as.symbol(funcName), funcName = funcName)) > Â Â print(tmp) > Â Â eval(tmp, envir = envir) > Â Â tmp <- substitute(defaultSymbol <- funcItself, list(defaultSymbol = > as.symbol(paste(sep = ".", > Â Â Â Â funcName, "default")), funcItself = funcItself)) > Â Â print(tmp) > Â Â eval(tmp, envir = envir) > } > > E.g., > > Â Â > wsx <- function(x, base=2)log(x, base=base) > Â Â > toGeneric("wsx") > Â Â wsx <- function(x, ...) UseMethod("wsx") > Â Â wsx.default <- function (x, base = 2) > Â Â log(x, base = base) > Â Â > wsx > Â Â function (x, ...) > Â Â UseMethod("wsx") > Â Â > wsx.default > Â Â function (x, base = 2) > Â Â log(x, base = base) > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > >> -----Original Message----- >> From: [hidden email] >> [mailto:[hidden email]] On Behalf Of rballen >> Sent: Wednesday, December 22, 2010 2:42 PM >> To: [hidden email] >> Subject: [R] forcing evaluation of a char string argument >> >> >> I'm trying to make a function to turn a regular function into >> an S3 generic >> one. I want myMethod to be: >> >> function(x,...) UseMethod("myMethod") >> >> But I keep getting: >> >> function(x,...) UseMethod(func) >> >> Here's the function: >> >> toGeneric<-function(func) { >> env<-environment(get(func)) >> >> # default method of new generic = the original function >> assign(paste(func,".default",sep=""),get(func),pos=env) >> >> assign(func,function(x,...) UseMethod(func),pos=env) >> } >> >> toGeneric("myMethod") >> >> I messed around with force, substitute, and deparse, but I >> can't get any of >> those to help. >> >> Thanks. >> -- >> View this message in context: >> http://r.789695.n4.nabble.com/forcing-evaluation-of-a-char-str > ing-argument-tp3161365p3161365.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> [hidden email] mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > > > ________________________________ > View message @ > http://r.789695.n4.nabble.com/forcing-evaluation-of-a-char-string-argument-tp3161365p3161542.html > To unsubscribe from forcing evaluation of a char string argument, click > here. -- View this message in context: http://r.789695.n4.nabble.com/forcing-evaluation-of-a-char-string-argument-tp3161365p3161666.html Sent from the R help mailing list archive at Nabble.com. [[alternative HTML version deleted]]
______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.