I’m trying to find a way to use a hash map in R, and after some searching I get the R-environment.
But how can I iterate through all the items in an environment ?
When I run the following code, I was expecting output like this :
1
2
But I get two lines of NULL instead, how can I get what I want ?
map <- new.env(hash=T, parent=emptyenv())
assign('a', 1, map)
assign('b', 2, map)
for (v in ls(map)) {
print(map$v)
}
The use of “$” inside a function where it is desired to interpret the input is a common source of programming error. Use instead the form object[[value]] (without the quotes.)