Skip to contents

Recompose an R function

Usage

ast_recompose(x, as_expr = FALSE)

Arguments

x

an object of class ast

as_expr

(logical) return the function as an expression? default: FALSE

Value

a function as a character string, or an expression if as_expr=TRUE

Examples

foo <- function(x) {
  x + 1
}
foo(5)
#> [1] 6

# decompose the function
df <- ast_decompose(foo)
#> Error in as_string(x): Can't convert a logical vector to a string.
df
#> function (x, df1, df2, ncp, log = FALSE) 
#> {
#>     if (missing(ncp)) 
#>         .Call(C_df, x, df1, df2, log)
#>     else .Call(C_dnf, x, df1, df2, ncp, log)
#> }
#> <bytecode: 0x564bf3967008>
#> <environment: namespace:stats>

# recompose the function
fun <- ast_recompose(df)
#> Error: x must be of class ast, data.frame
fun
#> Error: object 'fun' not found

parse(text = fun)
#> Error: object 'fun' not found
eval(parse(text = fun))
#> Error: object 'fun' not found
eval(parse(text = fun))(5)
#> Error: object 'fun' not found