I have a function who's task is to create a variable in the parent object. What I want is for the function to create the variable at the level at which it's called.
createVariable <- function(var.name, var.value) {
assign(var.name,var.value,envir=parent.frame())
}
# Works
testFunc <- function() {
createVariable("testVar","test")
print(testVar)
}
# Doesn't work
testFunc2 <- function() {
testFunc()
print(testVar)
}
> testFunc()
[1] "test"
> testFunc2()
[1] "test"
Error in print(testVar) : object 'testVar' not found
I'm wondering if there's any way to do this without creating the variable in the global environment scope.
Edit: Is there also a way to unit test that a variable has been created?