Skip to content

Add create_quarto_project() #2045

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Suggests:
knitr,
magick,
pkgload (>= 1.3.2.1),
quarto,
rmarkdown,
roxygen2 (>= 7.1.2),
spelling (>= 1.2),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export(create_from_github)
export(create_github_token)
export(create_package)
export(create_project)
export(create_quarto_project)
export(create_tidy_package)
export(edit_file)
export(edit_git_config)
Expand Down
7 changes: 6 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@
is not specified (#2117, @salim-b).

* usethis's criteria for recognizing a project have expanded to include (#2133):
- a `.vscode/` directory, which Positron or VS Code might create
- a `.vscode/settings.json` file, which Positron or VS Code might create
- a `_quarto.yml` file, typical of a Quarto project
- an `renv.lock` file, which renv creates

* `create_quarto_project()` is a new experimental function that combines basic
usage of `quarto::quarto_create_project()` with some of the niceties of
Copy link
Member

Choose a reason for hiding this comment

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

I think putting quarto::quarto_create_project() inside the usethis::create_*() envelope is the main contribution here. E.g. challenging a project-within-a-project and opening / switching to the newly created Quarto project.

usethis's `create_*()` functions, such as opening the newly created project in
your IDE (#1891, @focardozom).

# usethis 3.1.0

* `use_vignette()` and `use_article()` support Quarto. The `name` of the new
Expand Down
66 changes: 58 additions & 8 deletions R/create.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
#'
#' @description
#' These functions create an R project:
#' * `create_package()` creates an R package
#' * `create_package()` creates an R package.
#' * `create_project()` creates a non-package project, i.e. a data analysis
#' project
#' project.
#' * `r lifecycle::badge("experimental")` `create_quarto_project()` creates a
Copy link
Member

@jennybc jennybc Jul 23, 2025

Choose a reason for hiding this comment

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

I'm marking this as experimental because, e.g., the behaviour when called inside an existing project is not so nice (mostly a limitation rooted in the quarto cli when called like this). I've also exposed the bare minimum of arguments. I want to make it clear that this might very well need some refinement.

#' Quarto project. It is a simplified convenience wrapper around
#' `quarto::quarto_create_project()`, which you should call directly for
Copy link
Member

Choose a reason for hiding this comment

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

I can't actually link to quarto::quarto_create_project() because quarto is only in Suggests.

#' more advanced usage.
#'
#' Both functions can be called on an existing project; you will be asked before
#' any existing files are changed.
#' These functions work best when creating a project *de novo*, but
#' `create_package()` and `create_project()` can be called in an existing
#' project; you will be asked before any existing files are changed.
#'
#' @inheritParams use_description
#' @param fields A named list of fields to add to `DESCRIPTION`, potentially
Expand All @@ -19,17 +24,22 @@
#' @param rstudio If `TRUE`, calls [use_rstudio()] to make the new package or
#' project into an [RStudio
#' Project](https://r-pkgs.org/workflow101.html#sec-workflow101-rstudio-projects).
#' If `FALSE` and a non-package project, a sentinel `.here` file is placed so
#' that the directory can be recognized as a project by the
#' [here](https://here.r-lib.org) or
#' [rprojroot](https://rprojroot.r-lib.org) packages.
#'
#' If `FALSE`, the goal is to ensure that the directory can be recognized as
#' a project by, for example, the [here](https://here.r-lib.org) package. If
#' the project is neither an R package nor a Quarto project, a sentinel
#' `.here` file is placed to mark the project root.
Comment on lines +28 to +31
Copy link
Member

Choose a reason for hiding this comment

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

This is mildly aspirational. I have only recently added rprojroot::is_quarto_project to usethis's project-finding criteria and I have a PR in flight to have the here package do the same: r-lib/here#130. But I think this will hopefully be true soon anough.

#' @param open If `TRUE`, [activates][proj_activate()] the new project:
#'
#' * If using RStudio desktop, the project is opened in a new session.
#' * If using Positron, the project is opened in a new window.
#' * If on RStudio server, the current RStudio project is activated.
#' * Otherwise, the working directory and active project is changed in the
#' current R session.
#' @param type The type of Quarto project to create. See
#' `?quarto::quarto_create_project` for the most up-to-date list, but
#' `"website"`, `"blog"`, `"book"`, and `"manuscript"` are common choices.
#'
#' @returns Path to the newly created project or package, invisibly.
#' @seealso [create_tidy_package()] is a convenience function that extends
#' `create_package()` by immediately applying as many of the tidyverse
Expand Down Expand Up @@ -114,6 +124,46 @@ create_project <- function(
invisible(proj_get())
}

#' @rdname create_package
#' @export
create_quarto_project <- function(
path,
type = "default",
rstudio = rstudioapi::isAvailable(),
open = rlang::is_interactive()
) {
check_installed("quarto")

path <- user_path_prep(path)
parent_dir <- path_dir(path)
check_path_is_directory(parent_dir)

name <- path_file(path_abs(path))
challenge_nested_project(parent_dir, name)
challenge_home_directory(path)

create_directory(path)
local_project(path, force = TRUE)

res <- quarto::quarto_create_project(
name = name,
dir = parent_dir,
type = type,
no_prompt = TRUE,
quiet = getOption("usethis.quiet", default = FALSE)
)

if (open) {
if (proj_activate(proj_get())) {
# working directory/active project already set; clear the scheduled
# restoration of the original project
withr::deferred_clear()
}
}

invisible(proj_get())
}

#' Create a project from a GitHub repo
#'
#' @description
Expand Down
34 changes: 26 additions & 8 deletions man/create_package.Rd

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

25 changes: 21 additions & 4 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,28 @@ create_local_package <- function(
}

create_local_project <- function(
# it is convenient if `dir` produces a project name that would be allowed for
# a CRAN package, even for a generic project test fixture
dir = file_temp(pattern = "testproj"),
env = parent.frame(),
rstudio = FALSE
) {
create_local_thing(dir, env, rstudio, "project")
}

create_local_quarto_project <- function(
dir = file_temp(pattern = "test-quarto-proj"),
env = parent.frame(),
rstudio = FALSE
) {
create_local_thing(dir, env, rstudio, "quarto_project")
}

create_local_thing <- function(
dir = file_temp(pattern = pattern),
env = parent.frame(),
rstudio = FALSE,
thing = c("package", "project")
thing = c("package", "project", "quarto_project")
) {
thing <- match.arg(thing)
if (fs::dir_exists(dir)) {
Expand All @@ -45,7 +55,7 @@ create_local_thing <- function(

withr::defer(
{
ui_bullets(c("Deleting temporary project: {.path {dir}}"))
ui_bullets(c("v" = "Deleting temporary project: {.path {dir}}"))
fs::dir_delete(dir)
},
envir = env
Expand All @@ -65,7 +75,12 @@ create_local_thing <- function(
open = FALSE,
check_name = FALSE
),
project = create_project(dir, rstudio = rstudio, open = FALSE)
project = create_project(dir, rstudio = rstudio, open = FALSE),
quarto_project = create_quarto_project(
dir,
rstudio = rstudio,
open = FALSE
)
)
)

Expand All @@ -74,7 +89,9 @@ create_local_thing <- function(

withr::defer(
{
ui_bullets(c("Restoring original working directory: {.path {old_wd}}"))
ui_bullets(c(
"v" = "Restoring original working directory: {.path {old_wd}}"
))
setwd(old_wd)
},
envir = env
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-create.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,11 @@ test_that("we discourage project creation in home directory", {
expect_usethis_error(create_project(path_home_r()), "create anyway")
}
})

test_that("create_quarto_project() works for basic usage", {
skip_if_not_installed("quarto")

dir <- create_local_quarto_project()
expect_proj_file("_quarto.yml")
expect_true(possibly_in_proj(dir))
})
Loading