Skip to contents

R-check codecov.io

Decompose and Recompose Functions

Install

remotes::install_github("sckott/astr")

decompose

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

modify an aspect of the function

out <- ast_modify(df, from = "+", to = "-")
out
#> { fxn: foo }
#> function (x)
#> {
#>     x - 1
#> }

recompose

ast_recompose(out)
#> [1] "function (x)\n{\n    x - 1\n}"
ex <- ast_recompose(out, as_expr = TRUE)
eval(ex)(5)
#> [1] 4

Meta

  • Please report any issues or bugs.
  • License: MIT
  • Get citation information for astr in R doing citation(package = 'astr')
  • Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.