Skip to content

Commit

Permalink
Merge pull request #166 from ThinkR-open/v0.4.1
Browse files Browse the repository at this point in the history
chore: upgrade to 0.4.1
  • Loading branch information
statnmap authored Oct 3, 2022
2 parents bfd6867 + 2237c0a commit 40cd8df
Show file tree
Hide file tree
Showing 14 changed files with 240 additions and 219 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/R-CMD-check-HTML5.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: R-CMD-check-HTML5

jobs:
R-CMD-check:
runs-on: ubuntu-latest
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes
_R_CHECK_RD_VALIDATE_RD2HTML_: TRUE
steps:
- uses: actions/checkout@v2

- name: Install pdflatex
run: sudo apt-get install texlive-latex-base texlive-fonts-recommended texlive-fonts-extra texlive-latex-extra

- name: Install tidy
run: sudo apt install tidy

- uses: r-lib/actions/setup-r@v2
with:
r-version: 'devel'
http-user-agent: 'release'
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck
needs: check

- uses: r-lib/actions/check-r-package@v2
with:
args: '"--as-cran"'
build_args: 'character()'
error-on: '"note"'
8 changes: 4 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Package: fusen
Title: Build a Package from Rmarkdown File
Version: 0.4.0.9000
Title: Build a Package from Rmarkdown Files
Version: 0.4.1
Authors@R: c(
person("Sebastien", "Rochette", , "sebastien@thinkr.fr", role = c("aut", "cre"),
comment = c(ORCID = "0000-0002-1565-9313")),
person("ThinkR", role = "cph")
)
Description: Use Rmarkdown First method to build your package. Start your
Description: Use Rmarkdown First method to build your package. Start your
package with documentation, functions, examples and tests in the same
unique file. Everything can be set from the Rmarkdown template file
provided in your project, then inflated as a package. Inflating the
provided in your project, then inflated as a package. Inflating the
template copies the relevant chunks and sections in the appropriate
files required for package development.
License: MIT + file LICENSE
Expand Down
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# fusen (development version)
# fusen 0.4.1

## New features

- Load all `function` chunks of the flat file currently opened with `load_flat_functions()` (Like a `load_all()` for a flat file)
- Allow to `inflate()` a Quarto ".qmd" flat file (#160)

## Minor

- Fix HTML5 doc

# fusen 0.4.0

## New features
Expand Down
176 changes: 80 additions & 96 deletions R/add_flat_template.R
Original file line number Diff line number Diff line change
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(pkg = ".",
#' @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(pkg = ".",
#'
#' 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,67 +89,62 @@ add_full <- function(pkg = ".",
#' # 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 @@ -160,50 +155,40 @@ add_flat_template <- function(template = c("full", "minimal", "additional", "tea
)
}
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 @@ -215,8 +200,9 @@ add_flat_template <- function(template = c("full", "minimal", "additional", "tea
}
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 @@ -227,15 +213,15 @@ add_flat_template <- function(template = c("full", "minimal", "additional", "tea
# 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 @@ -247,11 +233,11 @@ add_flat_template <- function(template = c("full", "minimal", "additional", "tea
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")

lines <- c("*.html", "*.R")
gitfile <- normalizePath(file.path(full_dev_dir, ".gitignore"), mustWork = FALSE)
if (!file.exists(gitfile)) {
existing_lines <- ""
Expand All @@ -263,13 +249,11 @@ add_flat_template <- function(template = c("full", "minimal", "additional", "tea
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
}
Loading

0 comments on commit 40cd8df

Please sign in to comment.