Access local variable in global scope (outside function)

How to access local variable outside function ?

reprex:

x = 1
y <- function(a){
  x = x + 2
  z = x
  z
  }

y()
x
# x value is Unchanged 

How can access altered value outside function ?

You can explicitly return an execution or function environment, but it's not a very natural idiom in R. See the Function environments section in Advanced R.
https://adv-r.hadley.nz/environments.html#function-environments

2 Likes

If you do want to return both values, @AbhishekHP, one way would be to return a list. I've renamed some of your values just to be super clear about what is happening when:

# initial value
val <- 1

# function
y <- function(x){
  
  # add 2 to x
  a <- x + 2
  
  # square prev value
  b <- a^2
  
  # create list
  out <- list(
    intermediate_value = a,
    final_value = b
  )
  
  # return output
  return(out)
  
}

# example
y(val)
#> $intermediate_value
#> [1] 3
#> 
#> $final_value
#> [1] 9

Created on 2019-03-20 by the reprex package (v0.2.1)

2 Likes

How Mara mentioned it's not a very natural idiom in R, but if you are interested in environment access or creation etc. it's may a good point to start... :wink:

x=1
y <- function(){
  x <- x + 2
  z = x
  assign("x", z, envir = .GlobalEnv)
  return(z)
}

x
#> [1] 1
y()
#> [1] 3
x 
#> [1] 3

y()
#> [1] 5
x 
#> [1] 5

y2 <- function(x) {
  inner_y <- function(x) assign("Global.res", x^2, envir = .GlobalEnv)
  inner_y(x+1)
}

y2(2)
Global.res
#> [1] 9

y2(3)
Global.res
#> [1] 16

Created on 2019-03-20 by the reprex package (v0.2.1)

Best regards
Adam

1 Like

You probably want to avoid that in user-facing code. I'd be surprised if I ran y() and x changed in the GlobalEnv.

3 Likes

Agree completely with @mara - I've always viewed environment changes as something that falls into the "just because you can doesn't mean you should" category.

When I first picked up R, I was really frustrated with the fact that functions could only return one "thing". What I didn't really grasp then was that, once you get out of the scalar or data frame binary, there are lots of ways to flexibly return objects that provide predictable output. This was a big learning curve for me coming from Stata land, though.

2 Likes

I fully agree with @mara and @chris.prener! Sorry, I didn't mean to cause any confusion here. Object changes have to be avoid in your global environment. I was only concerned with the point that you have certain creative possibilities, e.g. to create a temporary environment next to the global environment, to assign variables etc.

It was more like a idea of motivation. It's still great to learn a programming language and especially looking outside the box. :slight_smile:

2 Likes

For sure, actually, a package I use all the time, nbastatR (not on CRAN – just basketball stats fun), gives you the option to assign data frames and dictionaries to the global env, and I use it all the time (since I know what I'm getting).

He has assign_to_environment as a parameter, so you can wrest back control if you want! :scream:

1 Like

Thanks Adam for simple solution

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.