fn_body() is a simple wrapper around base::body(). It always returns a { expression and throws an error when the input is a primitive function (whereas body() returns NULL). The setter version preserves attributes, unlike body<-.

fn_body(fn = caller_fn())

fn_body(fn) <- value

Arguments

fn

A function. It is lookep up in the calling frame if not supplied.

value

New formals or formals names for fn.

Examples

# fn_body() is like body() but always returns a block: fn <- function() do() body(fn)
#> do()
fn_body(fn)
#> { #> do() #> }
# It also throws an error when used on a primitive function: try(fn_body(base::list))
#> Error : `fn` is not a closure #> Backtrace: #> █ #> 1. ├─base::tryCatch(...) #> 2. │ └─base:::tryCatchList(expr, classes, parentenv, handlers) #> 3. │ ├─base:::tryCatchOne(...) #> 4. │ │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> 5. │ └─base:::tryCatchList(expr, names[-nh], parentenv, handlers[-nh]) #> 6. │ └─base:::tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> 7. │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> 8. ├─base::withCallingHandlers(...) #> 9. ├─base::saveRDS(...) #> 10. ├─base::do.call(...) #> 11. ├─(function (what, args, quote = FALSE, envir = parent.frame()) ... #> 12. └─(function (..., crayon_enabled, crayon_colors, pkgdown_internet) ... #> 13. └─pkgdown::build_site(...) #> 14. └─pkgdown:::build_site_local(...) #> 15. └─pkgdown::build_reference(...) #> 16. └─purrr::map(...) #> 17. └─pkgdown:::.f(.x[[i]], ...) #> 18. └─pkgdown:::data_reference_topic(...) #> 19. ├─pkgdown:::as_data(...) #> 20. └─pkgdown:::as_data.tag_examples(...) #> 21. └─purrr::pmap_chr(...) #> 22. └─pkgdown:::.f(...) #> 23. ├─withr::with_options(...) #> 24. │ └─base::force(code) #> 25. └─evaluate::evaluate(code, env, new_device = TRUE) #> 26. └─evaluate:::evaluate_call(...) #> 27. ├─evaluate:::timing_fn(...) #> 28. ├─evaluate:::handle(...) #> 29. │ └─base::try(f, silent = TRUE) #> 30. │ └─base::tryCatch(...) #> 31. │ └─base:::tryCatchList(expr, classes, parentenv, handlers) #> 32. │ └─base:::tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> 33. │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> 34. ├─base::withCallingHandlers(...) #> 35. ├─base::withVisible(eval(expr, envir, enclos)) #> 36. └─base::eval(expr, envir, enclos) #> 37. └─base::eval(expr, envir, enclos) #> 38. ├─base::try(fn_body(base::list)) #> 39. │ └─base::tryCatch(...) #> 40. │ └─base:::tryCatchList(expr, classes, parentenv, handlers) #> 41. │ └─base:::tryCatchOne(expr, names, parentenv, handlers[[1L]]) #> 42. │ └─base:::doTryCatch(return(expr), name, parentenv, handler) #> 43. └─rlang::fn_body(base::list)