Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting name of a function passed as an argument to a function

Background

Function is passed as an argument to a function. The problem pertains to:

  • getting the name of that function as a string for convenient subsequent manipulation
  • locating that function within the package from which is called
  • understanding :: and ::: calls

Example

Function fun_tst executes function FUN on x:

fun_tst <- function(x = 1:100, FUN = mean) {
    return(FUN(x))
}

mean

fun_tst()
# [1] 50.5

sum

fun_tst(x = 1:1e3, FUN = sum)
# [1] 500500

Problem

fun_tst <- function(x = 1:100, FUN = mean) {
    msg <- paste("Executing function", FUN)
    print(msg)
    return(FUN(x))
} 


fun_tst(x = 1:1e3, FUN = sum)

Error in paste("Executing function", FUN) : cannot coerce type 'builtin' to vector of type 'character'


Attempts

1)

Interestingly, print can handle FUN object but results return function body.

fun_tst <- function(x = 1:100, FUN = mean) {
    print(FUN)
    return(FUN(x))
} 


fun_tst(x = 1:1e3, FUN = sum)

function (..., na.rm = FALSE) .Primitive("sum") [1] 500500

2) subsitute

fun_tst <- function(x = 1:100, FUN = mean) {
    fun_name <- substitute(FUN)
    msg <- paste("Executing function", fun_name, collapse = " ")
    print(msg)
    return(FUN(x))
} 


fun_tst(x = 1:1e3, FUN = sum)

>> fun_tst(x = 1:1e3, FUN = sum)
[1] "Executing function sum"
[1] 500500

Almost there but it looks like a total mess when used with :: as in:

>> fun_tst(x = 1:1e3, FUN = dplyr::glimpse)
[1] "Executing function :: Executing function dplyr Executing function glimpse"
 int [1:1000] 1 2 3 4 5 6 7 8 9 10 ..

Desired results

fun_tst(x = 1:1e3, FUN = dplyr::glimpse)
# Executing function glimpse from package dplyr
int [1:1000] 1 2 3 4 5 6 7 8 9 10 ...

fun_tst(x = 1:1e3, FUN = sum)
# Executing function sum from package base
like image 401
Konrad Avatar asked Mar 06 '23 04:03

Konrad


2 Answers

You're almost there with your second try (using substitute). The problem comes from the way R converts language objects to character:

> as.character(substitute(dplyr::glimpse))
[1] "::"      "dplyr"   "glimpse" 

Given this, it's not surprising that paste mangles it that way. I would fix this just by handling the two cases separately:

fun_tst <- function(x = 1:100, FUN = mean) {
  fun_name <- substitute(FUN)
  if (length(fun_name) == 1) {
    msg <- paste("Executing function", fun_name, "from package base")
  } else {
    msg <- paste("Executing function", fun_name[3], "from package", fun_name[2])
  }
  print(msg)
  return(FUN(x))
} 

This works on both of your examples:

> fun_tst(x = 1:1e3, FUN = sum)
[1] "Executing function sum from package base"
[1] 500500
> fun_tst(x = 1:1e3, FUN = dplyr::glimpse)
[1] "Executing function glimpse from package dplyr"
 int [1:1000] 1 2 3 4 5 6 7 8 9 10 ...

However, as written, it will think all functions in the global environment are from base, even if they're user-defined or introduced with a library call. If this is your use case, don't explicitly say "from package base".

like image 90
Frank Avatar answered Mar 07 '23 21:03

Frank


If you use deparse() and substitute you'll get the desired output, see a similar post on passing variable names to plot(), https://stackoverflow.com/a/9666650/1993932.

fun_tst <- function(x = 1:100, FUN = mean) {
  message(paste("Executing function",deparse(substitute(FUN))))
  return((FUN(x)))
}
> fun_tst(x = 1:1e3, FUN = sum)
Executing function sum
[1] 500500

> fun_tst(x = 1:1e3, FUN = dplyr::glimpse)
Executing function dplyr::glimpse
 int [1:1000] 1 2 3 4 5 6 7 8 9 10 ...

If you rather want the message as a character vector, replace message with print.

like image 27
nadizan Avatar answered Mar 07 '23 20:03

nadizan