I’m trying to use assign values in an object in a list. What I want to do is change some elements. For example:
x <- list()
x$test <- 1
assign("x$test", 2)
x$test == 1
[1] TRUE
Any thoughts? I need to use assign because I am building a function which will take the names of the objects within the list (x) as inputs.
Looks like you’re out of luck. From the help file:
If you’re passing
names(x)as input, couldn’t you use:Also, are you intending for your function to modify some global variable? e.g.:
Because this won’t work (scope problems). You can make it work with a bit of footwork (
<<-), but this is generally considered bad practice as it’s easy to intrtoduce unintentional bugs into your code.If you could give an example of why you want this function/what purpose it will serve, we could help you find an alternative solution.