Home > Net >  access the assigned name in an R function
access the assigned name in an R function

Time:01-28

I wonder if R functions can know the name of the variable their result is assigned to.

e.g.

selfaware_squared <- function(x) {
  cat("Assigning square to variable name '", some_magic, "'.")
  return(x^2)
}

> a <- selfaware_squared(2)
Assigning square to variable name ' a '.

CodePudding user response:

sys.call is an option if you are using assign to do the assignment. It allows you to retrieve outer function calls from inner ones:

f <- function(x) {
  cl <- sys.call(-1L)
  if (!is.null(cl) && cl[[1L]] == "assign") {
    nm <- match.call(assign, cl)$x
    if (is.character(nm) && length(nm) > 0L) {
      cat("assigning square of", sQuote(deparse(substitute(x))),
          "to variable", sQuote(nm[1L]), "\n")
    }
  }
  x * x
}

assign("y1", f(10)) # assigning square of ‘10’ to variable ‘y1’ 
y1
## [1] 100

assign("y2", f(1:10)) # assigning square of ‘1:10’ to variable ‘y2’ 
y2
## [1]   1   4   9  16  25  36  49  64  81 100

assign("y3", f(pi)) # assigning square of ‘pi’ to variable ‘y3’
y3
## [1] 9.869604

assign("y4", f(pi^2)) # assigning square of ‘pi^2’ to variable ‘y4’
y4
## [1] 97.40909

Unfortunately, this approach doesn't extend to <-, because it is a primitive not a closure (compare typeof(`<-`) and typeof(assign)). Its calls don't appear on the call stack. Hence y <- f(10) will assign the value 100 to y without generating any output.

CodePudding user response:

I think the only way the function could know the name of where it's output is being assigned is if it's provided as an input to the function. So for example you could make that an input and use assign() inside the function (although this should probably done with great care).

selfaware_squared <- function(x, nm) {
  cat("Assigning square to variable name '", nm, "'.")
  assign(x = nm, value = x^2, envir = globalenv())
}

selfaware_squared(2, "a")
#> Assigning square to variable name ' a '.

a
#> [1] 4

Created on 2022-01-27 by the reprex package (v2.0.1)

Also see discussion of this topic here.

  •  Tags:  
  • Related