Re: [Rd] Getting hold of a package's environment from C code
On 10/22/06, Duncan Murdoch <[EMAIL PROTECTED]> wrote: > On 10/22/2006 3:56 PM, Deepayan Sarkar wrote: > > Hi, > > > > I have a package where I'm calling an R function (say "foo") from C > > code. "foo" is in the same package, but is not exported. I construct > > the call using lang1(install("foo")), but to eval it I need the > > package's environment. Is there a way to do this? Passing the correct > > environment through .Call() is not an option. > > > > Right now, I'm getting the environment first using something like > > > > rho = PROTECT(eval(lang2(install("environment"), install("bar")), > > R_GlobalEnv)) > > > > where bar _is_ exported. However, this doesn't work if the package is > > loaded but not attached, and is also risky because someone might > > define another "bar" that is found first. > > In R code, you could use getNamespace("pkg") to get the namespace > environment. I haven't tried this, but I'd assume > > PROTECT(rho = eval(lang2(install("getNamespace"), > ScalarString(mkChar("pkg"; > > (or something like it) would be the C equivalent. Thanks, that works perfectly. -Deepayan __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] Getting hold of a package's environment from C code
Hi, I have a package where I'm calling an R function (say "foo") from C code. "foo" is in the same package, but is not exported. I construct the call using lang1(install("foo")), but to eval it I need the package's environment. Is there a way to do this? Passing the correct environment through .Call() is not an option. Right now, I'm getting the environment first using something like rho = PROTECT(eval(lang2(install("environment"), install("bar")), R_GlobalEnv)) where bar _is_ exported. However, this doesn't work if the package is loaded but not attached, and is also risky because someone might define another "bar" that is found first. One solution that might work is to make 'rho' a global variable and assign the correct value when the package is loaded. Is that a good idea? How should I PROTECT it? -Deepayan __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Getting hold of a package's environment from C code
On 10/22/2006 3:56 PM, Deepayan Sarkar wrote: > Hi, > > I have a package where I'm calling an R function (say "foo") from C > code. "foo" is in the same package, but is not exported. I construct > the call using lang1(install("foo")), but to eval it I need the > package's environment. Is there a way to do this? Passing the correct > environment through .Call() is not an option. > > Right now, I'm getting the environment first using something like > > rho = PROTECT(eval(lang2(install("environment"), install("bar")), > R_GlobalEnv)) > > where bar _is_ exported. However, this doesn't work if the package is > loaded but not attached, and is also risky because someone might > define another "bar" that is found first. In R code, you could use getNamespace("pkg") to get the namespace environment. I haven't tried this, but I'd assume PROTECT(rho = eval(lang2(install("getNamespace"), ScalarString(mkChar("pkg"; (or something like it) would be the C equivalent. > > One solution that might work is to make 'rho' a global variable and > assign the correct value when the package is loaded. Is that a good > idea? How should I PROTECT it? I'd say to avoid globals if you can, and I think you can here. Duncan Murdoch __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] utils::methods doesn't list methods of 'hidden' generics
Hi all. I've seen that, on my R installation, utils::methods doesn't list methods of generics whose name begins with a dot, and I can't see that mentioned in utils::methods help page. Antonio. # > .foo <- function(x, ...) UseMethod(".foo") > .foo.bar <- function(x, ...) {} > methods(.foo) no methods were found > version _ platform x86_64-unknown-linux-gnu arch x86_64 os linux-gnu system x86_64, linux-gnu status major 2 minor 4.0 year 2006 month 10 day03 svn rev39566 language R version.string R version 2.4.0 (2006-10-03) __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] Changing function arguments
R-Developers, I'm looking for some help computing on the R language. I'm hoping to write a function that parses a language or expression object and returns another expression with all instances of certain argument of a given function altered. For instance, say I would like my function, myFun to take an expression and whenever the argument 'x' appears within the function FUN inside that expression, return an altered expression in which 'x' is incremented by one. Thus, > x <- expression(FUN(x = 0) + log(FUN(x = 3))) > myFun(x) [1] expression(FUN(x = 1) + log(FUN(x = 4))) Conceptually, it looks like I want to recursively break a language object into its component functions, search for FUN and increment one of its arguments ('x'), then reassemble and return the resulting expression. However, I haven't been able to come up with a clean way to do this. Suggestions would be greatly appreciated. Thanks in advance, Robert Robert McGehee Quantitative Analyst Geode Capital Management, LLC 53 State Street, 5th Floor | Boston, MA | 02109 Tel: 617/392-8396Fax:617/476-6389 mailto:[EMAIL PROTECTED] This e-mail, and any attachments hereto, are intended for us...{{dropped}} __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Changing function arguments
See ?body ?parse ?deparse ?gsub > foo <- function(x) x+.1 > bar <- function(y) y+foo(x=1) + foo(x=2) > bar(1) [1] 4.2 > body(bar) y + foo(x = 1) + foo(x = 2) > body(bar) <- parse(text=gsub("x[ ]*=[ ]*([0-9])","x = 1 + > \\1",deparse(body(bar > bar(1) [1] 6.2 > body(bar) y + foo(x = 1 + 1) + foo(x = 1 + 2) > There are many, many ways to skin this cat. The 'parse( text = ... )' is but one. Operating on parsed objects without deparsing them is often preferred. On Sun, 22 Oct 2006, McGehee, Robert wrote: > R-Developers, > I'm looking for some help computing on the R language. > > I'm hoping to write a function that parses a language or expression > object and returns another expression with all instances of certain > argument of a given function altered. For instance, say I would like my > function, myFun to take an expression and whenever the argument 'x' > appears within the function FUN inside that expression, return an > altered expression in which 'x' is incremented by one. > > Thus, >> x <- expression(FUN(x = 0) + log(FUN(x = 3))) >> myFun(x) > [1] expression(FUN(x = 1) + log(FUN(x = 4))) > > Conceptually, it looks like I want to recursively break a language > object into its component functions, search for FUN and increment one of > its arguments ('x'), then reassemble and return the resulting > expression. However, I haven't been able to come up with a clean way to > do this. Suggestions would be greatly appreciated. > > Thanks in advance, > Robert > > Robert McGehee > Quantitative Analyst > Geode Capital Management, LLC > 53 State Street, 5th Floor | Boston, MA | 02109 > Tel: 617/392-8396Fax:617/476-6389 > mailto:[EMAIL PROTECTED] > > > > This e-mail, and any attachments hereto, are intended for us...{{dropped}} > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > Charles C. Berry(858) 534-2098 Dept of Family/Preventive Medicine E mailto:[EMAIL PROTECTED] UC San Diego http://biostat.ucsd.edu/~cberry/ La Jolla, San Diego 92093-0717 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Getting hold of a package's environment from C code
"Deepayan Sarkar" <[EMAIL PROTECTED]> writes: > On 10/22/06, Duncan Murdoch <[EMAIL PROTECTED]> wrote: >> In R code, you could use getNamespace("pkg") to get the namespace >> environment. I haven't tried this, but I'd assume >> >> PROTECT(rho = eval(lang2(install("getNamespace"), >> ScalarString(mkChar("pkg"; >> >> (or something like it) would be the C equivalent. Perhaps: R_FindNamespace(mkString(where)) + seth __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Getting hold of a package's environment from C code
Seth Falcon <[EMAIL PROTECTED]> writes: > Perhaps: > > R_FindNamespace(mkString(where)) Sorry, this won't help you for package-level code as this function is part of the internal use only API. It would be nice to have access to it or a similar function from C in package code. __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Changing function arguments
Try this. If the first arg of FUN is x then it increments it. incrx <- function (e) { is.node <- function(x) is.symbol(x) || is.double(x) if (is.node(e)) return(e) if (is.name(e[[1]]) && e[[1]] == as.name("FUN") && names(e)[2] == "x") e[[2]] <- e[[2]] + 1 for (i in 1:length(e)) e[[i]] <- incrx(e[[i]]) return(e) } incrx(expression(FUN(x = 0) + log(FUN(x = 3 On 10/22/06, McGehee, Robert <[EMAIL PROTECTED]> wrote: > R-Developers, > I'm looking for some help computing on the R language. > > I'm hoping to write a function that parses a language or expression > object and returns another expression with all instances of certain > argument of a given function altered. For instance, say I would like my > function, myFun to take an expression and whenever the argument 'x' > appears within the function FUN inside that expression, return an > altered expression in which 'x' is incremented by one. > > Thus, > > x <- expression(FUN(x = 0) + log(FUN(x = 3))) > > myFun(x) > [1] expression(FUN(x = 1) + log(FUN(x = 4))) > > Conceptually, it looks like I want to recursively break a language > object into its component functions, search for FUN and increment one of > its arguments ('x'), then reassemble and return the resulting > expression. However, I haven't been able to come up with a clean way to > do this. Suggestions would be greatly appreciated. > > Thanks in advance, > Robert > > Robert McGehee > Quantitative Analyst > Geode Capital Management, LLC > 53 State Street, 5th Floor | Boston, MA | 02109 > Tel: 617/392-8396Fax:617/476-6389 > mailto:[EMAIL PROTECTED] > > > > This e-mail, and any attachments hereto, are intended for us...{{dropped}} > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Getting hold of a package's environment from C code
On Sun, 22 Oct 2006, Deepayan Sarkar wrote: > On 10/22/06, Duncan Murdoch <[EMAIL PROTECTED]> wrote: >> On 10/22/2006 3:56 PM, Deepayan Sarkar wrote: >>> Hi, >>> >>> I have a package where I'm calling an R function (say "foo") from C >>> code. "foo" is in the same package, but is not exported. I construct >>> the call using lang1(install("foo")), but to eval it I need the >>> package's environment. Is there a way to do this? Passing the correct >>> environment through .Call() is not an option. >>> >>> Right now, I'm getting the environment first using something like >>> >>> rho = PROTECT(eval(lang2(install("environment"), install("bar")), >>> R_GlobalEnv)) >>> >>> where bar _is_ exported. However, this doesn't work if the package is >>> loaded but not attached, and is also risky because someone might >>> define another "bar" that is found first. >> >> In R code, you could use getNamespace("pkg") to get the namespace >> environment. I haven't tried this, but I'd assume >> >> PROTECT(rho = eval(lang2(install("getNamespace"), >> ScalarString(mkChar("pkg"; >> >> (or something like it) would be the C equivalent. mkString("pkg") is simpler than ScalarString(mkChar("pkg")). -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel