I want to create a list x
in R, containing entries, say like red = 32, green = 34
and so on. Suppose I have the function func
which takes in x
and changes red = 32
to red = 23
. I then want to call func(x)
and have that modify x
, so of I then query x
I get the updated version. How do I get such an object in R?
Asked
Active
Viewed 397 times
0
-
You can do this easily. But you should not. What you describe is called a site effect and we are trying to avoid site effects in R. Learn functional programming instead. – Roland Jul 22 '20 at 10:13
-
R usually uses [call-by-value](https://stackoverflow.com/q/15759117/1412059). However, you can (using C/C++ code) also do call-by-reference. The data.table package is an example for this. It's also easy to implement with Rcpp (for pretty much any object). Is that what you are asking? – Roland Jul 22 '20 at 13:37
1 Answers
1
I think what you are asking for is this function :
wrong_func <- function(list_data, name, value) {
list_data[[name]] <<- value
}
list_data <- list(red = 32, green = 34)
list_data$red
#[1] 32
wrong_func(list_data, "red", 23)
list_data$red
#[1] 23
However, this is incorrect way to do this and should be avoided. Instead do :
right_func <- function(list_data, name, value) {
list_data[[name]] <- value
list_data
}
list_data <- list(red = 32, green = 34)
list_data$red
#[1] 32
list_data <- right_func(list_data, "red", 23)
list_data$red
#[1] 23

Ronak Shah
- 377,200
- 20
- 156
- 213
-
I should have been clearer. I'm not looking for the function. Suppose ```right_func``` is given as you have it. What kinds of objects ```list_data``` are mutable? If I know that calling```right_func(list_data)``` and then ```list_data$red``` gives 23, *what was* ```list_data```? – sasa nnjuj Jul 22 '20 at 10:35
-
@sasannjuj `list_data` is a `list`, which is a mutable list object in R. However, R is not a strictly typed language, so Ronak's solution would work if `list_data` was any class for which the subsetting operators `[[` and `$` are defined. – Allan Cameron Jul 22 '20 at 10:44
-
What I'd like to see is ```right_func(list_data)``` *modifying the value* of ```list_data```, not the assignment. I agree this is the correct and standard way of doing the same thing, but I'm asking about the kind of data structure on ```list_data``` permitting this. – sasa nnjuj Jul 22 '20 at 10:54
-
@sasannjuj Isn't that what `wrong_func(list_data, "red", 23)` already does? – Ronak Shah Jul 22 '20 at 11:13
-
Yes, ```wrong_func``` does do this. My question is however the following: given ```right_func``` as you write it, assign a data structure to ```x``` such that if I type ```right_func(x)``` and then ```x$red``` I get 23 and not 32. – sasa nnjuj Jul 22 '20 at 11:23
-
I don't understand your question. One function behaves according to the way you want, another does not. Then use the function which behaves according to your requirement or if name is the problem you can rename it. `right_func <- wrong_func` – Ronak Shah Jul 22 '20 at 11:29