获取作为参数传递给函数的函数名称

时间:2018-10-29 13:30:17

标签: r function functional-programming namespaces substitution

背景

函数作为参数传递给函数。问题涉及:

  • 获取该函数的名称作为字符串,以方便后续操作
  • 在被称为包的程序包中找到该功能
  • 了解 :: ::: 通话

示例

函数fun_tst在x上执行函数FUN

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

问题

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)
  

paste(“ Executing function”,FUN)中的错误:无法强制输入   “内置”到“字符”类型的向量


尝试

1)

有趣的是,print可以处理FUN对象,但结果返回函数主体。

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

几乎在那儿,但是当与 :: 一起使用时,看起来就像一团糟:

>> 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 ..

所需结果

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

2 个答案:

答案 0 :(得分:3)

第二次尝试就快到了(使用(Get-CimInstance -Query "select Name, PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor") | foreach-object { write-host "$($_.Name): $($_.PercentProcessorTime)" }; )。问题来自R将while ($true) { $cores = (Get-CimInstance -Query "select Name, PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor") $cores | foreach-object { write-host "$($_.Name): $($_.PercentProcessorTime)" }; Start-Sleep -m 1000 [System.Console]::Clear() } 对象转换为字符的方式:

substitute

鉴于此,language如此处理就不足为奇了。我将通过分别处理两种情况来解决此问题:

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

这适用于您的两个示例:

paste

但是,按照书面形式,它将认为全局环境中的所有功能都来自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)) } ,即使它们是用户定义的或通过> 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 ... 调用而引入的。如果这是您的用例,请不要明确地说“来自软件包库”。

答案 1 :(得分:3)

如果您使用deparse()substitute,将获得所需的输出,请参见类似的文章,将变量名传递给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 ...

如果您希望将消息作为字符向量,请将message替换为print