[R] how to modify variables of another frame (but not global)

Uwe Ligges ligges at statistik.uni-dortmund.de
Tue Mar 23 12:59:08 CET 2004


Meinhard Ploner wrote:

> Hello!
> 
> Maybe "frame" is not the right term in this context.
> I explain my problem by example code:
> 
> fun2 <- function(objName, add) {
>     ## the object "objName" should be increased by "add",
>     ## but the evaluation should be done in the calling function (here: 
> fun1)
>     ##    ...... what's the right code??
> }
> 
> fun1 <- function() {
>     x <- 1
> 
>     fun2("x", 10)        ## should modify "x"
> 
>     ## now x should be 11, but only here and NOT globally!
>     ...
> }
> 
> 
> I would like to appreciate any solution!
> Thanks in advance
> 
> Meinhard Ploner
> 


fun2 <- function(objName, add) {
     x <- get(objName, pos = parent.frame()) + add
     assign(objName, x, pos = parent.frame())
}

fun1 <- function() {
     x <- 1
     fun2("x", 10)
     return(x)
}

fun1()
[1] 11


Uwe Ligges




More information about the R-help mailing list