Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Quarto articles #2015

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# usethis (development version)

* `use_vignette()` and `use_article()` gain `type` to allow creating Quarto vignettes and articles (@olivroy, #1997).

* `use_tidy_upkeep_issue()` now records the year it is being run in the
`Config/usethis/upkeep` field in DESCRIPTION. If this value exists it is
furthermore used to filter the checklist when making the issue.

## Bug fixes and minor improvements

* `use_package()` now decreases a package minimum version required when
`min_version` is lower than what is currently specified in the DESCRIPTION
file (@jplecavalier, #1957).
Expand Down
49 changes: 38 additions & 11 deletions R/vignette.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,59 @@
#' @param name Base for file name to use for new vignette. Should consist only
#' of numbers, letters, `_` and `-`. Lower case is recommended.
#' @param title The title of the vignette.
#' @param type One of `"quarto"` or `"rmarkdown"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#' @param type One of `"quarto"` or `"rmarkdown"`
#' @param builder One of `"quarto"` or `"rmarkdown"`

type is a little over-generic and could collide with another param. Let's use the more-specific builder (to match "VignetteBuilder"). If you go with this change, it needs to be changed throughout.

Copy link
Contributor Author

@olivroy olivroy Aug 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed that type may not be ideal. If we were to go with builder, the arguments should be updated to "knitr" instead of "rmarkdown"?

Maybe ext = c("qmd", "rmd") would work too.

Maybe there should be an option

options(usethis.vignette = "quarto") to avoid specifying it everytime?

I can imagine myself deciding to stick with a single type, and not have to specify it everytime.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like ext, and an option would be great!

#' @seealso The [vignettes chapter](https://r-pkgs.org/vignettes.html) of
#' [R Packages](https://r-pkgs.org).
#' [R Packages](https://r-pkgs.org) and the [Quarto vignettes](https://quarto-dev.github.io/quarto-r/articles/hello.html) reference.
#' @export
#' @examples
#' \dontrun{
#' use_vignette("how-to-do-stuff", "How to do stuff")
#' }
use_vignette <- function(name, title = name) {
use_vignette <- function(name, title = name, type = c("rmarkdown", "quarto")) {
olivroy marked this conversation as resolved.
Show resolved Hide resolved
check_is_package("use_vignette()")
check_required(name)
check_vignette_name(name)
type <- arg_match(type)

use_dependency("knitr", "Suggests")
use_dependency("rmarkdown", "Suggests")

proj_desc_field_update("VignetteBuilder", "knitr", overwrite = TRUE)
if (type == "rmarkdown") {
use_dependency("rmarkdown", "Suggests")
proj_desc_field_update("VignetteBuilder", "knitr", overwrite = FALSE)
use_vignette_template("vignette.Rmd", name, title)
} else if (type == "quarto") {
proj_desc_field_update("VignetteBuilder", "quarto", overwrite = FALSE)
use_vignette_template("vignette.qmd", name, title)
use_build_ignore("vignettes/*_files")
}
use_git_ignore("inst/doc")

use_vignette_template("vignette.Rmd", name, title)

invisible()
}

#' @export
#' @rdname use_vignette
use_article <- function(name, title = name) {
use_article <- function(name, title = name, type = c("rmarkdown", "quarto")) {
check_is_package("use_article()")

type <- arg_match(type)
deps <- proj_deps()
if (!"rmarkdown" %in% deps$package) {
proj_desc_field_update("Config/Needs/website", "rmarkdown", append = TRUE)

if (type == "rmarkdown") {
if (!"rmarkdown" %in% deps$package) {
proj_desc_field_update("Config/Needs/website", "rmarkdown", append = TRUE)
}

use_vignette_template("article.Rmd", name, title, subdir = "articles")
} else if (type == "quarto") {
if (!"quarto" %in% deps$package) {
proj_desc_field_update("Config/Needs/website", "quarto", append = TRUE)
}

use_vignette_template("article.qmd", name, title, subdir = "articles")
}

use_vignette_template("article.Rmd", name, title, subdir = "articles")
use_build_ignore("vignettes/articles")

invisible()
Expand All @@ -62,12 +81,20 @@
if (!is.null(subdir)) {
use_directory(path("vignettes", subdir))
}

use_git_ignore(c("*.html", "*.R"), directory = "vignettes")
# make sure nothing else is caught. (this should be assured as `.` are not allowed.)
vignette_ext <- path_ext(template)
arg_match0(vignette_ext, c("qmd", "Rmd"))
if (vignette_ext == "qmd" && uses_git()) {
# https://quarto-dev.github.io/quarto-r/articles/hello.html
use_git_ignore("*_files", directory = "vignettes")

Check warning on line 91 in R/vignette.R

View check run for this annotation

Codecov / codecov/patch

R/vignette.R#L91

Added line #L91 was not covered by tests
}

if (is.null(subdir)) {
path <- path("vignettes", asciify(name), ext = "Rmd")
path <- path("vignettes", asciify(name), ext = vignette_ext)
} else {
path <- path("vignettes", subdir, asciify(name), ext = "Rmd")
path <- path("vignettes", subdir, asciify(name), ext = vignette_ext)
}

data <- list(
Expand Down
12 changes: 12 additions & 0 deletions inst/templates/article.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: "{{{ vignette_title }}}"
format: html
olivroy marked this conversation as resolved.
Show resolved Hide resolved
knitr:
opts_chunk:
collapse: true
comment: "#>"
---

```{r setup}
library({{Package}})
```
18 changes: 18 additions & 0 deletions inst/templates/vignette.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
title: "{{{ vignette_title }}}"
format: html
knitr:
opts_chunk:
collapse: true
comment: "#>"
vignette: >
%\VignetteIndexEntry{{{ braced_vignette_title }}}
%\VignetteEngine{quarto::html}
%\VignetteEncoding{UTF-8}
---

```{r}
#| label: setup
library({{Package}})
```

8 changes: 5 additions & 3 deletions man/use_vignette.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 34 additions & 2 deletions tests/testthat/test-vignette.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,32 @@ test_that("use_vignette() does the promised setup", {
expect_true("inst/doc" %in% ignores)

deps <- proj_deps()
expect_true(
all(c("knitr", "rmarkdown") %in% deps$package[deps$type == "Suggests"])
expect_contains(
deps$package[deps$type == "Suggests"],
c("knitr", "rmarkdown")
)

expect_identical(proj_desc()$get_field("VignetteBuilder"), "knitr")
})

test_that("use_vignette() works with Quarto", {
create_local_package()

use_vignette("name", "title", type = "quarto")
expect_proj_file("vignettes/name.qmd")

ignores <- read_utf8(proj_path(".gitignore"))
expect_true("inst/doc" %in% ignores)

deps <- proj_deps()
expect_contains(
deps$package[deps$type == "Suggests"],
"knitr"
)

expect_identical(proj_desc()$get_field("VignetteBuilder"), "quarto")
})

# use_article -------------------------------------------------------------

test_that("use_article goes in article subdirectory", {
Expand All @@ -54,6 +73,19 @@ test_that("use_article() adds rmarkdown to Config/Needs/website", {
)
})

test_that("use_article() adds quarto to Config/Needs/website", {
create_local_package()
local_interactive(FALSE)

proj_desc_field_update("Config/Needs/website", "somepackage", append = TRUE)
use_article("name", "title", "quarto")

expect_setequal(
proj_desc()$get_list("Config/Needs/website"),
c("quarto", "somepackage")
)
})

# helpers -----------------------------------------------------------------

test_that("valid_vignette_name() works", {
Expand Down
Loading