#' Create a function and test in current project
#' @param function_name Function name to be created
#' @param pkg_dir Project where function is going to be created
#' @export
create_function <- function(function_name = NULL, pkg_dir = NULL) {
if (is.null(function_name)) {
rlang::abort("Argument {function_name} should be set.")
}
if (function_name %in% c("")) {
rlang::abort("Argument {function_name} should not be am empty string.")
}
if (is.null(pkg_dir)) {
pkg_dir <- usethis::proj_get()
}
if (!fs::file_exists(pkg_dir)) {
rlang::abort(glue::glue("{pkg_dir} don't exists."))
}
function_file <- fs::path(pkg_dir, "R", function_name, ext = "R")
if (fs::file_exists(function_file)) {
rlang::inform(
glue::glue(
"{function_file} already exists. Opening it to edit."
)
)
}
# allow opening files in other project dir
usethis::with_project(
path = pkg_dir,
code = {
fs::dir_create(fs::path(pkg_dir, "R"))
usethis::use_r(name = function_name)
# BUG use_testthat() throwing errors because dev
# version is lower than 3 - in DESCRIPTION file
# usethis::use_testthat()
usethis::use_test(name = function_name)
},
force = TRUE
)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.