I'm looking to assign a data.frame
to the global environment so that the name of the data.frame
is the same as the one passed into a function I create. The typical way to do this is using assign(arg_name, x, envir = .GlobalEnv)
:
my_fun <- function(x) {
arg_name <- deparse(substitute(x))
print(arg_name)
x[["newvariable"]] <- 1
assign(arg_name, x, envir = .GlobalEnv)
}
my_fun(mtcars)
which works but I'm trying to do the short hand version using "super-assignment": <<-
. I haven't been able to make it work as it just returns a data.frame
called arg_name
rather than mtcars
:
my_fun2 <- function(x) {
arg_name <- deparse(substitute(x))
print(arg_name)
x[["newvariable"]] <- 1
arg_name <<- x
}
my_fun2(mtcars)
Any suggestions? Also, is deparse(substitute(x))
still the best (shortest) way to extract the name of the data.frame
for this type of case?
Thanks