diff --git a/DESCRIPTION b/DESCRIPTION index ee1436c6..c3b6aad8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: fusen Title: Build a Package from Rmarkdown File -Version: 0.3.0.9000 +Version: 0.4.0 Authors@R: c( person("Sebastien", "Rochette", , "sebastien@thinkr.fr", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-1565-9313")), diff --git a/NEWS.md b/NEWS.md index 73e59919..6051ca32 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,15 +1,22 @@ -# fusen 0.3.0.9000 +# fusen 0.4.0 + +## New features * `inflate()` the current opened flat file if `flat_file` is empty (#138) +* Add rmarkdown template for additional flat file for RStudio +* Add wrappers around `add_flat_template()` for lazy devs: `add_additional()`, `add_full()`, `add_minimal()` +* Show "flat_template" origin of files generated by {fusen} (@ +ALanguillaume) +* Allow `inflate(vignette_name = c("Super title" = "01-Super Slug"))` for nice Title different from vignette Entry (#87) +* Get the author and date from flat file to the vignette (#129) + +## Bug fixes + * Read DESCRIPTION file for package name when available (#144 @VincentGuyader) * Read `nyc_squirrels` with encoding to avoid encoding problems with `use_data()` * Allow flat files with `tests` only -* Add rmarkdown template for additional flat file for RStudio * Extract yaml metadata from flat file, like author or date to include in the inflated vignette -* Add wrappers around `add_flat_template()` for lazy devs: `add_additional()`, `add_full()`, `add_minimal()` * Simplify "flat_teaching" with a unique simple function -* Show "flat_template" origin of files generated by {fusen} (@ -ALanguillaume) * Fix `asciify_name()` to account for diacritics (@ ALanguillaume) * Improve template 'full' for internal data use @@ -30,8 +37,8 @@ ALanguillaume) * Avoid creating vignette with `inflate(vignette_name = NA)` * Decide whether or not to open vignette when inflate with `inflate(open_vignette = FALSE)` * Improve documentation included in flat templates to reflect changes in using dev_history file -* Add Rstudio Addin to insert a new flat template -* Add Rstudio Addin to insert chunks for new function (@ColinFay) +* Add RStudio Addin to insert a new flat template +* Add RStudio Addin to insert chunks for new function (@ColinFay) * Deal with `\dontrun{}` in example chunks * Allow short names for chunks: dev, fun, ex, test * `create_fusen()` to create a {fusen} project from command line or with RStudio new project (@ALanguillaume) @@ -54,7 +61,7 @@ ALanguillaume) * Fix inflate with empty functions chunks * Fix filename to inflate in templates with new calls of `add_dev_history()` (@Cervangirard) * Default vignette name is now "Get started" creating "vignettes/get-started.Rmd" -* All open files are saved when using `inflate()` where {rstudioapi} works +* All open files are saved when using `inflate()` where {RStudioapi} works * Ask to restart RStudio after first inflate # fusen 0.2.4 diff --git a/R/add_dev_history.R b/R/add_dev_history.R index bc19199a..5517cbdd 100644 --- a/R/add_dev_history.R +++ b/R/add_dev_history.R @@ -32,8 +32,7 @@ add_dev_history <- function(pkg = ".", overwrite = FALSE, open = TRUE, dev_dir = "dev", name = c("full", "minimal", "additional", "teaching")) { - - .Deprecated('add_flat_template', package = 'fusen', old = 'add_dev_history') + .Deprecated("add_flat_template", package = "fusen", old = "add_dev_history") name <- match.arg(name) diff --git a/R/add_flat_template.R b/R/add_flat_template.R index 8a95aa4e..ccfc5af9 100644 --- a/R/add_flat_template.R +++ b/R/add_flat_template.R @@ -2,53 +2,53 @@ #' @rdname add_flat_template #' @export -add_additional <- function( - pkg = ".", - dev_dir = "dev", - flat_name = "additional", - overwrite = FALSE, - open = TRUE) { +add_additional <- function(pkg = ".", + dev_dir = "dev", + flat_name = "additional", + overwrite = FALSE, + open = TRUE) { add_flat_template( template = "additional", - pkg = pkg, + pkg = pkg, dev_dir = dev_dir, flat_name = flat_name, overwrite = overwrite, - open = open) + open = open + ) } #' @rdname add_flat_template #' @export -add_minimal <- function( - pkg = ".", - dev_dir = "dev", - flat_name = "minimal", - overwrite = FALSE, - open = TRUE) { +add_minimal <- function(pkg = ".", + dev_dir = "dev", + flat_name = "minimal", + overwrite = FALSE, + open = TRUE) { add_flat_template( template = "minimal", - pkg = pkg, + pkg = pkg, dev_dir = dev_dir, flat_name = flat_name, overwrite = overwrite, - open = open) + open = open + ) } #' @rdname add_flat_template #' @export -add_full <- function( - pkg = ".", - dev_dir = "dev", - flat_name = "full", - overwrite = FALSE, - open = TRUE) { +add_full <- function(pkg = ".", + dev_dir = "dev", + flat_name = "full", + overwrite = FALSE, + open = TRUE) { add_flat_template( template = "full", - pkg = pkg, + pkg = pkg, dev_dir = dev_dir, flat_name = flat_name, overwrite = overwrite, - open = open) + open = open + ) } #' Add flat Rmd file that drives package development @@ -59,7 +59,7 @@ add_full <- function( #' @param open Logical. Whether to open file after creation #' @param dev_dir Name of directory for development Rmarkdown files. Default to "dev". #' @param flat_name Name of the file to write in dev. -#' Use the name of the main function of your template to get chunks pre-filled with this function name. +#' Use the name of the main function of your template to get chunks pre-filled with this function name. #' #' @importFrom tools file_path_sans_ext #' @details @@ -76,7 +76,7 @@ add_full <- function( #' #' Abbreviated names can also be used for the different templates: #' "add" for additional, "min" for minimal, "teach" for teaching, "dev" for "dev_history". -#' +#' #' `add_additional()`, `add_minimal()`, `add_full()` are wrapper around `add_flat_template("additional")`, ... #' However, `add_dev_history()` is a deprecated function from a previous version. #' @@ -89,62 +89,67 @@ add_full <- function( #' # Create a new project #' dummypackage <- tempfile("dummypackage") #' dir.create(dummypackage) -#' +#' #' # Add #' add_flat_template(template = "teaching", pkg = dummypackage) #' # Delete dummy package #' unlink(dummypackage, recursive = TRUE) -#' +#' #' # For classical use in your package #' \dontrun{ #' # first time ever using 'fusen' -#' add_flat_template("full") -#' +#' add_flat_template("full") +#' #' # first time in your new package #' add_flat_template("minimal") -#' +#' #' # add new flat file for new functions #' add_flat_template("add") -#' +#' #' # add new flat template for teaching (a reduced full template) #' add_flat_template("teaching") -#' #'} -add_flat_template <- function( - template = c("full", "minimal", "additional", "teaching", "dev_history"), - pkg = ".", - dev_dir = "dev", - flat_name = template, - overwrite = FALSE, - open = TRUE) { - +#' #' +#' } +add_flat_template <- function(template = c("full", "minimal", "additional", "teaching", "dev_history"), + pkg = ".", + dev_dir = "dev", + flat_name = template, + overwrite = FALSE, + open = TRUE) { project_name <- get_pkg_name(pkg = pkg) - + if (project_name != asciify_name(project_name, to_pkg = TRUE)) { - stop("Please rename your project/directory with: `", asciify_name(project_name, to_pkg = TRUE), - "` as a package name should only contain letters, numbers and dots.") + stop( + "Please rename your project/directory with: `", asciify_name(project_name, to_pkg = TRUE), + "` as a package name should only contain letters, numbers and dots." + ) } - + template <- match.arg(template) - if (!template %in% c("full", "teaching", "dev_history") - & !flat_name %in% c("minimal", "additional")) { + if (!template %in% c("full", "teaching", "dev_history") & + !flat_name %in% c("minimal", "additional")) { fun_name <- gsub("-", "_", asciify_name(flat_name)) } else { fun_name <- NA } - flat_name <- paste0("flat_", - asciify_name(gsub("[.]Rmd$", "", flat_name[1])), ".Rmd") - + flat_name <- paste0( + "flat_", + asciify_name(gsub("[.]Rmd$", "", flat_name[1])), ".Rmd" + ) + pkg <- normalizePath(pkg) full_dev_dir <- file.path(pkg, dev_dir) - if (!dir.exists(full_dev_dir)) {dir.create(full_dev_dir)} - dev_file_path <- file.path(full_dev_dir, flat_name) #"dev_history.Rmd") - + if (!dir.exists(full_dev_dir)) { + dir.create(full_dev_dir) + } + dev_file_path <- file.path(full_dev_dir, flat_name) # "dev_history.Rmd") + # Which template ---- if (template == "dev_history") { dev_file_path <- character(0) } else { template_file <- system.file(paste0("flat-template-", template, ".Rmd"), package = "fusen") - + if (file.exists(dev_file_path) & overwrite == FALSE) { n <- length(list.files(full_dev_dir, pattern = "^flat_.*[.]Rmd")) dev_file_path <- file.path(full_dev_dir, paste0(file_path_sans_ext(flat_name), "_", n + 1, ".Rmd")) @@ -155,40 +160,50 @@ add_flat_template <- function( ) } dev_name <- basename(dev_file_path) - + # Change lines asking for pkg name lines_template <- readLines(template_file) - + lines_template[grepl("", lines_template)] <- - gsub("", project_name, - lines_template[grepl("", lines_template)]) - + gsub( + "", project_name, + lines_template[grepl("", lines_template)] + ) + # Change flat_template file name # _inflate lines_template[grepl("dev/flat_template.Rmd", lines_template)] <- - gsub("dev/flat_template.Rmd", file.path(dev_dir, dev_name), - lines_template[grepl("dev/flat_template.Rmd", lines_template)]) + gsub( + "dev/flat_template.Rmd", file.path(dev_dir, dev_name), + lines_template[grepl("dev/flat_template.Rmd", lines_template)] + ) # _title lines_template[grepl("flat_template.Rmd", lines_template)] <- - gsub("flat_template.Rmd", dev_name, - lines_template[grepl("flat_template.Rmd", lines_template)]) - + gsub( + "flat_template.Rmd", dev_name, + lines_template[grepl("flat_template.Rmd", lines_template)] + ) + # Change my_fun to fun_name if (!is.na(fun_name)) { lines_template[grepl("my_fun", lines_template)] <- - gsub("my_fun", fun_name, - lines_template[grepl("my_fun", lines_template)]) + gsub( + "my_fun", fun_name, + lines_template[grepl("my_fun", lines_template)] + ) } - + cat(enc2utf8(lines_template), file = dev_file_path, sep = "\n") } - + # Add the-dev-history when needed ---- if (template %in% c("full", "minimal", "dev_history")) { dev_file <- file.path(full_dev_dir, "0-dev_history.Rmd") if (file.exists(dev_file) & !isTRUE(overwrite)) { - message("'0-dev_history.Rmd' already exists. It was not overwritten. ", - "Set `add_flat_template(overwrite = TRUE)` if you want to do so.") + message( + "'0-dev_history.Rmd' already exists. It was not overwritten. ", + "Set `add_flat_template(overwrite = TRUE)` if you want to do so." + ) } else { copy <- file.copy( system.file("the-dev-history.Rmd", package = "fusen"), @@ -200,9 +215,8 @@ add_flat_template <- function( } dev_file_path <- c(dev_file_path, dev_file) } - } - + # Add data for the full template exemple if (template %in% c("full")) { inst_dir <- file.path(pkg, "inst") @@ -213,7 +227,7 @@ add_flat_template <- function( # Example dataset file.copy(system.file("nyc_squirrels_sample.csv", package = "fusen"), inst_dir) } - + # .Rbuildignore ---- # usethis::use_build_ignore(dev_dir) # Cannot be used outside project if (length(list.files(pkg, pattern = "[.]Rproj")) == 0) { @@ -221,7 +235,7 @@ add_flat_template <- function( } else { lines <- c(paste0("^", dev_dir, "$")) } - + buildfile <- normalizePath(file.path(pkg, ".Rbuildignore"), mustWork = FALSE) if (!file.exists(buildfile)) { existing_lines <- "" @@ -233,11 +247,11 @@ add_flat_template <- function( all <- c(existing_lines, new) cat(enc2utf8(all), file = buildfile, sep = "\n") } - + # Add a gitignore file in dev_dir ---- # Files to ignore lines <- c("*.html", "*.R") - + gitfile <- normalizePath(file.path(full_dev_dir, ".gitignore"), mustWork = FALSE) if (!file.exists(gitfile)) { existing_lines <- "" @@ -249,11 +263,13 @@ add_flat_template <- function( all <- c(existing_lines, new) cat(enc2utf8(all), file = gitfile, sep = "\n") } - + if (length(list.files(pkg, pattern = "[.]Rproj")) == 0) { here::set_here(pkg) } - if (isTRUE(open) & interactive()) {usethis::edit_file(dev_file_path)} - + if (isTRUE(open) & interactive()) { + usethis::edit_file(dev_file_path) + } + dev_file_path } diff --git a/R/addins.R b/R/addins.R index 3bb4d507..d592f702 100644 --- a/R/addins.R +++ b/R/addins.R @@ -3,14 +3,15 @@ addin_add_template <- function() { if ( requireNamespace("rstudioapi") && - rstudioapi::isAvailable() + rstudioapi::isAvailable() ) { # This will allow to interactively have the function name if (rstudioapi::hasFun("showPrompt")) { template <- rstudioapi::showPrompt( title = "Enter the flat template type", message = "e.g. add = additional; min = minimal;", - default = "additional") + default = "additional" + ) } else { template <- readline("Enter the flat template type: ") } @@ -18,7 +19,8 @@ addin_add_template <- function() { flat_name <- rstudioapi::showPrompt( title = "Choose the function name", message = "Name is used to pre-fill the template", - default = "my_fun") + default = "my_fun" + ) } else { flat_name <- readline("Enter the function name: ") } diff --git a/R/build_fusen_chunks.R b/R/build_fusen_chunks.R index 5a821368..ece407b2 100644 --- a/R/build_fusen_chunks.R +++ b/R/build_fusen_chunks.R @@ -1,30 +1,28 @@ # WARNING - Generated by {fusen} from /dev/flat_addins.Rmd: do not edit by hand #' Add `{fusen}` chunks -#' +#' #' Create `{fusen}` chunks inside your Rmd #' -#' @param function_name Name of the function to create. +#' @param function_name Name of the function to create. #' If NULL (the default), the user will be prompted to enter it. -#' @param export Should the function be exported? -#' Default is `getOption("fusen.export.functions")`. If NULL, the -#' user will be prompted to enter it. +#' @param export Should the function be exported? +#' Default is `getOption("fusen.export.functions")`. If NULL, the +#' user will be prompted to enter it. #' #' @export #' #' @return A list with the context and the content, invisibly. -#' +#' #' @examples #' add_fusen_chunks("this") -add_fusen_chunks <- function( - function_name = NULL, - export = getOption("fusen.export.functions") -) { +add_fusen_chunks <- function(function_name = NULL, + export = getOption("fusen.export.functions")) { if ( requireNamespace("rstudioapi") && - rstudioapi::isAvailable() && - rstudioapi::hasFun("getSourceEditorContext") && - rstudioapi::hasFun("insertText") + rstudioapi::isAvailable() && + rstudioapi::hasFun("getSourceEditorContext") && + rstudioapi::hasFun("insertText") ) { # This will allow to interactively have the function name if (is.null(function_name)) { @@ -34,7 +32,7 @@ add_fusen_chunks <- function( function_name <- readline("Enter the function name: ") } } - + if (is.null(export)) { if (rstudioapi::hasFun("showQuestion")) { export <- rstudioapi::showQuestion("{fusen}", "Should the function be exported?", ok = "yes", cancel = "no") @@ -42,34 +40,34 @@ add_fusen_chunks <- function( export <- readline("Should the function be exported? (y/n) ") == "y" } } - + curr_editor <- rstudioapi::getSourceEditorContext() if (!grepl("\\.Rmd$", curr_editor$path)) { stop("fusen chunks can only be added inside a Rmd file.") } - + s <- curr_editor$selection - + # What happens if the user has selected something in the Rmd? # Throw an error - if (nchar(s[[1L]]$text) != 0L){ + if (nchar(s[[1L]]$text) != 0L) { stop("fusen chunks can't be inserted on top of selected text.") } - + chunks <- build_fusen_chunks( - function_name, + function_name, export ) - + rstudioapi::insertText( - location = s[[1L]]$range$start, + location = s[[1L]]$range$start, text = chunks ) - + return( invisible( list( - context = curr_editor, + context = curr_editor, chunk_text = chunks ) ) @@ -79,7 +77,7 @@ add_fusen_chunks <- function( #' Build fusen chunks -#' +#' #' Internal tool to build the chunk text #' #' @param function_name Name of the function to build the chunk text with @@ -87,7 +85,7 @@ add_fusen_chunks <- function( #' @noRd #' @examples #' cat(build_fusen_chunks("pouet")) -build_fusen_chunks <- function(function_name, export = TRUE){ +build_fusen_chunks <- function(function_name, export = TRUE) { paste( sep = "\n", sprintf("# %s", function_name), @@ -100,7 +98,7 @@ build_fusen_chunks <- function(function_name, export = TRUE){ "#' @return", "#' ", { - if (export){ + if (export) { "#' @export" } else { "#' @noRd" diff --git a/R/create_fusen_rsproject.R b/R/create_fusen_rsproject.R index e60187b3..76fa17f0 100644 --- a/R/create_fusen_rsproject.R +++ b/R/create_fusen_rsproject.R @@ -18,24 +18,23 @@ #' @examples #' my_path <- tempfile("mypkg") #' create_fusen(path = my_path, template = "full", open = FALSE) -create_fusen <- function( - path, - template = c("full", "minimal", "teaching"), - open = TRUE, - overwrite = FALSE, - with_git = FALSE -) { - +create_fusen <- function(path, + template = c("full", "minimal", "teaching"), + open = TRUE, + overwrite = FALSE, + with_git = FALSE) { path <- normalizePath(path, mustWork = FALSE) template <- match.arg(template) project_name <- get_pkg_name(pkg = path) if (project_name != asciify_name(project_name, to_pkg = TRUE)) { - stop("Please rename your project/directory with: `", asciify_name(project_name, to_pkg = TRUE), - "` as a package name should only contain letters, numbers and dots.") + stop( + "Please rename your project/directory with: `", asciify_name(project_name, to_pkg = TRUE), + "` as a package name should only contain letters, numbers and dots." + ) } - if (dir.exists(path)){ + if (dir.exists(path)) { cli::cli_alert_warning( paste( "The path:", path, "already exists." @@ -45,13 +44,16 @@ create_fusen <- function( cli::cli_alert_danger( paste( "Aborting fusen project creation.", - "Set `create_fusen(overwrite = TRUE)` to avoid a stop.")) + "Set `create_fusen(overwrite = TRUE)` to avoid a stop." + ) + ) stop("Could not create fusen project", call. = FALSE) } else { cli::cli_alert_warning( paste( "You set `create_fusen(overwrite = TRUE)`.", - "Some files may be overwritten in 'dev/'.") + "Some files may be overwritten in 'dev/'." + ) ) } } else { @@ -89,10 +91,10 @@ create_fusen <- function( ## Open new project if function is called from Rstudio console ## Rstudio project wizard will spontaneously open the new project - if (isTRUE(open) && #!rstudio_project_wizard_context && - requireNamespace("rstudioapi") && - rstudioapi::isAvailable() && - rstudioapi::hasFun("openProject")) { + if (isTRUE(open) && # !rstudio_project_wizard_context && + requireNamespace("rstudioapi") && + rstudioapi::isAvailable() && + rstudioapi::hasFun("openProject")) { cli::cat_rule("Opening new fusen project") rstudioapi::openProject(path = path) cli::cli_alert_success("Opened new fusen project") @@ -106,18 +108,13 @@ create_fusen <- function( #' This will only work with Rstudio Project Wizard #' @noRd -create_fusen_gui <- function( - path, - template, - with_git -) { - +create_fusen_gui <- function(path, + template, + with_git) { create_fusen( path = file.path(getwd(), path), template = template, open = FALSE, # Project opening is done spontaneously by Rstudio Project Wizard with_git = with_git ) - } - diff --git a/R/fill_description.R b/R/fill_description.R index ab5067c9..4a15122d 100644 --- a/R/fill_description.R +++ b/R/fill_description.R @@ -19,12 +19,16 @@ #' pkg = dummypackage, #' fields = list( #' Title = "Build A Package From Rmarkdown file", -#' Description = paste("Use Rmd First method to build your package.", -#' "Start your package with documentation.", -#' "Everything can be set from a Rmarkdown file in your project."), +#' Description = paste( +#' "Use Rmd First method to build your package.", +#' "Start your package with documentation.", +#' "Everything can be set from a Rmarkdown file in your project." +#' ), #' `Authors@R` = c( -#' person("Sebastien", "Rochette", email = "sebastien@thinkr.fr", -#' role = c("aut", "cre"), comment = c(ORCID = "0000-0002-1565-9313")), +#' person("Sebastien", "Rochette", +#' email = "sebastien@thinkr.fr", +#' role = c("aut", "cre"), comment = c(ORCID = "0000-0002-1565-9313") +#' ), #' person(given = "ThinkR", role = "cph") #' ) #' ) @@ -74,8 +78,15 @@ fill_description <- function(pkg = ".", fields, overwrite = FALSE) { #' Example from base::tolower() #' @noRd capwords <- function(s, strict = FALSE) { - cap <- function(s) paste(toupper(substring(s, 1, 1)), - {s <- substring(s, 2); if(strict) tolower(s) else s}, - sep = "", collapse = " " ) + cap <- function(s) { + paste(toupper(substring(s, 1, 1)), + { + s <- substring(s, 2) + if (strict) tolower(s) else s + }, + sep = "", + collapse = " " + ) + } sapply(strsplit(s, split = " "), cap, USE.NAMES = !is.null(names(s))) } diff --git a/R/inflate-utils.R b/R/inflate-utils.R index eb52fd64..6f5a4e38 100644 --- a/R/inflate-utils.R +++ b/R/inflate-utils.R @@ -12,7 +12,8 @@ parse_fun <- function(x) { # x <- rmd_fun[3,] "function(\\s*)\\(", # R6Class "R6Class(\\s*)\\(", - sep = "|") + sep = "|" + ) regex_extract_fun_name <- paste( # function @@ -21,7 +22,8 @@ parse_fun <- function(x) { # x <- rmd_fun[3,] "[\\w[.]]*(?=(\\s*)(<-|=)(\\s*)R6Class)", # R6::R6Class "[\\w[.]]*(?=(\\s*)(<-|=)(\\s*)R6::R6Class)", - sep = "|") + sep = "|" + ) # stringi::stri_extract_first_regex( # c("zaza <- function()", "zozo <- R6Class()", "zuzu <- R6::R6Class()"), @@ -71,15 +73,19 @@ parse_fun <- function(x) { # x <- rmd_fun[3,] example_pos_end <- all_arobase[all_arobase > example_pos_start][1] - 1 example_pos_end <- ifelse(is.na(example_pos_end), - grep("function(\\s*)\\(", code) - 1, - example_pos_end + grep("function(\\s*)\\(", code) - 1, + example_pos_end ) # Get @rdname and @filename for groups - tag_filename <- gsub("^#'\\s*@filename\\s*", "", - code[grep("^#'\\s*@filename", code)]) - tag_rdname <- gsub("^#'\\s*@rdname\\s*", "", - code[grep("^#'\\s*@rdname", code)]) + tag_filename <- gsub( + "^#'\\s*@filename\\s*", "", + code[grep("^#'\\s*@filename", code)] + ) + tag_rdname <- gsub( + "^#'\\s*@rdname\\s*", "", + code[grep("^#'\\s*@rdname", code)] + ) rox_filename <- c(tag_filename, tag_rdname)[1] # Clean code for @filename as non standard roxygen code[grep("^#'\\s*@filename", code)] <- "#'" @@ -103,7 +109,7 @@ add_names_to_parsed <- function(parsed_tbl, fun_code) { # Which parts were functions which_parsed_fun <- which(!is.na(parsed_tbl$label) & - grepl(regex_functions, parsed_tbl$label)) + grepl(regex_functions, parsed_tbl$label)) # From fun_code, we retrieve fun_name & rox_filename parsed_tbl[["fun_name"]] <- NA_character_ @@ -122,7 +128,8 @@ add_names_to_parsed <- function(parsed_tbl, fun_code) { df <- data.frame( sec_title = parsed_tbl[["sec_title"]][which_parsed_fun], sec_fun_name = parsed_tbl[["fun_name"]][which_parsed_fun], - fake = NA) + fake = NA + ) sec_title_name <- group_code(df, group_col = "sec_title", code_col = "fake") # Fill values for parts that are not function chunks @@ -131,23 +138,29 @@ add_names_to_parsed <- function(parsed_tbl, fun_code) { # reorder chunks for fun, ex, test ? # however, what happens when multiple groups under same title ? sec_fun_name <- sec_title_name[ - sec_title_name[["sec_title"]] == x, "sec_fun_name"] + sec_title_name[["sec_title"]] == x, "sec_fun_name" + ] parsed_tbl[group, "sec_fun_name"] <- ifelse(length(sec_fun_name) == 0, - NA_character_, as.character(sec_fun_name)) + NA_character_, as.character(sec_fun_name) + ) parsed_tbl[group, ] <- tidyr::fill( parsed_tbl[group, ], fun_name, rox_filename, chunk_filename, - .direction = "down") + .direction = "down" + ) parsed_tbl[group, ] <- tidyr::fill( parsed_tbl[group, ], fun_name, rox_filename, chunk_filename, - .direction = "up") + .direction = "up" + ) }) %>% do.call("rbind", .) parsed_tbl[["fun_name"]][pkg_filled[["order"]]] <- pkg_filled[["fun_name"]] } else { - pkg_filled <- parsed_tbl[, c("fun_name", "chunk_filename", "rox_filename", - "sec_fun_name", "sec_title")] + pkg_filled <- parsed_tbl[, c( + "fun_name", "chunk_filename", "rox_filename", + "sec_fun_name", "sec_title" + )] pkg_filled[, "order"] <- 1:nrow(pkg_filled) } @@ -157,31 +170,36 @@ add_names_to_parsed <- function(parsed_tbl, fun_code) { pkg_filled[["file_name"]] <- NA_character_ # chunk_filename pkg_filled[["file_name"]] <- ifelse(!is.na(pkg_filled[["chunk_filename"]]), - pkg_filled[["chunk_filename"]], NA_character_) + pkg_filled[["chunk_filename"]], NA_character_ + ) # rox_filename pkg_filled[["file_name"]] <- ifelse( is.na(pkg_filled[["file_name"]]) & !is.na(pkg_filled[["rox_filename"]]), pkg_filled[["rox_filename"]], - pkg_filled[["file_name"]]) + pkg_filled[["file_name"]] + ) # sec_title - get sec_fun_name pkg_filled[["file_name"]] <- ifelse( is.na(pkg_filled[["file_name"]]) & !is.na(pkg_filled[["sec_fun_name"]]), pkg_filled[["sec_fun_name"]], - pkg_filled[["file_name"]]) + pkg_filled[["file_name"]] + ) # fun_name pkg_filled[["file_name"]] <- ifelse( is.na(pkg_filled[["file_name"]]) & !is.na(pkg_filled[["fun_name"]]), pkg_filled[["fun_name"]], - pkg_filled[["file_name"]]) + pkg_filled[["file_name"]] + ) # sec_title alone if not real function pkg_filled[["file_name"]] <- ifelse( is.na(pkg_filled[["file_name"]]) & !is.na(pkg_filled[["sec_title"]]), pkg_filled[["sec_title"]], - pkg_filled[["file_name"]]) + pkg_filled[["file_name"]] + ) # Clean filename if script extension pkg_filled[["file_name"]] <- gsub("[.]R$|[.]r$", "", pkg_filled[["file_name"]]) @@ -204,7 +222,8 @@ parse_test <- function(x, pkg, relative_flat_file) { # x <- rmd_test[1,] test_file <- file.path( pkg, "tests", "testthat", - paste0("test-", asciify_name(file_name), ".R")) + paste0("test-", asciify_name(file_name), ".R") + ) if (file.exists(test_file)) { cli::cli_alert_warning(paste(basename(test_file), "has been overwritten")) @@ -212,7 +231,8 @@ parse_test <- function(x, pkg, relative_flat_file) { # x <- rmd_test[1,] lines <- c( sprintf("# WARNING - Generated by {fusen} from %s: do not edit by hand\n", relative_flat_file), - x[["code"]][[1]]) + x[["code"]][[1]] + ) write_utf8(path = test_file, lines = lines) return(file_name) @@ -227,20 +247,21 @@ add_fun_code_examples <- function(parsed_tbl, fun_code) { # Example in separate chunk which_parsed_ex <- which(!is.na(parsed_tbl$label) & - grepl(regex_example, parsed_tbl$label)) + grepl(regex_example, parsed_tbl$label)) rmd_ex <- parsed_tbl[which_parsed_ex, ] # Get file_name variable fun_code$order <- 1:nrow(fun_code) fun_file <- parsed_tbl[!is.na(parsed_tbl[["fun_name"]]), c("fun_name", "file_name")] - fun_file_groups <- fun_file[!duplicated(fun_file),] + fun_file_groups <- fun_file[!duplicated(fun_file), ] # Keep all as some are not functions - fun_code <- as_tibble(merge(fun_code, fun_file_groups, by = 'fun_name', all.x = TRUE, sort = FALSE)) + fun_code <- as_tibble(merge(fun_code, fun_file_groups, by = "fun_name", all.x = TRUE, sort = FALSE)) fun_code <- fun_code[order(fun_code[["order"]]), ] # Get file_name for not functions. Only last place where possible fun_code[["file_name"]] <- ifelse(is.na(fun_code[["file_name"]]), - fun_code[["sec_title"]], - fun_code[["file_name"]]) + fun_code[["sec_title"]], + fun_code[["file_name"]] + ) # Example already in skeleton fun_code$example_in <- apply(fun_code, 1, function(x) { @@ -257,10 +278,12 @@ add_fun_code_examples <- function(parsed_tbl, fun_code) { ex_alone <- rmd_ex[is.na(rmd_ex[["fun_name"]]), ] if (nrow(ex_alone) != 0) { - message("Some example chunks are not associated to any function: ", - paste(ex_alone[["label"]], collapse = ", "), ".", - "\nIf you plan to include them only in the vignette, then ", - "you can give them any other name except `dev*`, `fun*`, `test*`") + message( + "Some example chunks are not associated to any function: ", + paste(ex_alone[["label"]], collapse = ", "), ".", + "\nIf you plan to include them only in the vignette, then ", + "you can give them any other name except `dev*`, `fun*`, `test*`" + ) } rmd_ex <- rmd_ex[!is.na(rmd_ex[["fun_name"]]), ] @@ -271,7 +294,7 @@ add_fun_code_examples <- function(parsed_tbl, fun_code) { tibble::tibble( fun_name = rmd_ex[x, ][["fun_name"]], # example_chunk = list(paste("#'", rmd_get_chunk(rmd_ex[x, ])$code)) - example_chunk = list(paste("#'", unlist(rmd_node_code(rmd_ex[x,][["ast"]])))) + example_chunk = list(paste("#'", unlist(rmd_node_code(rmd_ex[x, ][["ast"]])))) ) } ) %>% do.call("rbind", .) @@ -306,16 +329,16 @@ add_fun_code_examples <- function(parsed_tbl, fun_code) { # Add to function code fun_code[["code_example"]] <- lapply(seq_len(nrow(fun_code)), function(x) { - fun_code_x <- fun_code[x, ] if (is.na(fun_code_x[["fun_name"]])) { return( - unlist(fun_code_x[["code"]])) + unlist(fun_code_x[["code"]]) + ) } end_skeleton <- ifelse(is.na(fun_code_x[["example_pos_start"]]), - fun_code_x[["example_pos_end"]], - fun_code_x[["example_pos_start"]] - 1 + fun_code_x[["example_pos_end"]], + fun_code_x[["example_pos_start"]] - 1 ) all_fun_code <- stats::na.omit(c( @@ -348,33 +371,34 @@ add_fun_code_examples <- function(parsed_tbl, fun_code) { #' @param code_col Character. Name of column to combine #' @noRd group_code <- function(df, group_col, code_col) { - lapply(unique(df[[group_col]]), #sec_title - function(x) { - group <- df[df[[group_col]] == x,] # sec_title - # Take first content of everything - group_combined <- group[1, ] - # Combine code column in a vector of character - # from all code in the group - comb_ex <- unlist( - lapply(group[[code_col]], function(y) c(y, "")) - ) - # Remove last extra empty line - comb_ex <- comb_ex[-length(comb_ex)] - group_combined[[code_col]] <- list(comb_ex) - group_combined - }) %>% + lapply( + unique(df[[group_col]]), # sec_title + function(x) { + group <- df[df[[group_col]] == x, ] # sec_title + # Take first content of everything + group_combined <- group[1, ] + # Combine code column in a vector of character + # from all code in the group + comb_ex <- unlist( + lapply(group[[code_col]], function(y) c(y, "")) + ) + # Remove last extra empty line + comb_ex <- comb_ex[-length(comb_ex)] + group_combined[[code_col]] <- list(comb_ex) + group_combined + } + ) %>% do.call("rbind", .) } #' Retrieve name of the package if there is a DESCRIPTION #' @noRd -get_pkg_name <- function(pkg){ +get_pkg_name <- function(pkg) { desc <- file.path(pkg, "DESCRIPTION") if (file.exists(desc)) { pkgname <- read.dcf(desc)[colnames(read.dcf(desc)) == "Package"] - } else{ + } else { pkgname <- basename(pkg) - } pkgname } @@ -392,21 +416,32 @@ create_vignette_head <- function(pkg, vignette_name, yaml_options = NULL) { # Get all yaml options except Title, output, editor_options yaml_options <- yaml_options[ - !names(yaml_options) %in% c("output", "title", "editor_options")] + !names(yaml_options) %in% c("output", "title", "editor_options") + ] + + if (is.null(names(vignette_name)) || names(vignette_name) == "") { + vignette_title <- vignette_name + } else { + vignette_title <- names(vignette_name) + } enc2utf8( glue( '--- -title: ".{vignette_name}." +title: ".{vignette_title}." output: rmarkdown::html_vignette', ifelse(length(yaml_options) != 0, - glue::glue_collapse( - c("", - glue("{names(yaml_options)}: \"{yaml_options}\""), ""), - sep = "\n"), - "\n"), + glue::glue_collapse( + c( + "", + glue("{names(yaml_options)}: \"{yaml_options}\""), "" + ), + sep = "\n" + ), + "\n" + ), 'vignette: > - %\\VignetteIndexEntry{.{vignette_name}.} + %\\VignetteIndexEntry{.{asciify_name(vignette_name)}.} %\\VignetteEngine{knitr::rmarkdown} %\\VignetteEncoding{UTF-8} --- @@ -422,7 +457,7 @@ knitr::opts_chunk$set( library(.{pkgname}.) ``` ', - .open = ".{", .close = "}." + .open = ".{", .close = "}." ) ) } @@ -432,7 +467,7 @@ library(.{pkgname}.) #' @param lines Character. Lines of code to write #' @param append Logical. Whether to append to existing file #' @noRd -write_utf8 <- function(path, lines, append = FALSE){ +write_utf8 <- function(path, lines, append = FALSE) { file_mode <- if (append) "ab" else "wb" con <- file(path, open = file_mode, encoding = "utf-8") @@ -476,15 +511,23 @@ asciify_name <- function(name, to_pkg = FALSE) { # Escape all diacritics name <- stri_trans_general(name, id = "Latin-ASCII") - cleaned_name <- gsub("^[.]*|^-|-$", "", - gsub("-+", "-", - gsub("-_|_-", "-", - gsub("[^([:alnum:]*_*-*)*]", "-", name)))) + cleaned_name <- gsub( + "^[.]*|^-|-$", "", + gsub( + "-+", "-", + gsub( + "-_|_-", "-", + gsub("[^([:alnum:]*_*-*)*]", "-", name) + ) + ) + ) # grepl("^[[:alpha:]][[:alnum:]_-]*$", cleaned_name) if (isTRUE(to_pkg)) { - cleaned_name <- gsub("[^a-zA-Z0-9]+", ".", - gsub("^[0-9]+", "", cleaned_name)) + cleaned_name <- gsub( + "[^a-zA-Z0-9]+", ".", + gsub("^[0-9]+", "", cleaned_name) + ) } else { # asciify from {usethis} usethis:::asciify() cleaned_name <- tolower( diff --git a/R/inflate.R b/R/inflate.R index a4b28aa0..87d4a66f 100644 --- a/R/inflate.R +++ b/R/inflate.R @@ -41,10 +41,12 @@ regex_example <- paste(regex_example_vec, collapse = "|") #' #' # {fusen} steps #' dev_file <- add_flat_template(template = "full", pkg = dummypackage, overwrite = TRUE) -#' flat_file <- dev_file[grepl('flat', dev_file)] +#' flat_file <- dev_file[grepl("flat", dev_file)] #' fill_description(pkg = dummypackage, fields = list(Title = "Dummy Package")) -#' inflate(pkg = dummypackage, flat_file = flat_file, -#' vignette_name = "Exploration of my Data", check = FALSE) +#' inflate( +#' pkg = dummypackage, flat_file = flat_file, +#' vignette_name = "Exploration of my Data", check = FALSE +#' ) #' #' # Explore directory of the package #' # browseURL(dummypackage) @@ -59,25 +61,28 @@ inflate <- function(pkg = ".", flat_file, open_vignette = TRUE, check = TRUE, document = TRUE, overwrite = "ask", ...) { - if (!is.null(list(...)[["name"]])) { - warning(paste0("The `name` argument to `inflate()` is deprecated since {fusen} version 0.3.0,", - " and will be removed in a future version.", - "\nPlease use `vignette_name = '", list(...)[["name"]],"'` instead.\n")) + warning(paste0( + "The `name` argument to `inflate()` is deprecated since {fusen} version 0.3.0,", + " and will be removed in a future version.", + "\nPlease use `vignette_name = '", list(...)[["name"]], "'` instead.\n" + )) vignette_name <- list(...)[["name"]] } if (!is.null(list(...)[["rmd"]])) { - warning(paste0("The `rmd` argument to `inflate()` is deprecated since {fusen} version 0.3.0,", - " and will be removed in a future version.", - "\nPlease use `flat_file = '", list(...)[["rmd"]],"'` instead.\n")) + warning(paste0( + "The `rmd` argument to `inflate()` is deprecated since {fusen} version 0.3.0,", + " and will be removed in a future version.", + "\nPlease use `flat_file = '", list(...)[["rmd"]], "'` instead.\n" + )) flat_file <- list(...)[["rmd"]] } # Save all open files if ( requireNamespace("rstudioapi") && - rstudioapi::isAvailable() && - rstudioapi::hasFun("documentSaveAll") + rstudioapi::isAvailable() && + rstudioapi::hasFun("documentSaveAll") ) { rstudioapi::documentSaveAll() } @@ -86,17 +91,18 @@ inflate <- function(pkg = ".", flat_file, # If flat_file empty if ( requireNamespace("rstudioapi") && - rstudioapi::isAvailable() && - rstudioapi::hasFun("documentPath") + rstudioapi::isAvailable() && + rstudioapi::hasFun("documentPath") ) { current_file <- rstudioapi::documentPath() if (missing(flat_file) & grepl("^flat.*[.]Rmd$", basename(current_file))) { if (overwrite == "ask") { - sure <- paste( + sure <- paste( "The current file will be inflated:\n", current_file, ".\n", - "Are you sure this is what you planned? (y/n)\n") - do_it <- readline(sure) == "y" + "Are you sure this is what you planned? (y/n)\n" + ) + do_it <- readline(sure) == "y" } else { do_it <- isTRUE(overwrite) } @@ -108,9 +114,11 @@ inflate <- function(pkg = ".", flat_file, } if (missing(flat_file)) { - stop("`flat_file` argument is empty. ", - "Did you run `inflate()` directly in the console, ", - "instead of the one at the bottom of your flat file?") + stop( + "`flat_file` argument is empty. ", + "Did you run `inflate()` directly in the console, ", + "instead of the one at the bottom of your flat file?" + ) } old <- setwd(pkg) @@ -193,13 +201,18 @@ inflate <- function(pkg = ".", flat_file, parsed_tbl$chunk_filename <- unlist( lapply( parsed_tbl[["options"]], - function(x) ifelse(is.null(x[["filename"]]), - NA_character_, gsub('"', '', x[["filename"]]))) + function(x) { + ifelse(is.null(x[["filename"]]), + NA_character_, gsub('"', "", x[["filename"]]) + ) + } + ) ) # Define sec_title to group functions in same R file sec_title <- paste(parsed_tbl[["sec_h1"]], - parsed_tbl[["sec_h2"]], - sep = "-") + parsed_tbl[["sec_h2"]], + sep = "-" + ) if (length(sec_title) != 0) { parsed_tbl$sec_title <- sec_title @@ -266,7 +279,6 @@ inflate <- function(pkg = ".", flat_file, #' @importFrom stats na.omit #' @noRd create_functions_all <- function(parsed_tbl, fun_code, pkg, relative_flat_file) { - fun_names <- fun_code[["fun_name"]] if (length(unique(na.omit(fun_names))) != length(na.omit(fun_names))) { @@ -295,7 +307,7 @@ create_functions_all <- function(parsed_tbl, fun_code, pkg, relative_flat_file) "please rename chunks with 'examples-fun_name' for instance.\n", "Duplicates: ", paste(labels_in_vignette[duplicated(labels_in_vignette)], - collapse = ", " + collapse = ", " ) ) } @@ -324,11 +336,10 @@ create_functions_all <- function(parsed_tbl, fun_code, pkg, relative_flat_file) #' @importFrom parsermd rmd_get_chunk #' @noRd get_functions_tests <- function(parsed_tbl) { - which_parsed_fun <- which(!is.na(parsed_tbl$label) & - grepl(regex_functions, parsed_tbl$label)) + grepl(regex_functions, parsed_tbl$label)) which_parsed_tests <- which(!is.na(parsed_tbl$label) & - grepl(regex_tests, parsed_tbl$label)) + grepl(regex_tests, parsed_tbl$label)) rmd_fun <- parsed_tbl[which_parsed_fun, ] @@ -363,7 +374,6 @@ get_functions_tests <- function(parsed_tbl) { #' @param relative_flat_file Path to the flat file to show in R scripts #' @noRd create_r_files <- function(fun_code, pkg, relative_flat_file) { - fun_code <- fun_code[(lengths(fun_code[["code"]]) != 0), ] # Combine code with same sec_title to be set in same R file @@ -400,11 +410,10 @@ create_r_files <- function(fun_code, pkg, relative_flat_file) { #' #' @noRd create_tests_files <- function(parsed_tbl, pkg, relative_flat_file) { - project_name <- get_pkg_name(pkg = pkg) rmd_test <- parsed_tbl[!is.na(parsed_tbl$label) & - grepl(regex_tests, parsed_tbl$label), ] + grepl(regex_tests, parsed_tbl$label), ] # If there is at least one test if (nrow(rmd_test) != 0) { @@ -414,7 +423,8 @@ create_tests_files <- function(parsed_tbl, pkg, relative_flat_file) { stop( "Some `test` chunks can not be handled: ", paste(rmd_test[["label"]][!is.na(rmd_test[["file_name"]])], - collapse = ", "), + collapse = ", " + ), ". Please associate these `test` chunks with a `function` chunk, ", "under a section title or with a `filename='mytestfile.R'` chunk option." ) @@ -426,9 +436,10 @@ create_tests_files <- function(parsed_tbl, pkg, relative_flat_file) { # Filter if code is still empty after code grouped rmd_test[["is_empty"]] <- lapply( - rmd_test[["code"]], function(x) grepl("^\\s*$", paste(x, collapse = ""))) %>% + rmd_test[["code"]], function(x) grepl("^\\s*$", paste(x, collapse = "")) + ) %>% unlist() - rmd_test <- rmd_test[!rmd_test[["is_empty"]],] + rmd_test <- rmd_test[!rmd_test[["is_empty"]], ] if (nrow(rmd_test) != 0) { # Add directory @@ -452,7 +463,8 @@ create_tests_files <- function(parsed_tbl, pkg, relative_flat_file) { out <- unlist(lapply( seq_len(nrow(rmd_test)), - function(x) parse_test(rmd_test[x, ], pkg, relative_flat_file))) + function(x) parse_test(rmd_test[x, ], pkg, relative_flat_file) + )) } } } @@ -490,10 +502,10 @@ create_vignette <- function(parsed_tbl, pkg, relative_flat_file, vignette_name, ), collapse = "|") vignette_tbl <- parsed_tbl[ !(grepl(not_in_vignette, parsed_tbl[["label"]]) | - grepl("rmd_yaml_list", parsed_tbl[["type"]])), + grepl("rmd_yaml_list", parsed_tbl[["type"]])), ] - flat_yaml <- parsed_tbl[grepl("rmd_yaml_list", parsed_tbl[["type"]]),] + flat_yaml <- parsed_tbl[grepl("rmd_yaml_list", parsed_tbl[["type"]]), ] # Make chunk names unique # vignette_tbl[["label"]][grepl("unnamed", vignette_tbl[["label"]])] <- # gsub("unnamed-", "parsermd-", vignette_tbl[["label"]][grepl("unnamed", vignette_tbl[["label"]])]) @@ -532,9 +544,11 @@ create_vignette <- function(parsed_tbl, pkg, relative_flat_file, vignette_name, usethis::use_git_ignore("inst/doc") # Vignette head - head <- create_vignette_head(pkg = pkg, - vignette_name = vignette_name, - yaml_options = yaml_options) + head <- create_vignette_head( + pkg = pkg, + vignette_name = vignette_name, + yaml_options = yaml_options + ) # Write vignette lines <- c( @@ -545,7 +559,7 @@ create_vignette <- function(parsed_tbl, pkg, relative_flat_file, vignette_name, relative_flat_file ) ) - if ( nrow(vignette_tbl) != 0 ) { + if (nrow(vignette_tbl) != 0) { lines <- c( lines, parsermd::as_document(vignette_tbl) @@ -554,6 +568,7 @@ create_vignette <- function(parsed_tbl, pkg, relative_flat_file, vignette_name, write_utf8(path = vignette_file, lines = lines) - if (isTRUE(open_vignette) & interactive()) {usethis::edit_file(vignette_file)} + if (isTRUE(open_vignette) & interactive()) { + usethis::edit_file(vignette_file) + } } - diff --git a/README.Rmd b/README.Rmd index 034ed942..f6d263bd 100644 --- a/README.Rmd +++ b/README.Rmd @@ -152,6 +152,22 @@ After that, you need to move your functions and scripts in the correct place. Le *{fusen} is also addressed to more advanced developers who are fed up with switching* between R files, tests files, vignettes. In particular, when changing arguments of a function, we need to change examples, unit tests in multiple places. Here, you can do it in one place. No risk to forget one. Think also about code review: everything related to one function is at the same place. +## Q&A : All tips and tricks of a {fusen} template + +- Can I be lazy in names used? +- Can I knit the content of the flat template ? +- How to declare packages with library() for the future vignette ? +- How to include examples that cannot be run ? +- Document your internal datasets in a function chunk as usual +- How to ignore some chunks ? +- How to create a vignette with different title and Index Entry? +- How not to create a vignette ? +- How to get a pre-filled template for a specific function name ? +- How to Inflate multiple flat files ? +- How to store multiple functions in a unique R file ? +- How to read dataset that I usually put in “tests/testthat/” for my unit tests? + +=> See vignette Tips and Tricks: https://thinkr-open.github.io/fusen/articles/tips-and-tricks.html ## Why is this package named {fusen} ? diff --git a/README.md b/README.md index 4416fa5e..f544e74b 100644 --- a/README.md +++ b/README.md @@ -193,6 +193,25 @@ tests in multiple places. Here, you can do it in one place. No risk to forget one. Think also about code review: everything related to one function is at the same place. +## Q&A : All tips and tricks of a {fusen} template + +- Can I be lazy in names used? +- Can I knit the content of the flat template ? +- How to declare packages with library() for the future vignette ? +- How to include examples that cannot be run ? +- Document your internal datasets in a function chunk as usual +- How to ignore some chunks ? +- How to create a vignette with different title and Index Entry? +- How not to create a vignette ? +- How to get a pre-filled template for a specific function name ? +- How to Inflate multiple flat files ? +- How to store multiple functions in a unique R file ? +- How to read dataset that I usually put in “tests/testthat/” for my + unit tests? + +=> See vignette Tips and Tricks: + + ## Why is this package named {fusen} ? A fusen is an origami. It is a flat piece of paper that you fold in a diff --git a/data-raw/DATASET.R b/data-raw/DATASET.R index 38eb180e..65f3c03e 100644 --- a/data-raw/DATASET.R +++ b/data-raw/DATASET.R @@ -10,8 +10,11 @@ clients <- database$clients %>% tidyr::fill(departement) %>% mutate( entry_year = lubridate::year(entry_date), - age_class = cut(age, breaks = c(18, 25, 40, 55, 70, 100), - include.lowest = TRUE)) + age_class = cut(age, + breaks = c(18, 25, 40, 55, 70, 100), + include.lowest = TRUE + ) + ) # usethis::use_data(clients, overwrite = TRUE) diff --git a/dev/dev_history.R b/dev/dev_history.R index 5eaa79a9..f180ce5e 100644 --- a/dev/dev_history.R +++ b/dev/dev_history.R @@ -220,6 +220,9 @@ spelling::spell_check_package() urlchecker::url_check() urlchecker::url_update() +# Upgrade version number +usethis::use_version(which = c("patch", "minor", "major", "dev")[2]) + # check on other distributions # _rhub devtools::check_rhub() @@ -248,7 +251,7 @@ rstudioapi::navigateToFile(system.file(package = "rhub", "bin", "rhub-linux-dock # /opt/R-devel/bin/R -rhub::check(platform = "windows-x86_64-devel") +rhub::check(platform = "windows-x86_64-devel", show_status = FALSE) # _win devel devtools::check_win_devel() diff --git a/man/add_flat_template.Rd b/man/add_flat_template.Rd index e28c514a..914d10f0 100644 --- a/man/add_flat_template.Rd +++ b/man/add_flat_template.Rd @@ -92,7 +92,7 @@ unlink(dummypackage, recursive = TRUE) # For classical use in your package \dontrun{ # first time ever using 'fusen' -add_flat_template("full") +add_flat_template("full") # first time in your new package add_flat_template("minimal") @@ -102,5 +102,6 @@ add_flat_template("add") # add new flat template for teaching (a reduced full template) add_flat_template("teaching") -#'} +#' +} } diff --git a/man/fill_description.Rd b/man/fill_description.Rd index 9518cb35..0d795967 100644 --- a/man/fill_description.Rd +++ b/man/fill_description.Rd @@ -30,12 +30,16 @@ fill_description( pkg = dummypackage, fields = list( Title = "Build A Package From Rmarkdown file", - Description = paste("Use Rmd First method to build your package.", - "Start your package with documentation.", - "Everything can be set from a Rmarkdown file in your project."), + Description = paste( + "Use Rmd First method to build your package.", + "Start your package with documentation.", + "Everything can be set from a Rmarkdown file in your project." + ), `Authors@R` = c( - person("Sebastien", "Rochette", email = "sebastien@thinkr.fr", - role = c("aut", "cre"), comment = c(ORCID = "0000-0002-1565-9313")), + person("Sebastien", "Rochette", + email = "sebastien@thinkr.fr", + role = c("aut", "cre"), comment = c(ORCID = "0000-0002-1565-9313") + ), person(given = "ThinkR", role = "cph") ) ) diff --git a/man/inflate.Rd b/man/inflate.Rd index b0a3fa54..7830bd00 100644 --- a/man/inflate.Rd +++ b/man/inflate.Rd @@ -49,10 +49,12 @@ dir.create(dummypackage) # {fusen} steps dev_file <- add_flat_template(template = "full", pkg = dummypackage, overwrite = TRUE) -flat_file <- dev_file[grepl('flat', dev_file)] +flat_file <- dev_file[grepl("flat", dev_file)] fill_description(pkg = dummypackage, fields = list(Title = "Dummy Package")) -inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "Exploration of my Data", check = FALSE) +inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "Exploration of my Data", check = FALSE +) # Explore directory of the package # browseURL(dummypackage) diff --git a/tests/testthat/test-add_dev_history.R b/tests/testthat/test-add_dev_history.R index 3e1067f8..06962e7b 100644 --- a/tests/testthat/test-add_dev_history.R +++ b/tests/testthat/test-add_dev_history.R @@ -17,6 +17,5 @@ test_that("add_dev_history is deprecated but works correctly", { expect_true(all(file.exists(dev_file_path))) expect_true(file.exists(file.path(dummypackage, "dev", "0-dev_history.Rmd"))) expect_true(file.exists(file.path(dummypackage, ".here"))) - }) unlink(dummypackage, recursive = TRUE) diff --git a/tests/testthat/test-add_flat_template.R b/tests/testthat/test-add_flat_template.R index 0301d913..e5c741b4 100644 --- a/tests/testthat/test-add_flat_template.R +++ b/tests/testthat/test-add_flat_template.R @@ -9,24 +9,25 @@ pkg_name <- basename(dummypackage) # add_flat_template ---- test_that("add_flat_template adds flat_template.Rmd and co.", { - dev_file_path <- expect_error(add_flat_template(pkg = dummypackage, open = FALSE), - regexp = NA) + dev_file_path <- expect_error(add_flat_template(pkg = dummypackage, open = FALSE), + regexp = NA + ) flat_file <- dev_file_path[grepl("flat_", dev_file_path)] - + expect_true(all(file.exists(dev_file_path))) expect_true(file.exists(file.path(dummypackage, "dev", "0-dev_history.Rmd"))) expect_true(file.exists(file.path(dummypackage, ".here"))) expect_true(file.exists(file.path(dummypackage, "inst", "nyc_squirrels_sample.csv"))) - + rbuildignore_file <- file.path(dummypackage, ".Rbuildignore") expect_true(file.exists(rbuildignore_file)) rbuildignore_lines <- readLines(rbuildignore_file) expect_true(any(grepl("^dev$", rbuildignore_lines, fixed = TRUE))) expect_true(any(grepl("[.]here", rbuildignore_lines))) - + dev_lines <- readLines(flat_file) expect_equal(length(grep(pkg_name, dev_lines)), 3) - + # Second time message and new file expect_message(add_flat_template(pkg = dummypackage)) expect_true(file.exists(file.path(dummypackage, "dev", "flat_full_2.Rmd"))) @@ -40,7 +41,7 @@ unlink(dummypackage, recursive = TRUE) # Create a new project dummypackage2 <- tempfile(pattern = "rproj.nohere") dir.create(dummypackage2) -cat("", file = file.path(dummypackage2, 'dummy.Rproj')) +cat("", file = file.path(dummypackage2, "dummy.Rproj")) # Add dev_file_path <- add_flat_template(pkg = dummypackage2, open = FALSE) @@ -49,7 +50,7 @@ flat_file <- dev_file_path[grepl("flat", dev_file_path)] test_that("add_flat_template works with .Rproj and no .here", { expect_true(all(file.exists(dev_file_path))) expect_false(file.exists(file.path(dummypackage2, ".here"))) - + rbuildignore_file <- file.path(dummypackage2, ".Rbuildignore") expect_true(file.exists(rbuildignore_file)) rbuildignore_lines <- readLines(rbuildignore_file) @@ -65,11 +66,11 @@ dir.create(dummypackage) test_that("add dev_history template works", { withr::with_dir(dummypackage, { dev_file_path <- expect_error(add_flat_template(pkg = dummypackage, template = "dev_history", open = FALSE), regexp = NA) - + expect_true(file.exists(dev_file_path)) - + usethis::with_project(dummypackage, { - + # Extract and test the description chunk dev_lines <- readLines(dev_file_path) # Change path of project @@ -77,12 +78,13 @@ test_that("add dev_history template works", { "here::here()", # To correct for Windows path paste0('"', gsub("\\\\", "\\\\\\\\", dummypackage), '"'), dev_lines, - # paste0('"', newdir, '"'), dev_lines, - fixed = TRUE) + # paste0('"', newdir, '"'), dev_lines, + fixed = TRUE + ) dev_parse <- parsermd::parse_rmd(dev_lines) desc_code <- tempfile("desc") - parsermd::rmd_select(dev_parse, "description")[[1]] %>% - parsermd::rmd_node_code() %>% + parsermd::rmd_select(dev_parse, "description")[[1]] %>% + parsermd::rmd_node_code() %>% cat(., sep = "\n", file = desc_code) # Execute code expect_error(source(desc_code), regexp = NA) @@ -90,7 +92,6 @@ test_that("add dev_history template works", { expect_true(file.exists(file.path(dummypackage, "DESCRIPTION"))) expect_true(file.exists(file.path(dummypackage, "LICENSE"))) expect_true(file.exists(file.path(dummypackage, "LICENSE.md"))) - }) }) @@ -104,15 +105,18 @@ dir.create(dummypackage) test_that("add dev_history template works with windows \\users path", { withr::with_dir(dummypackage, { dev_file_path <- expect_error( - add_flat_template(pkg = dummypackage, template = "dev_history", - open = FALSE), - regexp = NA) - + add_flat_template( + pkg = dummypackage, template = "dev_history", + open = FALSE + ), + regexp = NA + ) + expect_true(file.exists(dev_file_path)) # Test specific \\users path newdir_uu <- tempfile("aa\\U/gzv") dir.create(newdir_uu, recursive = TRUE) - + usethis::with_project(dummypackage, { # Extract and test the description chunk dev_lines <- readLines(dev_file_path) @@ -122,11 +126,12 @@ test_that("add dev_history template works with windows \\users path", { # To correct for Windows path paste0('"', gsub("\\\\", "\\\\\\\\", newdir_uu), '"'), dev_lines, # paste0('"', newdir_uu, '"'), dev_lines, - fixed = TRUE) + fixed = TRUE + ) dev_parse <- parsermd::parse_rmd(dev_lines) desc_code <- tempfile("desc", fileext = ".R") - parsermd::rmd_select(dev_parse, "description")[[1]] %>% - parsermd::rmd_node_code() %>% + parsermd::rmd_select(dev_parse, "description")[[1]] %>% + parsermd::rmd_node_code() %>% cat(., sep = "\n", file = desc_code) # Execute code expect_error(source(desc_code), regexp = NA) @@ -137,7 +142,6 @@ test_that("add dev_history template works with windows \\users path", { # No LICENSE because of separation of DESCRIPTION # expect_true(file.exists(file.path(dummypackage, "LICENSE"))) expect_true(file.exists(file.path(dummypackage, "LICENSE.md"))) - }) }) @@ -147,7 +151,7 @@ unlink(dummypackage) # Create a new project dummypackage3 <- tempfile(pattern = "malformed_pkg") dir.create(dummypackage3) -cat("", file = file.path(dummypackage3, 'dummy.Rproj')) +cat("", file = file.path(dummypackage3, "dummy.Rproj")) # Add test_that("add_flat_template fails", { @@ -170,32 +174,35 @@ for (template in all_templates) { # Add dev_file_path <- add_flat_template(pkg = dummypackage4, template = template, open = FALSE) flat_file <- dev_file_path[grepl("flat", dev_file_path)] - + # Change lines asking for pkg name lines_template <- readLines(system.file("tests-templates/dev-template-tests.Rmd", package = "fusen")) lines_template[grepl("", lines_template)] <- - gsub("", basename(dummypackage4), - lines_template[grepl("", lines_template)]) + gsub( + "", basename(dummypackage4), + lines_template[grepl("", lines_template)] + ) cat(enc2utf8(lines_template), file = flat_file, sep = "\n") - + withr::with_dir(dummypackage4, { usethis::proj_set(dummypackage4) here:::do_refresh_here(dummypackage4) - + if (rmarkdown::pandoc_available("1.12.3")) { rmarkdown::render( input = file.path(dummypackage4, "dev", paste0("flat_", template, ".Rmd")), output_file = file.path(dummypackage4, "dev", paste0("flat_", template, ".html")), - envir = new.env(), quiet = TRUE) + envir = new.env(), quiet = TRUE + ) } }) - + test_that(paste0("template", template, "runs as markdown"), { expect_true(file.exists(file.path(dummypackage4, "dev", paste0("flat_", template, ".Rmd")))) if (template %in% c("full", "minimal")) { expect_true(file.exists(file.path(dirname(flat_file), "0-dev_history.Rmd"))) } - + if (rmarkdown::pandoc_available("1.12.3")) { expect_true(file.exists(file.path(dummypackage4, "DESCRIPTION"))) expect_true(file.exists(file.path(dummypackage4, "LICENSE"))) @@ -217,22 +224,26 @@ test_that("Other flat_name works", { # Template additional used with "add" dev_file_path <- expect_error( add_flat_template( - template = "add", - pkg = dummypackage, flat_name = "hello", open = FALSE), - regexp = NA) + template = "add", + pkg = dummypackage, flat_name = "hello", open = FALSE + ), + regexp = NA + ) expect_true(file.exists(file.path(dummypackage, "dev/flat_hello.Rmd"))) hello_flat <- readLines(dev_file_path) # 7 times hello for function name # 2 times hello in flat_hello.Rmd expect_equal(length(grep("hello", hello_flat)), 7 + 2) expect_equal(length(grep("flat_hello[.]Rmd", hello_flat)), 2) - + # Template minimal used with "min", same name, flat changes name expect_message( add_flat_template( - template = "min", - pkg = dummypackage, flat_name = "hello", open = FALSE), - regexp = "flat_hello.Rmd already exists.") + template = "min", + pkg = dummypackage, flat_name = "hello", open = FALSE + ), + regexp = "flat_hello.Rmd already exists." + ) flat_file <- file.path(dummypackage, "dev/flat_hello_2.Rmd") expect_true(file.exists(flat_file)) hello_flat <- readLines(flat_file) @@ -240,17 +251,20 @@ test_that("Other flat_name works", { # 2 times hello in flat_hello_2.Rmd expect_equal(length(grep("hello", hello_flat)), 7 + 2) expect_equal(length(grep("flat_hello_2[.]Rmd", hello_flat)), 2) - + # Try inflate to see if files get hello name usethis::with_project(dummypackage, { fill_description(pkg = dummypackage, fields = list(Title = "Dummy Package")) expect_error( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "hello", check = FALSE, - open_vignette = FALSE), - regexp = NA) + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "hello", check = FALSE, + open_vignette = FALSE + ), + regexp = NA + ) }) - + expect_true(file.exists(file.path(dummypackage, "R", "hello.R"))) expect_true(file.exists(file.path(dummypackage, "tests", "testthat", "test-hello.R"))) expect_true(file.exists(file.path(dummypackage, "vignettes", "hello.Rmd"))) @@ -272,14 +286,16 @@ test_that("Other dev_dir works", { # Template additional used with "add" dev_file_path <- expect_error( add_flat_template( - template = "add", - pkg = dummypackage, flat_name = "hello", + template = "add", + pkg = dummypackage, flat_name = "hello", dev_dir = "devdir", - open = FALSE), - regexp = NA) + open = FALSE + ), + regexp = NA + ) expect_true(file.exists(file.path(dummypackage, "devdir/flat_hello.Rmd"))) hello_flat <- readLines(dev_file_path) - + # 7 times hello for function name # 2 times hello in flat_hello.Rmd # 1 time devdir/flat_hello.Rmd @@ -298,21 +314,22 @@ pkg_name <- basename(dummypackage) # add_full ---- test_that("add_full adds flat_full.Rmd", { - dev_file_path <- expect_error(add_full(pkg = dummypackage, open = FALSE), - regexp = NA) + dev_file_path <- expect_error(add_full(pkg = dummypackage, open = FALSE), + regexp = NA + ) flat_file <- dev_file_path[grepl("flat_", dev_file_path)] - + expect_true(all(file.exists(dev_file_path))) expect_true(file.exists(file.path(dummypackage, "dev", "0-dev_history.Rmd"))) expect_true(file.exists(file.path(dummypackage, ".here"))) expect_true(file.exists(file.path(dummypackage, "inst", "nyc_squirrels_sample.csv"))) - + rbuildignore_file <- file.path(dummypackage, ".Rbuildignore") expect_true(file.exists(rbuildignore_file)) rbuildignore_lines <- readLines(rbuildignore_file) expect_true(any(grepl("^dev$", rbuildignore_lines, fixed = TRUE))) expect_true(any(grepl("[.]here", rbuildignore_lines))) - + dev_lines <- readLines(flat_file) expect_equal(length(grep(pkg_name, dev_lines)), 3) }) @@ -323,21 +340,22 @@ dir.create(dummypackage) pkg_name <- basename(dummypackage) # add_minimal test_that("add_minimal adds flat_minimal.Rmd", { - dev_file_path <- expect_error(add_minimal(pkg = dummypackage, open = FALSE), - regexp = NA) + dev_file_path <- expect_error(add_minimal(pkg = dummypackage, open = FALSE), + regexp = NA + ) flat_file <- dev_file_path[grepl("flat_", dev_file_path)] - + expect_true(all(file.exists(dev_file_path))) expect_true(file.exists(file.path(dummypackage, "dev", "0-dev_history.Rmd"))) expect_true(file.exists(file.path(dummypackage, ".here"))) expect_false(file.exists(file.path(dummypackage, "inst", "nyc_squirrels_sample.csv"))) - + rbuildignore_file <- file.path(dummypackage, ".Rbuildignore") expect_true(file.exists(rbuildignore_file)) rbuildignore_lines <- readLines(rbuildignore_file) expect_true(any(grepl("^dev$", rbuildignore_lines, fixed = TRUE))) expect_true(any(grepl("[.]here", rbuildignore_lines))) - + dev_lines <- readLines(flat_file) expect_equal(length(grep("minimal", dev_lines)), 2) }) @@ -348,23 +366,23 @@ dir.create(dummypackage) pkg_name <- basename(dummypackage) # add_additional test_that("add_additional adds flat_additional.Rmd", { - dev_file_path <- expect_error(add_additional(pkg = dummypackage, open = FALSE), - regexp = NA) + dev_file_path <- expect_error(add_additional(pkg = dummypackage, open = FALSE), + regexp = NA + ) flat_file <- dev_file_path[grepl("flat_", dev_file_path)] - + expect_true(all(file.exists(dev_file_path))) expect_false(file.exists(file.path(dummypackage, "dev", "0-dev_history.Rmd"))) expect_true(file.exists(file.path(dummypackage, ".here"))) expect_false(file.exists(file.path(dummypackage, "inst", "nyc_squirrels_sample.csv"))) - + rbuildignore_file <- file.path(dummypackage, ".Rbuildignore") expect_true(file.exists(rbuildignore_file)) rbuildignore_lines <- readLines(rbuildignore_file) expect_true(any(grepl("^dev$", rbuildignore_lines, fixed = TRUE))) expect_true(any(grepl("[.]here", rbuildignore_lines))) - + dev_lines <- readLines(flat_file) expect_equal(length(grep("additional", dev_lines)), 2) }) unlink(dummypackage, recursive = TRUE) - diff --git a/tests/testthat/test-build_fusen_chunks.R b/tests/testthat/test-build_fusen_chunks.R index 3161a6a0..2a6ae8f2 100644 --- a/tests/testthat/test-build_fusen_chunks.R +++ b/tests/testthat/test-build_fusen_chunks.R @@ -52,9 +52,9 @@ test_that("build_fusen_chunks add lines as expected", { # If interactive in RStudio if ( requireNamespace("rstudioapi") && - rstudioapi::isAvailable() && - rstudioapi::hasFun("navigateToFile") && - rstudioapi::hasFun("documentSave") #&& + rstudioapi::isAvailable() && + rstudioapi::hasFun("navigateToFile") && + rstudioapi::hasFun("documentSave") # && # rstudioapi::hasFun("documentClose") ) { print("Test with RStudio") @@ -70,21 +70,21 @@ test_that("build_fusen_chunks add lines as expected", { # if (basename(the_open_path) != basename(path_dev_history)) { # stop("Open the file was short to get the correct Id of the opened file") # } - # add chunks - add_fusen_chunks(function_name = "zaza", export = TRUE) - rstudioapi::documentSave(id) - if (rstudioapi::hasFun("documentClose")) { - rstudioapi::documentClose(id) - } - # # Back to current position - rstudioapi::navigateToFile(curr_editor$path, line = curr_position[1]) + # add chunks + add_fusen_chunks(function_name = "zaza", export = TRUE) + rstudioapi::documentSave(id) + if (rstudioapi::hasFun("documentClose")) { + rstudioapi::documentClose(id) + } + # # Back to current position + rstudioapi::navigateToFile(curr_editor$path, line = curr_position[1]) } else { print("test without interactive") dev_lines <- readLines(path_dev_history) dev_lines[40] <- build_fusen_chunks("zaza", export = TRUE) cat(dev_lines, sep = "\n", file = path_dev_history) } - + dev_lines_new <- readLines(path_dev_history) expect_equal(length(dev_lines_new), length(dev_lines_orig) + 24) expect_equal(dev_lines_new[40], "# zaza") @@ -93,7 +93,7 @@ test_that("build_fusen_chunks add lines as expected", { expect_equal(dev_lines_new[55], "```{r example-zaza}") expect_equal(dev_lines_new[56], "zaza()") expect_equal(dev_lines_new[59], "```{r tests-zaza}") - + unlink(path_foosen, recursive = TRUE) }) }) @@ -109,13 +109,13 @@ test_that("build_fusen_chunks add lines as expected", { fill_description(pkg = path_foosen, fields = list(Title = "Dummy Package")) path_dev_history <- dev_file[grepl("flat", dev_file)] dev_lines_orig <- readLines(path_dev_history) - + # If interactive in RStudio if ( requireNamespace("rstudioapi") && - rstudioapi::isAvailable() && - rstudioapi::hasFun("navigateToFile") && - rstudioapi::hasFun("documentSave") #&& + rstudioapi::isAvailable() && + rstudioapi::hasFun("navigateToFile") && + rstudioapi::hasFun("documentSave") # && # rstudioapi::hasFun("documentClose") ) { print("Test with RStudio") @@ -145,7 +145,7 @@ test_that("build_fusen_chunks add lines as expected", { dev_lines[40] <- build_fusen_chunks("zaza", export = FALSE) cat(dev_lines, sep = "\n", file = path_dev_history) } - + dev_lines_new <- readLines(path_dev_history) expect_equal(length(dev_lines_new), length(dev_lines_orig) + 24) expect_equal(dev_lines_new[40], "# zaza") diff --git a/tests/testthat/test-create_fusen_rsproject.R b/tests/testthat/test-create_fusen_rsproject.R index fff2fb01..e4cf6b66 100644 --- a/tests/testthat/test-create_fusen_rsproject.R +++ b/tests/testthat/test-create_fusen_rsproject.R @@ -111,14 +111,14 @@ if (git_output != 0) { path_gigit <- file.path(dummypackage, "gigit") expect_warning( create_fusen(path_gigit, template = "full", open = FALSE, with_git = TRUE), - regexp = "Error initializing git repository") + regexp = "Error initializing git repository" + ) expect_true(dir.exists(path_gigit)) expect_true(all(file.exists(c( file.path(path_gigit, "dev", "flat_full.Rmd"), file.path(path_gigit, "dev", "0-dev_history.Rmd") - ) - ))) + )))) # git is not initialized expect_false(dir.exists(file.path(path_gigit, ".git"))) }) @@ -139,8 +139,7 @@ if (git_output != 0) { expect_true(all(file.exists(c( file.path(path_gigit, "dev", "flat_full.Rmd"), file.path(path_gigit, "dev", "0-dev_history.Rmd") - ) - ))) + )))) # git is initialized expect_true(dir.exists(file.path(path_gigit, ".git"))) }) @@ -159,8 +158,7 @@ if (git_output != 0) { expect_true(all(file.exists(c( file.path(path_gigit, "dev", "flat_full.Rmd"), file.path(path_gigit, "dev", "0-dev_history.Rmd") - ) - ))) + )))) # git is not initialized expect_false(dir.exists(file.path(path_gigit, ".git"))) }) @@ -216,7 +214,8 @@ withr::with_dir(dummypackage, { path_bad_name <- file.path(dummypackage, "bad-name") expect_error( create_fusen(path_bad_name, template = "full", open = FALSE), - regexp = "Please rename") + regexp = "Please rename" + ) expect_false(dir.exists(path_bad_name)) }) diff --git a/tests/testthat/test-fill_description.R b/tests/testthat/test-fill_description.R index 7dbc5fed..ddad3662 100644 --- a/tests/testthat/test-fill_description.R +++ b/tests/testthat/test-fill_description.R @@ -4,7 +4,6 @@ dir.create(dummypackage) pkg_name <- basename(dummypackage) test_that("fill_description adds DESCRIPTION", { - expect_message( fill_description( pkg = dummypackage, @@ -29,7 +28,9 @@ test_that("fill_description adds DESCRIPTION", { expect_message( fill_description( pkg = dummypackage, fields = list(Title = "Second launch") - ))) + ) + ) + ) lines <- readLines(file.path(dummypackage, "DESCRIPTION")) expect_true(lines[1] == paste0("Package: ", pkg_name)) @@ -46,25 +47,32 @@ test_that("no dot description fails", { pkg = dummypackage, fields = list( Title = "Build A Package From Rmarkdown file", - Description = paste("Use Rmd First method to build your package.", - "Start your package with documentation.", - "Everything can be set from a Rmarkdown file in your project"), + Description = paste( + "Use Rmd First method to build your package.", + "Start your package with documentation.", + "Everything can be set from a Rmarkdown file in your project" + ), `Authors@R` = c( - person("Sebastien", "Rochette", email = "sebastien@thinkr.fr", - role = c("aut", "cre"), comment = c(ORCID = "0000-0002-1565-9313")), + person("Sebastien", "Rochette", + email = "sebastien@thinkr.fr", + role = c("aut", "cre"), comment = c(ORCID = "0000-0002-1565-9313") + ), person(given = "ThinkR", role = "cph") ) ), overwrite = TRUE ), - "A dot was added.") + "A dot was added." + ) lines <- readLines(file.path(dummypackage, "DESCRIPTION")) # Description with dot expect_equal( lines[4], - paste("Description: Use Rmd First method to build your package.", - "Start your package with documentation.", - "Everything can be set from a Rmarkdown file in your project.") + paste( + "Description: Use Rmd First method to build your package.", + "Start your package with documentation.", + "Everything can be set from a Rmarkdown file in your project." + ) ) }) @@ -76,18 +84,18 @@ dummypackage <- tempfile(pattern = "dummy") dir.create(dummypackage) test_that("curly bracket in title and description works", { -# Works with {} in text although not allowed by CRAN -expect_message(fill_description( - pkg = dummypackage, - fields = list( - Title = "Build a package with {fusen}", - Description = "Use Rmarkdown First method to build your package with {fusen}.", - `Authors@R` = c( - person("Sebastien", "Rochette", email = "sebastien@thinkr.fr", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-1565-9313")), - person(given = "ThinkR", role = "cph") + # Works with {} in text although not allowed by CRAN + expect_message(fill_description( + pkg = dummypackage, + fields = list( + Title = "Build a package with {fusen}", + Description = "Use Rmarkdown First method to build your package with {fusen}.", + `Authors@R` = c( + person("Sebastien", "Rochette", email = "sebastien@thinkr.fr", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-1565-9313")), + person(given = "ThinkR", role = "cph") + ) ) - ) -), "Title Case") + ), "Title Case") }) # Delete dummy package diff --git a/tests/testthat/test-inflate-part1.R b/tests/testthat/test-inflate-part1.R index 20cc82cb..63b73cdb 100644 --- a/tests/testthat/test-inflate-part1.R +++ b/tests/testthat/test-inflate-part1.R @@ -19,9 +19,11 @@ usethis::with_project(dummypackage, { ) suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "Get started", check = FALSE, - open_vignette = FALSE) + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "Get started", check = FALSE, + open_vignette = FALSE + ) ) test_that("inflate() worked correctly", { @@ -98,12 +100,12 @@ usethis::with_project(dummypackage, { # _extra empty line and examples my_space_lines <- readLines(my_spacefunctionfile) expect_true(all(my_space_lines[8:12] == c( - "#' @examples", "#' my_space(10)", "#' @export", "", "my_space <- function(x) {" + "#' @examples", "#' my_space(10)", "#' @export", "", "my_space <- function(x) {" ))) # _extra empty line and noRd my_space2_lines <- readLines(my_space2functionfile) expect_true(all(my_space2_lines[8:10] == c( - "#' @noRd", "", "my_space2 <- function(x) {" + "#' @noRd", "", "my_space2 <- function(x) {" ))) # vignette @@ -112,9 +114,10 @@ usethis::with_project(dummypackage, { vignette_lines <- readLines(the_vignette) # Do not edit by hand after yaml pos.dash <- grep("---", vignette_lines) - donotedit <-grep( + donotedit <- grep( "", - vignette_lines) + vignette_lines + ) expect_equal(length(donotedit), 1) expect_true(pos.dash[2] < donotedit) # No dev chunks in the vignette @@ -132,7 +135,8 @@ usethis::with_project(dummypackage, { "", "test_that(\"my_median works properly and show error if needed\", {", " expect_error(my_median(\"text\"))", - "})") + "})" + ) ) expect_true(file.exists( file.path(dummypackage, "tests", "testthat", "test-my_other_median.R") @@ -163,8 +167,10 @@ usethis::with_project(dummypackage, { skip_on_os(os = c("windows", "solaris")) # If this check is run inside a not "--as-cran" check, then it wont work as expected - check_out <- rcmdcheck::rcmdcheck(dummypackage, quiet = TRUE, - args = c("--no-manual")) + check_out <- rcmdcheck::rcmdcheck(dummypackage, + quiet = TRUE, + args = c("--no-manual") + ) # No errors expect_true(length(check_out[["errors"]]) == 0) @@ -199,7 +205,6 @@ if (packageVersion("parsermd") > "0.1.2") { flat_file <- dev_file[grepl("flat_", dev_file)] usethis::with_project(dummypackage.special, { - testfile <- "tests-templates/dev-template-tests-special-char.Rmd" file.copy( system.file(testfile, package = "fusen"), @@ -208,9 +213,11 @@ if (packageVersion("parsermd") > "0.1.2") { ) suppressMessages( - inflate(pkg = dummypackage.special, flat_file = flat_file, - vignette_name = "Get started", check = FALSE, - open_vignette = FALSE) + inflate( + pkg = dummypackage.special, flat_file = flat_file, + vignette_name = "Get started", check = FALSE, + open_vignette = FALSE + ) ) test_that("inflate with special yaml worked correctly", { @@ -229,8 +236,10 @@ flat_file <- dev_file[grepl("flat_", dev_file)] usethis::with_project(dummypackage, { suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, vignette_name = "Get started", - check = FALSE, document = FALSE, open_vignette = FALSE) + inflate( + pkg = dummypackage, flat_file = flat_file, vignette_name = "Get started", + check = FALSE, document = FALSE, open_vignette = FALSE + ) ) desc_lines <- readLines(file.path(dummypackage, "DESCRIPTION")) @@ -253,9 +262,11 @@ usethis::with_project(dummypackage, { overwrite = TRUE ) suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "Get started", check = FALSE, - open_vignette = FALSE) + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "Get started", check = FALSE, + open_vignette = FALSE + ) ) test_that("inflate() output no error", { @@ -279,9 +290,11 @@ usethis::with_project(dummypackage, { test_that("inflate() output message", { suppressMessages( expect_message( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "Get started", check = FALSE, - open_vignette = FALSE) + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "Get started", check = FALSE, + open_vignette = FALSE + ) ) ) }) @@ -294,33 +307,43 @@ usethis::with_project(dummypackage, { # Tests errors - vignette already exists ---- usethis::with_project(dummypackage, { suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "Get started", - check = FALSE, overwrite = "yes", - open_vignette = FALSE) + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "Get started", + check = FALSE, overwrite = "yes", + open_vignette = FALSE + ) ) test_that("inflate() output error when second time (not interactive)", { expect_error( suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "Get started", - check = FALSE, - open_vignette = FALSE)) + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "Get started", + check = FALSE, + open_vignette = FALSE + ) + ) ) expect_error( suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "Get started", - check = FALSE, overwrite = 'no', - open_vignette = FALSE)) + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "Get started", + check = FALSE, overwrite = "no", + open_vignette = FALSE + ) + ) ) }) # No error with overwrite = 'yes' suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, vignette_name = "Get started", - check = FALSE, overwrite = "yes", open_vignette = FALSE) + inflate( + pkg = dummypackage, flat_file = flat_file, vignette_name = "Get started", + check = FALSE, overwrite = "yes", open_vignette = FALSE + ) ) test_that("inflate() output no error", { @@ -343,9 +366,12 @@ usethis::with_project(dummypackage, { test_that("inflate() output error duplicate functions", { expect_error( suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "Get started", check = FALSE, - open_vignette = FALSE)) + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "Get started", check = FALSE, + open_vignette = FALSE + ) + ) ) }) # Clean R, tests and vignettes @@ -362,9 +388,11 @@ usethis::with_project(dummypackage, { test_that("inflate() output error duplicate label names for vignette", { expect_error( suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "Get started", check = FALSE, - open_vignette = FALSE) + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "Get started", check = FALSE, + open_vignette = FALSE + ) ) ) }) @@ -378,15 +406,17 @@ usethis::with_project(dummypackage, { usethis::with_project(dummypackage, { file.remove(file.path(dummypackage, ".here")) file.remove(file.path(dummypackage, ".Rbuildignore")) - cat("", file = file.path(dummypackage, 'dummy.Rproj')) + cat("", file = file.path(dummypackage, "dummy.Rproj")) # Add # {fusen} steps dev_file <- add_flat_template(pkg = dummypackage, overwrite = TRUE, open = FALSE) suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "Get started", check = FALSE, - open_vignette = FALSE) + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "Get started", check = FALSE, + open_vignette = FALSE + ) ) test_that("add_flat_template inflates with .Rproj and no .here", { @@ -430,9 +460,11 @@ flat_file <- dev_file[grepl("flat_", dev_file)] usethis::with_project(dummypackage, { suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "# y _ p n@ \u00E9 ! 1", check = FALSE, - open_vignette = FALSE) + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "# y _ p n@ \u00E9 ! 1", check = FALSE, + open_vignette = FALSE + ) ) # Vignette name is also cleaned by {usethis} for special characters vignette_path <- file.path(dummypackage, "vignettes", "y-p-n-e-1.Rmd") @@ -441,9 +473,9 @@ usethis::with_project(dummypackage, { expect_true(file.exists(vignette_path)) # usethis::use_vignette writes in UTF-8 vig_lines <- readLines(vignette_path, encoding = "UTF-8") - expect_true(sum(grepl(enc2utf8("# y _ p n@ \u00E9 ! 1"), vig_lines, fixed = TRUE)) == 2) + expect_true(sum(grepl(enc2utf8("# y _ p n@ \u00E9 ! 1"), vig_lines, fixed = TRUE)) == 1) expect_equal(vig_lines[2], enc2utf8('title: "# y _ p n@ \u00E9 ! 1"')) - expect_true(sum(grepl("y-p-n-e-1", vig_lines, fixed = TRUE)) == 0) + expect_true(sum(grepl("y-p-n-e-1", vig_lines, fixed = TRUE)) == 1) }) # Clean R, tests and vignettes @@ -465,10 +497,13 @@ usethis::with_project(dummypackage, { test_that("stop when no DESCRIPTION file", { expect_error( suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, - check = FALSE, open_vignette = FALSE) + inflate( + pkg = dummypackage, flat_file = flat_file, + check = FALSE, open_vignette = FALSE + ) ), - "DESCRIPTION file") + "DESCRIPTION file" + ) }) }) @@ -494,9 +529,9 @@ flat_file <- dev_file[grepl("flat_", dev_file)] # If interactive in RStudio if ( requireNamespace("rstudioapi") && - rstudioapi::isAvailable() && - rstudioapi::hasFun("navigateToFile") && - rstudioapi::hasFun("documentId") + rstudioapi::isAvailable() && + rstudioapi::hasFun("navigateToFile") && + rstudioapi::hasFun("documentId") ) { print("Test with RStudio") # current position @@ -510,9 +545,11 @@ if ( usethis::with_project(dummypackage, { test_that("inflate works without flat file when current is Rmd", { expect_message( - inflate(pkg = dummypackage, #flat_file = flat_file, - vignette_name = "Get started", check = FALSE, - open_vignette = FALSE, overwrite = TRUE), + inflate( + pkg = dummypackage, # flat_file = flat_file, + vignette_name = "Get started", check = FALSE, + open_vignette = FALSE, overwrite = TRUE + ), regexp = "The current file will be inflated" ) }) diff --git a/tests/testthat/test-inflate-part2.R b/tests/testthat/test-inflate-part2.R index de9aef1c..95d88cb8 100644 --- a/tests/testthat/test-inflate-part2.R +++ b/tests/testthat/test-inflate-part2.R @@ -21,10 +21,13 @@ usethis::with_project(dummypackage, { # No error expect_error( suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, - check = FALSE, open_vignette = FALSE) + inflate( + pkg = dummypackage, flat_file = flat_file, + check = FALSE, open_vignette = FALSE + ) ), - regexp = NA) + regexp = NA + ) # Check files expect_equal(length(list.files(file.path(dummypackage, "R"))), 1) @@ -36,8 +39,10 @@ usethis::with_project(dummypackage, { skip_on_cran() # Could not find function "my_norox2 in the vignette ? - expect_error(rcmdcheck::rcmdcheck(dummypackage, quiet = TRUE, - args = c("--no-manual"))) + expect_error(rcmdcheck::rcmdcheck(dummypackage, + quiet = TRUE, + args = c("--no-manual") + )) }) }) @@ -56,16 +61,14 @@ for (pkgname in c("full", "teaching", "minimal")) { flat_file <- dev_file[grepl("flat_", dev_file)] usethis::with_project(path_foosen, { - - # Do not check inside check if on CRAN - # skip_on_os(os = c("windows", "solaris")) - skip_on_cran() - - fill_description(pkg = path_foosen, fields = list(Title = "Dummy Package")) usethis::use_gpl_license() test_that(paste("Check returns OK for template", pkgname), { + # Do not check inside check if on CRAN + # skip_on_os(os = c("windows", "solaris")) + skip_on_cran() + # extract the 'inflate' line in the flat file # Add pkg, check, quiet, args, overwrite # And inflate @@ -90,7 +93,8 @@ for (pkgname in c("full", "teaching", "minimal")) { suppressMessages( eval(parse(text = to_inflate)) ), - regexp = NA) + regexp = NA + ) # Run rcmdcheck # Do not check inside check if on CRAN @@ -98,8 +102,10 @@ for (pkgname in c("full", "teaching", "minimal")) { skip_on_cran() # If this check is run inside a not "--as-cran" check, then it wont work as expected - check_out <- rcmdcheck::rcmdcheck(path_foosen, quiet = TRUE, - args = c("--no-manual")) + check_out <- rcmdcheck::rcmdcheck(path_foosen, + quiet = TRUE, + args = c("--no-manual") + ) # No errors expect_true(length(check_out[["errors"]]) == 0) @@ -130,7 +136,8 @@ for (pkgname in c("full", "teaching", "minimal")) { suppressMessages( eval(parse(text = to_inflate)) ), - regexp = NA) + regexp = NA + ) } skip_if_not(interactive()) @@ -149,7 +156,8 @@ for (pkgname in c("full", "teaching", "minimal")) { suppressMessages( eval(parse(text = to_inflate)) ), - regexp = NA) + regexp = NA + ) # Should not be any errors with templates check_lines <- readLines(file.path(checkdir, paste0(basename(path_foosen), ".Rcheck"), "00check.log")) @@ -158,7 +166,6 @@ for (pkgname in c("full", "teaching", "minimal")) { }) # }) }) - } # end of template loop # Delete dummy package unlink(alltemp, recursive = TRUE) @@ -185,7 +192,6 @@ usethis::with_project(dummypackage, { # skip_on_cran() test_that("included data can be read", { - datafile <- file.path(dummypackage, "inst", "nyc_squirrels_sample.csv") expect_true(file.exists(datafile)) @@ -196,7 +202,7 @@ usethis::with_project(dummypackage, { flatlines_chunk <- grep("```", flatlines) flatlines_chunk_data <- grep("```{r development-dataset}", flatlines, fixed = TRUE) flatlines_chunk_data_end <- flatlines_chunk[flatlines_chunk > flatlines_chunk_data][1] - lines_data <- flatlines[(flatlines_chunk_data+1):(flatlines_chunk_data_end-1)] + lines_data <- flatlines[(flatlines_chunk_data + 1):(flatlines_chunk_data_end - 1)] # Can read data @@ -250,15 +256,18 @@ usethis::with_project(dummypackage, { suppressMessages( eval(parse(text = to_inflate)) ), - regexp = NA) + regexp = NA + ) # Should not be any errors with templates # check_lines <- readLines(file.path(checkdir, paste0(basename(dummypackage), ".Rcheck"), "00check.log")) # expect_equal(check_lines[length(check_lines)], "Status: OK") # If this check is run inside a not "--as-cran" check, then it wont work as expected - check_out <- rcmdcheck::rcmdcheck(dummypackage, quiet = TRUE, - args = c("--no-manual")) + check_out <- rcmdcheck::rcmdcheck(dummypackage, + quiet = TRUE, + args = c("--no-manual") + ) skip_on_cran() # No errors @@ -269,7 +278,6 @@ usethis::with_project(dummypackage, { } unlink(checkdir, recursive = TRUE) - }) }) @@ -302,15 +310,20 @@ usethis::with_project(dummypackage, { test_that("inflate() output no error", { expect_error( suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "Get started", check = FALSE, - open_vignette = FALSE)), + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "Get started", check = FALSE, + open_vignette = FALSE + ) + ), regexp = NA ) # R files with chunk content - Name after title as function name is NA - expect_equal(list.files(file.path(dummypackage, "R")), - c("my-data-doc.R", "my-pkg-doc.R", "onload.R")) + expect_equal( + list.files(file.path(dummypackage, "R")), + c("my-data-doc.R", "my-pkg-doc.R", "onload.R") + ) pkgdoc <- file.path(dummypackage, "R", "my-pkg-doc.R") expect_true(file.exists(pkgdoc)) pkgdoc_lines <- readLines(pkgdoc) @@ -341,10 +354,13 @@ usethis::with_project(dummypackage, { if (!interactive()) { expect_error( suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "Get started", check = FALSE, - quiet = TRUE, - overwrite = TRUE, open_vignette = FALSE)), + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "Get started", check = FALSE, + quiet = TRUE, + overwrite = TRUE, open_vignette = FALSE + ) + ), regexp = NA ) @@ -352,9 +368,11 @@ usethis::with_project(dummypackage, { skip_on_os(os = c("windows", "solaris")) # If this check is run inside a not "--as-cran" check, then it wont work as expected - check_out <- rcmdcheck::rcmdcheck(dummypackage, quiet = TRUE, - args = c("--no-manual"), - check_dir = checkdir) + check_out <- rcmdcheck::rcmdcheck(dummypackage, + quiet = TRUE, + args = c("--no-manual"), + check_dir = checkdir + ) # No errors expect_true(length(check_out[["errors"]]) == 0) @@ -370,10 +388,13 @@ usethis::with_project(dummypackage, { } else { expect_error( suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "Get started", check = TRUE, - check_dir = checkdir, quiet = TRUE, - overwrite = TRUE, open_vignette = FALSE)), + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "Get started", check = TRUE, + check_dir = checkdir, quiet = TRUE, + overwrite = TRUE, open_vignette = FALSE + ) + ), regexp = NA ) @@ -382,8 +403,6 @@ usethis::with_project(dummypackage, { expect_equal(check_lines[length(check_lines)], "Status: OK") unlink(checkdir, recursive = TRUE) } - - }) }) unlink(dummypackage, recursive = TRUE) @@ -410,11 +429,14 @@ usethis::with_project(dummypackage, { test_that("inflate() output no error with R6", { expect_error( suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "Get started", check = FALSE, - open_vignette = FALSE, - # To avoid having {R6} in suggests - document = FALSE)), + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "Get started", check = FALSE, + open_vignette = FALSE, + # To avoid having {R6} in suggests + document = FALSE + ) + ), regexp = NA ) @@ -447,22 +469,26 @@ flat_file <- dev_file[grepl("flat_", dev_file)] test_that("rmd and name are deprecated works", { usethis::with_project(dummypackage, { expect_warning( - inflate(pkg = ".", - # flat_file = flat_file, - rmd = flat_file, - vignette_name = "Get started", - check = FALSE, document = TRUE, - overwrite = TRUE, open_vignette = FALSE), - regexp = 'The `rmd` argument' + inflate( + pkg = ".", + # flat_file = flat_file, + rmd = flat_file, + vignette_name = "Get started", + check = FALSE, document = TRUE, + overwrite = TRUE, open_vignette = FALSE + ), + regexp = "The `rmd` argument" ) expect_warning( - inflate(pkg = ".", - flat_file = flat_file, - # vignette_name = "Get started", - name = "Get started", - check = FALSE, document = TRUE, - overwrite = TRUE, open_vignette = FALSE), - regexp = 'The `name` argument' + inflate( + pkg = ".", + flat_file = flat_file, + # vignette_name = "Get started", + name = "Get started", + check = FALSE, document = TRUE, + overwrite = TRUE, open_vignette = FALSE + ), + regexp = "The `name` argument" ) }) }) @@ -478,37 +504,44 @@ dev_file <- suppressMessages(add_flat_template(pkg = dummypackage, overwrite = T flat_file <- dev_file[grepl("flat_", dev_file)] usethis::with_project(dummypackage, { - test_that("inflate() worked correctly", { expect_message( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = NA, check = FALSE, - open_vignette = FALSE), + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = NA, check = FALSE, + open_vignette = FALSE + ), regexp = "no vignette created" ) expect_equal(length(list.files(file.path(dummypackage, "vignettes"))), 0) expect_message( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = NULL, check = FALSE, - open_vignette = FALSE), + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = NULL, check = FALSE, + open_vignette = FALSE + ), regexp = "no vignette created" ) expect_equal(length(list.files(file.path(dummypackage, "vignettes"))), 0) expect_message( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "", check = FALSE, - open_vignette = FALSE), + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "", check = FALSE, + open_vignette = FALSE + ), regexp = "no vignette created" ) expect_equal(length(list.files(file.path(dummypackage, "vignettes"))), 0) expect_error( suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "It works then", check = FALSE, - open_vignette = FALSE) + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "It works then", check = FALSE, + open_vignette = FALSE + ) ), regexp = NA ) @@ -537,9 +570,11 @@ usethis::with_project(dummypackage, { overwrite = TRUE ) suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "Get started", check = FALSE, - open_vignette = FALSE) + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "Get started", check = FALSE, + open_vignette = FALSE + ) ) test_that("inflate() worked correctly", { @@ -574,22 +609,30 @@ usethis::with_project(dummypackage, { expect_true(any(grepl("my_median <- function", r_lines))) expect_true(any(grepl("my_median2 <- function", r_lines))) # example at the right place - expect_equal(r_lines[12:14], - c("#' @examples", "#' my_median(2:20)" , "#' my_median(1:12)") + expect_equal( + r_lines[12:14], + c("#' @examples", "#' my_median(2:20)", "#' my_median(1:12)") ) - expect_equal(r_lines[29:31], - c("#' @examples", "#' my_median2(2:20)" , "#' my_median2(1:12)") + expect_equal( + r_lines[29:31], + c("#' @examples", "#' my_median2(2:20)", "#' my_median2(1:12)") ) # Same rdname r_lines <- readLines(my_rdname1_file) expect_true(any(grepl("my_fun_rdname1 <- function", r_lines))) expect_true(any(grepl("my_fun_rdname2 <- function", r_lines))) - expect_equal(r_lines[13:15], - c("#' @examples" , "#' my_fun_rdname1(2:20)", "#' my_fun_rdname1(1:12)")) - expect_equal(r_lines[21:25], - c("#' @rdname same_rdname" , - "#' @importFrom stats median", "#' @export", - "#' @examples", "#' my_fun_rdname2(1:12)" )) + expect_equal( + r_lines[13:15], + c("#' @examples", "#' my_fun_rdname1(2:20)", "#' my_fun_rdname1(1:12)") + ) + expect_equal( + r_lines[21:25], + c( + "#' @rdname same_rdname", + "#' @importFrom stats median", "#' @export", + "#' @examples", "#' my_fun_rdname2(1:12)" + ) + ) # Same chunk name r_lines <- readLines(my_chunk1_file) expect_true(any(grepl("my_fun_chunk1 <- function", r_lines))) @@ -689,9 +732,11 @@ usethis::with_project(dummypackage, { ) suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "Get started", check = FALSE, - open_vignette = FALSE) + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "Get started", check = FALSE, + open_vignette = FALSE + ) ) test_that("author and date worked correctly", { @@ -701,13 +746,17 @@ usethis::with_project(dummypackage, { content_vignette <- readLines(the_vignette) expect_true(any( - grepl("^author: ", content_vignette, fixed = FALSE))) + grepl("^author: ", content_vignette, fixed = FALSE) + )) expect_true(any( - grepl("author: \"S\\u00e9bastien Rochette\"", content_vignette, fixed = TRUE))) + grepl("author: \"S\\u00e9bastien Rochette\"", content_vignette, fixed = TRUE) + )) expect_true(any( - grepl("^date: ", content_vignette, fixed = FALSE))) + grepl("^date: ", content_vignette, fixed = FALSE) + )) expect_true(any( - grepl("date: \"`r Sys.Date()`\"", content_vignette, fixed = TRUE))) + grepl("date: \"`r Sys.Date()`\"", content_vignette, fixed = TRUE) + )) }) }) @@ -732,9 +781,11 @@ usethis::with_project(dummypackage, { ) suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "Get started", check = FALSE, - open_vignette = FALSE) + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "Get started", check = FALSE, + open_vignette = FALSE + ) ) test_that("unit tests only worked correctly", { @@ -778,11 +829,13 @@ usethis::with_project(dummypackage, { # no title may return expect_error( suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "Get started", check = FALSE, - open_vignette = FALSE) + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "Get started", check = FALSE, + open_vignette = FALSE + ) ), - regexp = NA #"Some `test` chunks can not be handled: tests-fails." + regexp = NA # "Some `test` chunks can not be handled: tests-fails." ) # Check tests do not exist @@ -825,11 +878,13 @@ usethis::with_project(dummypackage, { # no title may return expect_error( suppressMessages( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "Get started", check = FALSE, - open_vignette = FALSE) + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "Get started", check = FALSE, + open_vignette = FALSE + ) ), - regexp = NA #"Some `test` chunks can not be handled: tests-fails." + regexp = NA # "Some `test` chunks can not be handled: tests-fails." ) # Check tests do not exist @@ -873,9 +928,11 @@ usethis::with_project(dummypackage, { test_that("unit tests and examples only works", { # no title may return expect_message( - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "Get started", check = FALSE, - open_vignette = FALSE), + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "Get started", check = FALSE, + open_vignette = FALSE + ), regexp = "Some example chunks are not associated to any function" ) @@ -901,5 +958,32 @@ usethis::with_project(dummypackage, { unlink(dummypackage, recursive = TRUE) +# vignette name and slug independent ---- +dummypackage <- tempfile("vignette.name") +dir.create(dummypackage) + +# {fusen} steps +fill_description(pkg = dummypackage, fields = list(Title = "Dummy Package")) +dev_file <- suppressMessages(add_flat_template(pkg = dummypackage, overwrite = TRUE, open = FALSE)) +flat_file <- dev_file[grepl("flat_", dev_file)] + +usethis::with_project(dummypackage, { + suppressMessages( + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = c("Super title" = "01-Super Slug"), check = FALSE, + open_vignette = FALSE + ) + ) + the_vignette <- file.path(dummypackage, "vignettes", "01-super-slug.Rmd") + test_that("vignette header is good", { + expect_true(file.exists(the_vignette)) + the_vignette_lines <- readLines(the_vignette) + expect_true(grepl("title:.*Super title.*", the_vignette_lines[2])) + expect_false(any(grep("01-Super Slug", the_vignette_lines))) + expect_true(any(grep("01-super-slug", the_vignette_lines))) + }) +}) +unlink(dummypackage, recursive = TRUE) diff --git a/tests/testthat/test-inflate_utils.R b/tests/testthat/test-inflate_utils.R index eb741b2c..fd3a55c7 100644 --- a/tests/testthat/test-inflate_utils.R +++ b/tests/testthat/test-inflate_utils.R @@ -5,7 +5,8 @@ df <- tibble::tibble( id = c(1, 2, 3), the_group = c("A", "A", "B"), the_code = list( - c("text 1.1", "text 1.2"), c("text 2.1", "text 2.2"), c("text 3.1", "text 3.2")) + c("text 1.1", "text 1.2"), c("text 2.1", "text 2.2"), c("text 3.1", "text 3.2") + ) ) test_that("group_code groups columns", { @@ -13,10 +14,13 @@ test_that("group_code groups columns", { expect_equal(nrow(df_grouped), 2) expect_equal(df_grouped[["id"]], c(1, 3)) expect_equal(df_grouped[["the_group"]], c("A", "B")) - expect_equal(df_grouped[["the_code"]], - list( - c("text 1.1", "text 1.2", "", "text 2.1", "text 2.2"), - c("text 3.1", "text 3.2"))) + expect_equal( + df_grouped[["the_code"]], + list( + c("text 1.1", "text 1.2", "", "text 2.1", "text 2.2"), + c("text 3.1", "text 3.2") + ) + ) }) # Test is_pkg_proj ---- @@ -35,16 +39,18 @@ usethis::with_project(dummypackage, { }) file.remove(file.path(dummypackage, ".here")) - cat("", file = file.path(dummypackage, 'dummy.Rproj')) + cat("", file = file.path(dummypackage, "dummy.Rproj")) test_that("is_pkg_proj works when rproj no pkg", { expect_equal(is_pkg_proj(dummypackage), FALSE) }) # create_vignette usethis::with_project(dummypackage, { - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "Get started", check = FALSE, - open_vignette = FALSE) + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "Get started", check = FALSE, + open_vignette = FALSE + ) # See RStudio restart needed }) @@ -52,7 +58,7 @@ usethis::with_project(dummypackage, { expect_equal(is_pkg_proj(dummypackage), FALSE) }) - cat("BuildType: Package", file = file.path(dummypackage, 'dummy.Rproj')) + cat("BuildType: Package", file = file.path(dummypackage, "dummy.Rproj")) test_that("is_pkg_proj works when rproj is pkg", { expect_equal(is_pkg_proj(dummypackage), TRUE) @@ -61,7 +67,8 @@ usethis::with_project(dummypackage, { # asciify_name ---- test_that( - "Diacritics are properly escaped in vignette file name", { + "Diacritics are properly escaped in vignette file name", + { vignette_name <- asciify_name( "\u00c0 l'or\u00e9e de l'\u00e2pre f\u00f4ret c\u00e9l\u00e8ste" ) @@ -71,7 +78,8 @@ test_that( "# y _ p n@ \u00E9 ! 1" ) expect_identical(vignette_name, "y-p-n-e-1") - }) + } +) # create_vignette_head ---- yaml_options <- structure(list( @@ -86,7 +94,8 @@ test_that("create_vignette_head works", { # Full with authors output <- create_vignette_head( pkg = "mypkg", vignette_name = "the_name", - yaml_options) + yaml_options + ) expect_true(grepl("mypkg", output)) expect_true(grepl("the_name", output)) @@ -98,7 +107,8 @@ test_that("create_vignette_head works", { yaml_options <- yaml_options[c("title", "output", "editor_options")] output <- create_vignette_head( pkg = "mypkg", vignette_name = "the_name", - yaml_options) + yaml_options + ) expect_true(grepl("mypkg", output)) expect_true(grepl("the_name", output)) @@ -109,14 +119,14 @@ test_that("create_vignette_head works", { # No yaml options output <- create_vignette_head( pkg = "mypkg", vignette_name = "the_name", - yaml_options = NULL) + yaml_options = NULL + ) expect_true(grepl("mypkg", output)) expect_true(grepl("the_name", output)) expect_false(grepl('author: "S\\u00e9bastien Rochette"', output, fixed = TRUE)) expect_false(grepl('date: "`r Sys.Date()`"', output, fixed = TRUE)) expect_true(grepl("vignette: >", output)) - }) # Test create_vignette_head ---- @@ -128,13 +138,14 @@ dir.create(dummypackage) # {fusen} steps test_that("create_vignette_head works", { - expect_error(fill_description(pkg = dummypackage, fields = list(Title = "Dummy Package")) - , regexp = NA) + expect_error(fill_description(pkg = dummypackage, fields = list(Title = "Dummy Package")), + regexp = NA + ) usethis::with_project(dummypackage, { head <- create_vignette_head(pkg = dummypackage, vignette_name = "My Super Vignette") expect_true(grepl('title: "My Super Vignette"', head)) - expect_true(grepl(' %\\VignetteIndexEntry{My Super Vignette}', head, fixed = TRUE)) - expect_true(grepl(paste0('library(', basename(dummypackage) ,')'), head, fixed = TRUE)) + expect_true(grepl(" %\\VignetteIndexEntry{my-super-vignette}", head, fixed = TRUE)) + expect_true(grepl(paste0("library(", basename(dummypackage), ")"), head, fixed = TRUE)) }) }) unlink(dummypackage, recursive = TRUE) @@ -151,7 +162,7 @@ flat_file <- dev_file[grepl("flat_", dev_file)] test_that("get_pkg_name works", { flat_file_lines <- readLines(flat_file) - #the package name must be used and not the project name + # the package name must be used and not the project name expect_true(any(grepl("COUCOU2", flat_file_lines, fixed = TRUE))) expect_false(any(grepl("gname2", flat_file_lines))) }) @@ -162,15 +173,16 @@ unlink(dummypackage, recursive = TRUE) dummypackage <- tempfile("checkpk.gname") dir.create(dummypackage) # {fusen} steps -fill_description(pkg = dummypackage, fields = list(Title = "Dummy Package",Package="COUCOU")) +fill_description(pkg = dummypackage, fields = list(Title = "Dummy Package", Package = "COUCOU")) dev_file <- suppressMessages(add_flat_template(pkg = dummypackage, overwrite = TRUE, open = FALSE)) flat_file <- dev_file[grepl("flat_", dev_file)] test_that("get_pkg_name inflates", { usethis::with_project(dummypackage, { - inflate(pkg = dummypackage, flat_file = flat_file, - vignette_name = "Get started", check = FALSE, - open_vignette = FALSE) + inflate( + pkg = dummypackage, flat_file = flat_file, + vignette_name = "Get started", check = FALSE, + open_vignette = FALSE + ) }) }) - diff --git a/tests/testthat/test-skeleton.R b/tests/testthat/test-skeleton.R index fe3798ac..ba4c1b04 100644 --- a/tests/testthat/test-skeleton.R +++ b/tests/testthat/test-skeleton.R @@ -2,9 +2,12 @@ test_that("rmardown skeleton exists", { skip_if_not(interactive()) withr::with_tempdir({ expect_error( - rmarkdown::draft(file = "flat_skeleton.Rmd", - template = "additional", - package = "fusen", edit = FALSE), - regexp = NA) + rmarkdown::draft( + file = "flat_skeleton.Rmd", + template = "additional", + package = "fusen", edit = FALSE + ), + regexp = NA + ) }) }) diff --git a/vignettes/tips-and-tricks.Rmd b/vignettes/tips-and-tricks.Rmd index 4f677b1d..a3b6a2cc 100644 --- a/vignettes/tips-and-tricks.Rmd +++ b/vignettes/tips-and-tricks.Rmd @@ -116,7 +116,15 @@ This won't affect your package. ``` ```` -# How not to create a vignette ? +# How to create a vignette with different title and Index Entry? + +You can use `inflate(vignette_name = c("Super title" = "01-Super Slug"))` for vignette title different from vignette Entry + +```{r, eval=FALSE} +inflate(vignette_name = c("Super title" = "01-Super Vignette Index Entry")) +``` + +# How not to create a vignette? - If I only build internal functions, I may not want to inflate the flat file as a vignette. - The flat file is the developer's documentation and that may be enough