Decompose and Recompose Functions
remotes::install_github("sckott/astr")
library("astr")
a simple function
foo <- function(x) {
x + 1
}
foo(5)
#> [1] 6
decompose the function
df <- ast_decompose(foo)
df
#> { fxn: foo }
#> function (x)
#> {
#> x + 1
#> }
class(df)
#> [1] "ast" "data.frame"
modify an aspect of the function
out <- ast_modify(df, from = "+", to = "-")
out
#> { fxn: foo }
#> function (x)
#> {
#> x - 1
#> }
ast_recompose(out)
#> [1] "function (x)\n{\n x - 1\n}"
ex <- ast_recompose(out, as_expr = TRUE)
eval(ex)(5)
#> [1] 4
astr
in R doing citation(package = 'astr')
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.