Re: [R] Environment question

2015-11-13 Thread Duncan Murdoch
On 13/11/2015 12:53 PM, ALBERTO VIEIRA FERREIRA MONTEIRO wrote: I have another environment question. I understand why this works as expected: f.factory <- function() { y <- 2 fname <- paste("plus", y, sep = ".") f <- function(x) x + y assign(fname, f, envir = globalenv()) }

Re: [R] Environment question

2015-11-13 Thread ALBERTO VIEIRA FERREIRA MONTEIRO
I have another environment question. I understand why this works as expected: f.factory <- function() { y <- 2 fname <- paste("plus", y, sep = ".") f <- function(x) x + y assign(fname, f, envir = globalenv()) } f.factory() plus.2(2) # 4 and I also understand why this does NOT work:

Re: [R] Environment question

2015-11-13 Thread William Dunlap
Make a new environment for each function and populate it with the variables that your functions require. local() is a convenient way to do this: f.factory3 <- function(destinationEnvir = globalenv()) { for (y in 2:3) { fname <- paste("plus", y, sep = ".") f <- local(function(x) x + y,

Re: [R] Environment question

2015-10-23 Thread Duncan Murdoch
On 23/10/2015 11:08 AM, ALBERTO VIEIRA FERREIRA MONTEIRO wrote: > From the code below: > > y <- 1 > > f1 <- function() { > cat("y =", y, "\n") > } > > f2 <- function() { > y <- 2 > f1() > } > > f3 <- function() { > y <- 3 > f <- f1 > f() > } > > f4 <- function() { > y <- 4 >

Re: [R] Environment question

2015-10-23 Thread Bert Gunter
I do not understand exactly what you're looking for: "Any way to rewrite the code.." is pretty vague. Here is _an_ answer, which may completely miss what you mean, followed by some comments. y <- 1 f1 <- function(x=y) { cat("y =", x, "\n") } f2 <- function() { y <- 2 f1() } f3 <-

[R] Environment question

2015-10-23 Thread ALBERTO VIEIRA FERREIRA MONTEIRO
>From the code below: y <- 1 f1 <- function() { cat("y =", y, "\n") } f2 <- function() { y <- 2 f1() } f3 <- function() { y <- 3 f <- f1 f() } f4 <- function() { y <- 4 f <- function() { cat("y =", y, "\n") } f() } f1() f2() f3() f4() Clearly, f1(), f2() and f4() will

[R] environment question

2014-08-29 Thread Erin Hodgess
Hello! Here is yet another question which I strongly suspect has a simple answer. I build an RcmdrPlugin package and saved my workspace when I came out of R. For some reason, it save the namespace of the plugin as an environment. When I load the workspace back in, 2 environments appear,

Re: [R] environment question: changing variables from a formula through model.frame?

2011-01-30 Thread Niels Richard Hansen
Hi Tal On 29/01/11 13.25, Tal Galili wrote: Hello all, I came across a behavior of R with environments that I'm not sure what is causing it. It involves changing variables that are found through using model.frame on a formula inside a function. I wonder if it's a bug or a feature. And in

Re: [R] environment question: changing variables from a formula through model.frame?

2011-01-30 Thread Tal Galili
Hello Niels, Thank you very much! Your solution was exactly what I needed. (I didn't realize that formula has it's own environment - very useful!) Best, Tal Contact Details:--- Contact me: tal.gal...@gmail.com |

[R] environment question: changing variables from a formula through model.frame?

2011-01-29 Thread Tal Galili
Hello all, I came across a behavior of R with environments that I'm not sure what is causing it. It involves changing variables that are found through using model.frame on a formula inside a function. I wonder if it's a bug or a feature. And in either case, how it might be managed. Here is a

[R] environment question

2008-07-28 Thread Edna Bell
Hi R users! I was looking at some of the example code for the environment function. Here it is: e1 - new.env(parent = baseenv()) # this one has enclosure package:base. e2 - new.env(parent = e1) assign(a, 3, envir=e1) ls(e1) ls(e2) exists(a, envir=e2) # this succeeds by inheritance exists(a,

Re: [R] environment question

2008-07-28 Thread Jeffrey Horner
Edna Bell wrote: Hi R users! I was looking at some of the example code for the environment function. Here it is: e1 - new.env(parent = baseenv()) # this one has enclosure package:base. e2 - new.env(parent = e1) assign(a, 3, envir=e1) ls(e1) ls(e2) exists(a, envir=e2) # this succeeds by