Not sure if this is what you're after. But, you can assign values to that environment with $
.
my_fun <- function(in.env) {
# you may want to check if input argument is an environment
# do your computations
set.seed(45)
x <- sample(10)
y <- runif(10)
in.env$val <- sum(x*y)
}
my_fun(my.env <- new.env())
ls(my.env)
[1] "val"
my.env$val
# [1] 22.30493
Alternatively, you can also use assign
as follows:
my_fun <- function(in.env) {
# you may want to check if input argument is an environment
# do your computations
set.seed(45)
x <- sample(10)
y <- runif(10)
assign("val", sum(x*y), envir=in.env)
}
# assign to global environment
my_fun(globalenv())
> val
# [1] 22.30493
# assign to local environment, say, v
v <- new.env()
my_fun(v)
> v$val
# [1] 22.30493