Message-ID: <CDF73919-52A4-442D-8104-1E5F7E195081@posteo.ch>
Date: 2015-05-14T18:28:27Z
From: soeren.vogel at posteo.ch
Subject: Deparse substitute assign with list elements
Hello,
When I use function `foo` with list elements (example 2), it defines a new object named `b[[1]]`, which is not what I want. How can I change the function code to show the desired behaviour for all data structures passed to the function? Or is there a more appropriate way to sort of "pass by references" in a function?
Thanks
S?ren
<src>
bar <- function(x) {
return( x + 3 )
}
foo <- function(x, value) {
nm <- deparse(substitute(x))
tmp <- bar( value )
assign(nm, tmp, parent.frame())
}
# 1)
a <- NA
foo(a, 4)
a # 7, fine :-)
# 2)
b <- list(NA, NA)
foo(b[[1]], 4) # the first list item should be 7
b # wanted 7 but still list with two NAs :-(
</src>