From 2af7b025387e353e29adddd90ef26e7c4ecff2dc Mon Sep 17 00:00:00 2001 From: Yunuuuu Date: Sun, 22 Sep 2024 18:26:53 +0800 Subject: [PATCH] add `body_append()` --- R/utils.R | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/R/utils.R b/R/utils.R index e50dce4d..f97bfe5c 100644 --- a/R/utils.R +++ b/R/utils.R @@ -5,6 +5,17 @@ #' @importFrom utils packageName pkg_nm <- function() packageName(topenv(environment())) +#' @param ans Whether to assign the final results into the 'ans' variable. +#' @noRd +body_append <- function(fn, ..., ans = TRUE) { + args <- rlang::fn_fmls(fn) + body <- rlang::fn_body(fn) + body <- as.list(body) + if (ans) body[[length(body)]] <- rlang::expr(ans <- !!body[[length(body)]]) + body <- as.call(c(body, rlang::enexprs(...))) + rlang::new_function(args, body) +} + #' Read Example Data #' #' This function reads example data from the file. If no file is specified, it