. They'll just ignore w if it isn't relevant to them. Your
> foo() code becomes something like this:
>
>x <- y <- z <- 0
>
># here is my scope problem
>result <- list(x = x, y = y, z = z) # c == 0 case
>if (c==1) result <- bar1()
>
uot;]]
y <- result[["y"]]
z <- result[["z"]]
Duncan Murdoch
- Original Message -
From: "Duncan Murdoch"
To: "Sebastien Bihorel" ,
r-help@r-project.org
Sent: Tuesday, October 30, 2018 4:13:05 PM
Subject: Re: [R] Question about function
18 4:17:30 PM
Subject: Re: [R] Question about function scope
Hi Sebastien,
I like Duncan's response. An alternative approach is to pass around
environments, as in the following:
bar1 <- function(e) {
e$x <- e$y <- e$z <- 1
cat(sprintf('bar1: x=%d, y=%d, z=%d\n', e$x
That's cool! I think this solution would fit better with what my intended setup.
Thanks a lot
- Original Message -
From: "Duncan Murdoch"
To: "Sebastien Bihorel" ,
r-help@r-project.org
Sent: Tuesday, October 30, 2018 4:18:51 PM
Subject: Re: [R] Question abo
Here's another modification to your code that also works. It's a lot
uglier, but will allow bar1 and bar2 to be used in multiple functions,
not just foo.
bar1 <- function(env){
env$x <- 1
env$y <- 1
env$z <- 1
with(env, cat(sprintf('bar1: x=%d, y=%d, z=%d\n', x, y, z)))
}
bar2 <- func
.
- Original Message -
From: "Duncan Murdoch"
To: "Sebastien Bihorel" ,
r-help@r-project.org
Sent: Tuesday, October 30, 2018 4:13:05 PM
Subject: Re: [R] Question about function scope
On 30/10/2018 3:56 PM, Sebastien Bihorel wrote:
> Hi,
>
> From the R user manual, I
Hi Sebastien,
I like Duncan's response. An alternative approach is to pass around
environments, as in the following:
bar1 <- function(e) {
e$x <- e$y <- e$z <- 1
cat(sprintf('bar1: x=%d, y=%d, z=%d\n', e$x, e$y, e$z))
}
bar2 <- function(e) {
e$x <- e$y <- e$z <- 2
cat(sprintf('bar2: x=%d,
On 30/10/2018 3:56 PM, Sebastien Bihorel wrote:
Hi,
From the R user manual, I have a basic understanding of the scope of function
evaluation but have a harder time understanding how to mess with environments.
My problem can be summarized by the code shown at the bottom:
- the foo function per
Hi,
>From the R user manual, I have a basic understanding of the scope of function
>evaluation but have a harder time understanding how to mess with environments.
My problem can be summarized by the code shown at the bottom:
- the foo function performs some steps including the assignment of defa
9 matches
Mail list logo