[英]as.character usage on functions
I stumbled uppon one problem: 我偶然发现了一个问题:
temp.fun <- function() {}
as.character(temp.fun)
yields an error. 产生错误。 I undersand, why it is not possible to "convert" a function into a character.
我说,为什么不可能将一个函数“转换”为一个字符。 Question is, what properties to add to a function so that the method
as.character
returns a string defined by myself? 问题是,要添加到函数中的属性是什么,以便方法
as.character
返回由我自己定义的字符串?
Thanks a lot! 非常感谢!
deparse
can help: deparse
可以帮助:
> deparse( temp.fun )
[1] "function () " "{" "}"
Going further with the details of your comment, what you can do is create a class that derives function
and pass this instead of the function. 进一步了解评论的细节,你可以做的是创建一个派生
function
的类,并传递它而不是函数。
setClass( "myFunction", contains = "function" )
setMethod( "as.character", "myFunction", function(x, ...){
deparse( unclass( x ) )
} )
So that when you pass a function to the third party package, you pass a myFunction
instead: 因此,当您将函数传递给第三方软件包时,您将传递
myFunction
:
f <- new( "myFunction", function(){} )
as.character(f)
# [1] "function () " "{" "}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.