【发布时间】:2020-09-27 05:08:24
【问题描述】:
有人将 R 中的函数转换为字符串或文本对象吗?
假设对于一个简单的平方函数,我想转换为文本文件:
sqfxn <- function(x){
# Get square of x
# Expect:
# > sqfxn(2)
# > 4
output <- x^2
return(output)
}
是否有将sqfxn()转换为字符串对象的函数?
fxn_to_text <- function(x){
# convert function x to text
}
这将导致:
> txt_fxn <- fxn_to_txt(sqfxn)
> print(txt_fxn)
> "function(x){
# Get square of x
# Expect:
# > sqfxn(2)
# > 4
output <- x^2
return(output)
}"
谢谢!
【问题讨论】:
-
看到这实际上并不完全是重复的,因为用户希望将 cmets 保留在函数中。但不知道如何重新打开。
标签: r string function text tostring