Hi, it seems to me that lambda functions (and single line functions without curly brackets) are not supported by ast2ast::translate()
.
f1 <- function(x) sin(x)
f2 <- function(x) {sin(x)}
ast2ast::translate(f1)
#> Error in `purrr::map_if()`:
#> ! `.x` must be a vector, not a symbol.
#> Backtrace:
#> ▆
#> 1. └─ast2ast::translate(f1)
#> 2. └─ast2ast:::compiler_a2a(...)
#> 3. └─a$build_own_SEXP(verbose, reference = reference)
#> 4. └─self$ast2call()
#> 5. └─self$get_calls(self$ast[[i]])
#> 6. └─purrr::map_if(code, is.list, self$get_calls)
#> 7. └─purrr:::where_if(.x, .p)
#> 8. └─purrr:::map_(.x, .p, ..., .type = "logical", .purrr_error_call = .purrr_error_call)
#> 9. └─vctrs::vec_assert(.x, arg = ".x", call = .purrr_error_call)
#> 10. └─vctrs:::stop_scalar_type(x, arg, call = call)
#> 11. └─vctrs:::stop_vctrs(...)
#> 12. └─rlang::abort(message, class = c(class, "vctrs_error"), ..., call = call)
ast2ast::translate(f2)
#> function (xSEXP)
#> .Call(<pointer: 0x7fee734d82a0>, xSEXP)
#> <environment: 0x5621d09c6428>
ast2ast::translate(function(x) {sin(x)})
#> g++ -std=gnu++17 -I"/usr/share/R/include" -DNDEBUG -I../inst/include -fopenmp -I"/home/fangzhou/R/x86_64-pc-linux-gnu-library/4.3/Rcpp/include" -I"/home/fangzhou/R/x86_64-pc-linux-gnu-library/4.3/ast2ast/include" -I"/home/fangzhou/R/x86_64-pc-linux-gnu-library/4.3/RcppArmadillo/include" -I"/tmp/Rtmprk7h9f/sourceCpp-x86_64-pc-linux-gnu-1.0.10" -DRFCT -fpic -g -O2 -ffile-prefix-map=/build/r-base-sYMOIw/r-base-4.3.1=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -c file46304a55efdd.cpp -o file46304a55efdd.o
#> [1] "Sorry compilation failed!"
#> NULL
From this example, only the second translation works. The first (without bracket) and the third (lambda function) failed.
if (deparse(body(f))[[1]] != "{") {
body(f) <- parse(text = paste0("{", deparse(body(f)), "}"))[[1]]
}