Reputation: 720
I would like to convert a function object into a string
fn=function(x)
{
return(2*x)
}
my_convert_function(fn)
# would give
#"fn=function(x)
#{
# return(2*x)
#}"
Is it possible to do it ? Thank you for your ideas
Upvotes: 4
Views: 277
Reputation: 1624
As of R 4.0, there is also deparse1()
. This version of deparse()
returns a single string directly (if that is what you are looking for):
fn=function(x)
{
return(2*x)
}
deparse1(fn)
#> [1] "function (x) { return(2 * x) }"
Upvotes: 0
Reputation: 5169
deparse(fn)
[1] "function (x) " "{" " return(2 * x)" "}"
Upvotes: 5