如何将功能代码存储在列表中,作为字符串传递?

时间:2016-08-02 21:43:24

标签: r

我正在尝试将函数的源代码存储在列表中,但我想将函数名称作为字符串传递给列表。

这有效:

hello_world <- function() print("Hello World")
my_list <- list(func = hello_world)
my_list
# $func
# function () 
# print("Hello World")

但我希望能够将函数名称作为字符串传递。这当然只是给了我一个字符串:

my_list <- list(func = paste("hello", "world", sep = "_"))
my_list
# $func
# [1] "hello_world"

我试过了:

my_list <- list(func = as.formula(paste("hello", "world", sep = "_")))
  

错误:类型&#39;关闭的对象&#39;不是子集表格

my_list <- list(func = call(paste("hello", "world", sep = "_")))
my_list
# $func
# hello_world()

如果有人知道如何让这项工作成功,我会非常感激。

0 个答案:

没有答案