Skip to content

Commit

Permalink
Merge pull request #148 from ThinkR-open/vignette-name
Browse files Browse the repository at this point in the history
Vignette name
  • Loading branch information
statnmap committed Apr 30, 2022
2 parents 67198b1 + f249ac3 commit 3013d03
Show file tree
Hide file tree
Showing 27 changed files with 900 additions and 596 deletions.
2 changes: 1 addition & 1 deletion 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")),
Expand Down
23 changes: 15 additions & 8 deletions 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
Expand All @@ -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)
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions R/add_dev_history.R
Expand Up @@ -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)

Expand Down
174 changes: 95 additions & 79 deletions R/add_flat_template.R
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
#'
Expand All @@ -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"))
Expand All @@ -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("<my_package_name>", lines_template)] <-
gsub("<my_package_name>", project_name,
lines_template[grepl("<my_package_name>", lines_template)])

gsub(
"<my_package_name>", project_name,
lines_template[grepl("<my_package_name>", 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"),
Expand All @@ -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")
Expand All @@ -213,15 +227,15 @@ 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) {
lines <- c(paste0("^", dev_dir, "$"), "^\\.here$")
} else {
lines <- c(paste0("^", dev_dir, "$"))
}

buildfile <- normalizePath(file.path(pkg, ".Rbuildignore"), mustWork = FALSE)
if (!file.exists(buildfile)) {
existing_lines <- ""
Expand All @@ -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 <- ""
Expand All @@ -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
}

0 comments on commit 3013d03

Please sign in to comment.