I have a function
Trace.repair(dat, row.names)
While performing this function i change values in my table dat. Is there a way to update dat without doing the obvious
dat<-Trace.repair(dat, row.names){blah; blah; print(dat)}
I have a function
Trace.repair(dat, row.names)
While performing this function i change values in my table dat. Is there a way to update dat without doing the obvious
dat<-Trace.repair(dat, row.names){blah; blah; print(dat)}
use assign
. Something like this:
Trace.repair <- function(x) {
# do tricks with x
assign("dat", x, envir=parent.frame())
}
This assumes that you always want to change variable "dat" in your calling environment. If you want the outcome to be assigned to the variable with the same name as you call it, like Trace.repair(another_dat)
, you can recover the variable name by as.character(sys.call()[2])
and replace "dat"
in the code with that:
Trace.repair <- function(x) {
# do tricks with x
assign(as.character(sys.call()[2]), x, envir=parent.frame())
}
However, if you don't call it with explicit variable name but with something like Trace.repair(-dat)
, then you get a variable named "-dat" in your environment.