Skip to content

Commit 3013d03

Browse files
authored
Merge pull request #148 from ThinkR-open/vignette-name
Vignette name
2 parents 67198b1 + f249ac3 commit 3013d03

27 files changed

+900
-596
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: fusen
22
Title: Build a Package from Rmarkdown File
3-
Version: 0.3.0.9000
3+
Version: 0.4.0
44
Authors@R: c(
55
person("Sebastien", "Rochette", , "sebastien@thinkr.fr", role = c("aut", "cre"),
66
comment = c(ORCID = "0000-0002-1565-9313")),

NEWS.md

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
# fusen 0.3.0.9000
1+
# fusen 0.4.0
2+
3+
## New features
24

35
* `inflate()` the current opened flat file if `flat_file` is empty (#138)
6+
* Add rmarkdown template for additional flat file for RStudio
7+
* Add wrappers around `add_flat_template()` for lazy devs: `add_additional()`, `add_full()`, `add_minimal()`
8+
* Show "flat_template" origin of files generated by {fusen} (@
9+
ALanguillaume)
10+
* Allow `inflate(vignette_name = c("Super title" = "01-Super Slug"))` for nice Title different from vignette Entry (#87)
11+
* Get the author and date from flat file to the vignette (#129)
12+
13+
## Bug fixes
14+
415
* Read DESCRIPTION file for package name when available (#144 @VincentGuyader)
516
* Read `nyc_squirrels` with encoding to avoid encoding problems with `use_data()`
617
* Allow flat files with `tests` only
7-
* Add rmarkdown template for additional flat file for RStudio
818
* Extract yaml metadata from flat file, like author or date to include in the inflated vignette
9-
* Add wrappers around `add_flat_template()` for lazy devs: `add_additional()`, `add_full()`, `add_minimal()`
1019
* Simplify "flat_teaching" with a unique simple function
11-
* Show "flat_template" origin of files generated by {fusen} (@
12-
ALanguillaume)
1320
* Fix `asciify_name()` to account for diacritics (@
1421
ALanguillaume)
1522
* Improve template 'full' for internal data use
@@ -30,8 +37,8 @@ ALanguillaume)
3037
* Avoid creating vignette with `inflate(vignette_name = NA)`
3138
* Decide whether or not to open vignette when inflate with `inflate(open_vignette = FALSE)`
3239
* Improve documentation included in flat templates to reflect changes in using dev_history file
33-
* Add Rstudio Addin to insert a new flat template
34-
* Add Rstudio Addin to insert chunks for new function (@ColinFay)
40+
* Add RStudio Addin to insert a new flat template
41+
* Add RStudio Addin to insert chunks for new function (@ColinFay)
3542
* Deal with `\dontrun{}` in example chunks
3643
* Allow short names for chunks: dev, fun, ex, test
3744
* `create_fusen()` to create a {fusen} project from command line or with RStudio new project (@ALanguillaume)
@@ -54,7 +61,7 @@ ALanguillaume)
5461
* Fix inflate with empty functions chunks
5562
* Fix filename to inflate in templates with new calls of `add_dev_history()` (@Cervangirard)
5663
* Default vignette name is now "Get started" creating "vignettes/get-started.Rmd"
57-
* All open files are saved when using `inflate()` where {rstudioapi} works
64+
* All open files are saved when using `inflate()` where {RStudioapi} works
5865
* Ask to restart RStudio after first inflate
5966

6067
# fusen 0.2.4

R/add_dev_history.R

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
add_dev_history <- function(pkg = ".", overwrite = FALSE,
3333
open = TRUE, dev_dir = "dev",
3434
name = c("full", "minimal", "additional", "teaching")) {
35-
36-
.Deprecated('add_flat_template', package = 'fusen', old = 'add_dev_history')
35+
.Deprecated("add_flat_template", package = "fusen", old = "add_dev_history")
3736

3837
name <- match.arg(name)
3938

R/add_flat_template.R

+95-79
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,53 @@
22

33
#' @rdname add_flat_template
44
#' @export
5-
add_additional <- function(
6-
pkg = ".",
7-
dev_dir = "dev",
8-
flat_name = "additional",
9-
overwrite = FALSE,
10-
open = TRUE) {
5+
add_additional <- function(pkg = ".",
6+
dev_dir = "dev",
7+
flat_name = "additional",
8+
overwrite = FALSE,
9+
open = TRUE) {
1110
add_flat_template(
1211
template = "additional",
13-
pkg = pkg,
12+
pkg = pkg,
1413
dev_dir = dev_dir,
1514
flat_name = flat_name,
1615
overwrite = overwrite,
17-
open = open)
16+
open = open
17+
)
1818
}
1919

2020
#' @rdname add_flat_template
2121
#' @export
22-
add_minimal <- function(
23-
pkg = ".",
24-
dev_dir = "dev",
25-
flat_name = "minimal",
26-
overwrite = FALSE,
27-
open = TRUE) {
22+
add_minimal <- function(pkg = ".",
23+
dev_dir = "dev",
24+
flat_name = "minimal",
25+
overwrite = FALSE,
26+
open = TRUE) {
2827
add_flat_template(
2928
template = "minimal",
30-
pkg = pkg,
29+
pkg = pkg,
3130
dev_dir = dev_dir,
3231
flat_name = flat_name,
3332
overwrite = overwrite,
34-
open = open)
33+
open = open
34+
)
3535
}
3636

3737
#' @rdname add_flat_template
3838
#' @export
39-
add_full <- function(
40-
pkg = ".",
41-
dev_dir = "dev",
42-
flat_name = "full",
43-
overwrite = FALSE,
44-
open = TRUE) {
39+
add_full <- function(pkg = ".",
40+
dev_dir = "dev",
41+
flat_name = "full",
42+
overwrite = FALSE,
43+
open = TRUE) {
4544
add_flat_template(
4645
template = "full",
47-
pkg = pkg,
46+
pkg = pkg,
4847
dev_dir = dev_dir,
4948
flat_name = flat_name,
5049
overwrite = overwrite,
51-
open = open)
50+
open = open
51+
)
5252
}
5353

5454
#' Add flat Rmd file that drives package development
@@ -59,7 +59,7 @@ add_full <- function(
5959
#' @param open Logical. Whether to open file after creation
6060
#' @param dev_dir Name of directory for development Rmarkdown files. Default to "dev".
6161
#' @param flat_name Name of the file to write in dev.
62-
#' Use the name of the main function of your template to get chunks pre-filled with this function name.
62+
#' Use the name of the main function of your template to get chunks pre-filled with this function name.
6363
#'
6464
#' @importFrom tools file_path_sans_ext
6565
#' @details
@@ -76,7 +76,7 @@ add_full <- function(
7676
#'
7777
#' Abbreviated names can also be used for the different templates:
7878
#' "add" for additional, "min" for minimal, "teach" for teaching, "dev" for "dev_history".
79-
#'
79+
#'
8080
#' `add_additional()`, `add_minimal()`, `add_full()` are wrapper around `add_flat_template("additional")`, ...
8181
#' However, `add_dev_history()` is a deprecated function from a previous version.
8282
#'
@@ -89,62 +89,67 @@ add_full <- function(
8989
#' # Create a new project
9090
#' dummypackage <- tempfile("dummypackage")
9191
#' dir.create(dummypackage)
92-
#'
92+
#'
9393
#' # Add
9494
#' add_flat_template(template = "teaching", pkg = dummypackage)
9595
#' # Delete dummy package
9696
#' unlink(dummypackage, recursive = TRUE)
97-
#'
97+
#'
9898
#' # For classical use in your package
9999
#' \dontrun{
100100
#' # first time ever using 'fusen'
101-
#' add_flat_template("full")
102-
#'
101+
#' add_flat_template("full")
102+
#'
103103
#' # first time in your new package
104104
#' add_flat_template("minimal")
105-
#'
105+
#'
106106
#' # add new flat file for new functions
107107
#' add_flat_template("add")
108-
#'
108+
#'
109109
#' # add new flat template for teaching (a reduced full template)
110110
#' add_flat_template("teaching")
111-
#' #'}
112-
add_flat_template <- function(
113-
template = c("full", "minimal", "additional", "teaching", "dev_history"),
114-
pkg = ".",
115-
dev_dir = "dev",
116-
flat_name = template,
117-
overwrite = FALSE,
118-
open = TRUE) {
119-
111+
#' #'
112+
#' }
113+
add_flat_template <- function(template = c("full", "minimal", "additional", "teaching", "dev_history"),
114+
pkg = ".",
115+
dev_dir = "dev",
116+
flat_name = template,
117+
overwrite = FALSE,
118+
open = TRUE) {
120119
project_name <- get_pkg_name(pkg = pkg)
121-
120+
122121
if (project_name != asciify_name(project_name, to_pkg = TRUE)) {
123-
stop("Please rename your project/directory with: `", asciify_name(project_name, to_pkg = TRUE),
124-
"` as a package name should only contain letters, numbers and dots.")
122+
stop(
123+
"Please rename your project/directory with: `", asciify_name(project_name, to_pkg = TRUE),
124+
"` as a package name should only contain letters, numbers and dots."
125+
)
125126
}
126-
127+
127128
template <- match.arg(template)
128-
if (!template %in% c("full", "teaching", "dev_history")
129-
& !flat_name %in% c("minimal", "additional")) {
129+
if (!template %in% c("full", "teaching", "dev_history") &
130+
!flat_name %in% c("minimal", "additional")) {
130131
fun_name <- gsub("-", "_", asciify_name(flat_name))
131132
} else {
132133
fun_name <- NA
133134
}
134-
flat_name <- paste0("flat_",
135-
asciify_name(gsub("[.]Rmd$", "", flat_name[1])), ".Rmd")
136-
135+
flat_name <- paste0(
136+
"flat_",
137+
asciify_name(gsub("[.]Rmd$", "", flat_name[1])), ".Rmd"
138+
)
139+
137140
pkg <- normalizePath(pkg)
138141
full_dev_dir <- file.path(pkg, dev_dir)
139-
if (!dir.exists(full_dev_dir)) {dir.create(full_dev_dir)}
140-
dev_file_path <- file.path(full_dev_dir, flat_name) #"dev_history.Rmd")
141-
142+
if (!dir.exists(full_dev_dir)) {
143+
dir.create(full_dev_dir)
144+
}
145+
dev_file_path <- file.path(full_dev_dir, flat_name) # "dev_history.Rmd")
146+
142147
# Which template ----
143148
if (template == "dev_history") {
144149
dev_file_path <- character(0)
145150
} else {
146151
template_file <- system.file(paste0("flat-template-", template, ".Rmd"), package = "fusen")
147-
152+
148153
if (file.exists(dev_file_path) & overwrite == FALSE) {
149154
n <- length(list.files(full_dev_dir, pattern = "^flat_.*[.]Rmd"))
150155
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(
155160
)
156161
}
157162
dev_name <- basename(dev_file_path)
158-
163+
159164
# Change lines asking for pkg name
160165
lines_template <- readLines(template_file)
161-
166+
162167
lines_template[grepl("<my_package_name>", lines_template)] <-
163-
gsub("<my_package_name>", project_name,
164-
lines_template[grepl("<my_package_name>", lines_template)])
165-
168+
gsub(
169+
"<my_package_name>", project_name,
170+
lines_template[grepl("<my_package_name>", lines_template)]
171+
)
172+
166173
# Change flat_template file name
167174
# _inflate
168175
lines_template[grepl("dev/flat_template.Rmd", lines_template)] <-
169-
gsub("dev/flat_template.Rmd", file.path(dev_dir, dev_name),
170-
lines_template[grepl("dev/flat_template.Rmd", lines_template)])
176+
gsub(
177+
"dev/flat_template.Rmd", file.path(dev_dir, dev_name),
178+
lines_template[grepl("dev/flat_template.Rmd", lines_template)]
179+
)
171180
# _title
172181
lines_template[grepl("flat_template.Rmd", lines_template)] <-
173-
gsub("flat_template.Rmd", dev_name,
174-
lines_template[grepl("flat_template.Rmd", lines_template)])
175-
182+
gsub(
183+
"flat_template.Rmd", dev_name,
184+
lines_template[grepl("flat_template.Rmd", lines_template)]
185+
)
186+
176187
# Change my_fun to fun_name
177188
if (!is.na(fun_name)) {
178189
lines_template[grepl("my_fun", lines_template)] <-
179-
gsub("my_fun", fun_name,
180-
lines_template[grepl("my_fun", lines_template)])
190+
gsub(
191+
"my_fun", fun_name,
192+
lines_template[grepl("my_fun", lines_template)]
193+
)
181194
}
182-
195+
183196
cat(enc2utf8(lines_template), file = dev_file_path, sep = "\n")
184197
}
185-
198+
186199
# Add the-dev-history when needed ----
187200
if (template %in% c("full", "minimal", "dev_history")) {
188201
dev_file <- file.path(full_dev_dir, "0-dev_history.Rmd")
189202
if (file.exists(dev_file) & !isTRUE(overwrite)) {
190-
message("'0-dev_history.Rmd' already exists. It was not overwritten. ",
191-
"Set `add_flat_template(overwrite = TRUE)` if you want to do so.")
203+
message(
204+
"'0-dev_history.Rmd' already exists. It was not overwritten. ",
205+
"Set `add_flat_template(overwrite = TRUE)` if you want to do so."
206+
)
192207
} else {
193208
copy <- file.copy(
194209
system.file("the-dev-history.Rmd", package = "fusen"),
@@ -200,9 +215,8 @@ add_flat_template <- function(
200215
}
201216
dev_file_path <- c(dev_file_path, dev_file)
202217
}
203-
204218
}
205-
219+
206220
# Add data for the full template exemple
207221
if (template %in% c("full")) {
208222
inst_dir <- file.path(pkg, "inst")
@@ -213,15 +227,15 @@ add_flat_template <- function(
213227
# Example dataset
214228
file.copy(system.file("nyc_squirrels_sample.csv", package = "fusen"), inst_dir)
215229
}
216-
230+
217231
# .Rbuildignore ----
218232
# usethis::use_build_ignore(dev_dir) # Cannot be used outside project
219233
if (length(list.files(pkg, pattern = "[.]Rproj")) == 0) {
220234
lines <- c(paste0("^", dev_dir, "$"), "^\\.here$")
221235
} else {
222236
lines <- c(paste0("^", dev_dir, "$"))
223237
}
224-
238+
225239
buildfile <- normalizePath(file.path(pkg, ".Rbuildignore"), mustWork = FALSE)
226240
if (!file.exists(buildfile)) {
227241
existing_lines <- ""
@@ -233,11 +247,11 @@ add_flat_template <- function(
233247
all <- c(existing_lines, new)
234248
cat(enc2utf8(all), file = buildfile, sep = "\n")
235249
}
236-
250+
237251
# Add a gitignore file in dev_dir ----
238252
# Files to ignore
239253
lines <- c("*.html", "*.R")
240-
254+
241255
gitfile <- normalizePath(file.path(full_dev_dir, ".gitignore"), mustWork = FALSE)
242256
if (!file.exists(gitfile)) {
243257
existing_lines <- ""
@@ -249,11 +263,13 @@ add_flat_template <- function(
249263
all <- c(existing_lines, new)
250264
cat(enc2utf8(all), file = gitfile, sep = "\n")
251265
}
252-
266+
253267
if (length(list.files(pkg, pattern = "[.]Rproj")) == 0) {
254268
here::set_here(pkg)
255269
}
256-
if (isTRUE(open) & interactive()) {usethis::edit_file(dev_file_path)}
257-
270+
if (isTRUE(open) & interactive()) {
271+
usethis::edit_file(dev_file_path)
272+
}
273+
258274
dev_file_path
259275
}

0 commit comments

Comments
 (0)