Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
希望这不应该太复杂......但我有以下字符串x,并希望它以编程方式作为一个函数获取,以便我可以输入
x
HW.R()
生成 100 个随机法线....即我需要执行哪些操作x才能发生这种情况?
下面是生成的代码x
require(RCurl) x <- base64Decode("SFcuUiA8LSBmdW5jdGlvbigpewogIHJub3JtKDEwMCkKfQ==\n") x [1] "HW.R <- function(){\n rnorm(100)\n}"
正如您所怀疑的,它一点也不复杂。这应该可以完成工作:
x <- "HW.R <- function(){\n rnorm(100)\n}" eval(parse(text = x)) HW.R # function(){ # rnorm(100) # }
或者,您可以使用source(),只要您首先“包装”x在 a 中textConnection:
source()
textConnection
source(file = textConnection(x)) length(HW.R()) # [1] 100