diff --git a/NAMESPACE b/NAMESPACE index 6f06d1d3..96f05c03 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -16,6 +16,7 @@ export(add_js_input_binding) export(add_js_output_binding) export(add_module) export(add_partial_html_template) +export(add_r6) export(add_resource_path) export(add_rstudioconnect_file) export(add_sass_file) diff --git a/R/add_r_files.R b/R/add_r_files.R index eb8a42af..3668e294 100644 --- a/R/add_r_files.R +++ b/R/add_r_files.R @@ -139,6 +139,26 @@ add_utils <- function( ) } +#' @rdname file_creation +#' @export +add_r6 <- function( + name, + module = NULL, + pkg = get_golem_wd(), + open = TRUE, + dir_create = TRUE, + with_test = FALSE +) { + add_r_files( + name, + module, + ext = "class", + pkg = pkg, + open = open, + dir_create = dir_create, + with_test = with_test + ) +} #' Append roxygen comments to `fct_` and `utils_` files #' #' This function add boilerplate roxygen comments @@ -164,16 +184,21 @@ append_roxygen_comment <- function( if (ext == "utils") { file_type <- "utility" - } else { + } else if (ext == "fct") { file_type <- "function" + } else { + ext <- paste(ext, "generator") + file_type <- "R6" } write_there(sprintf("#' %s ", name)) write_there("#'") write_there(sprintf("#' @description A %s function", ext)) write_there("#'") - write_there(sprintf("#' @return The return value, if any, from executing the %s.", file_type)) - write_there("#'") + if (!(file_type == "R6")) { + write_there(sprintf("#' @return The return value, if any, from executing the %s.", file_type)) + write_there("#'") + } if (export) { write_there("#' @export") } else { @@ -183,4 +208,11 @@ append_roxygen_comment <- function( write_there(paste(name, "<- function() {")) write_there("}") } + if (file_type == "R6") { + write_there(paste0(name, " <- R6::R6Class(")) + write_there(paste0(" classname = '", name, "',")) + write_there(" public = list(") + write_there(" )") + write_there(")") + } } diff --git a/R/modules_fn.R b/R/modules_fn.R index 13b3293b..924fd3bb 100644 --- a/R/modules_fn.R +++ b/R/modules_fn.R @@ -10,6 +10,7 @@ #' @param dir_create Creates the directory if it doesn't exist, default is `TRUE`. #' @param fct If specified, creates a `mod_fct` file. #' @param utils If specified, creates a `mod_utils` file. +#' @param r6 If specified, creates a `mod_class` file. #' @param js,js_handler If specified, creates a module related JavaScript file. #' @param export Should the module be exported? Default is `FALSE`. #' @param module_template Function that serves as a module template. @@ -31,6 +32,7 @@ add_module <- function( dir_create = TRUE, fct = NULL, utils = NULL, + r6 = NULL, js = NULL, js_handler = NULL, export = FALSE, @@ -125,6 +127,10 @@ add_module <- function( ) } + if (!is.null(r6)) { + add_r6(r6, module = name, open = open) + } + if (with_test) { use_module_test( name = name, diff --git a/man/add_module.Rd b/man/add_module.Rd index d98be9b6..d9efd621 100644 --- a/man/add_module.Rd +++ b/man/add_module.Rd @@ -11,6 +11,7 @@ add_module( dir_create = TRUE, fct = NULL, utils = NULL, + r6 = NULL, js = NULL, js_handler = NULL, export = FALSE, @@ -32,6 +33,8 @@ add_module( \item{utils}{If specified, creates a \code{mod_utils} file.} +\item{r6}{If specified, creates a \code{mod_class} file.} + \item{js, js_handler}{If specified, creates a module related JavaScript file.} \item{export}{Should the module be exported? Default is \code{FALSE}.} diff --git a/man/file_creation.Rd b/man/file_creation.Rd index f27d7fbf..acebcacf 100644 --- a/man/file_creation.Rd +++ b/man/file_creation.Rd @@ -3,6 +3,7 @@ \name{add_fct} \alias{add_fct} \alias{add_utils} +\alias{add_r6} \title{Add fct_ and utils_ files} \usage{ add_fct( @@ -22,6 +23,15 @@ add_utils( dir_create = TRUE, with_test = FALSE ) + +add_r6( + name, + module = NULL, + pkg = get_golem_wd(), + open = TRUE, + dir_create = TRUE, + with_test = FALSE +) } \arguments{ \item{name}{The name of the file}