Creates a new Quarto document in the specified directory. Optionally copies a sample dataset and a worked analysis example, wires up custom branding assets from a directory of standardized files, and scaffolds a post-render purl hook for extracting R code.
Usage
create_qmd(
filename = NULL,
path = ".",
header_defaults = NULL,
overwrite = FALSE,
use_purl = FALSE,
include_examples = TRUE,
use_style = FALSE,
yaml_data = lifecycle::deprecated()
)Arguments
- filename
A string or
NULL. Name of the generated.qmdfile, relative topath. Must be supplied explicitly, e.g."analysis.qmd"or"reports/analysis.qmd". A folder named in it is created if it does not exist. See the section on documents in subfolders below.- path
A string. Path to the directory where the document will be created. Defaults to
"."(the current working directory).- header_defaults
A string or
NULL. Path to a YAML file supplying values to pre-populate the document header – typically a profile written bygenerate_profile(), but any YAML file following the same shape works. IfNULL(the default), the template is copied as-is with placeholder prompts intact. Every key in the file, at any depth, replaces the template's key of the same name; keys the file does not mention are left exactly as the template wrote them. A key whose value is itself a mapping (format: html: ...) is descended into and merged key by key, so a sibling the file doesn't mention –css:fromuse_style, say – survives; a key whose value is a sequence (author:,categories:) is replaced as a whole, not merged element by element. Namedyaml_databefore v0.5.1.9000; that name still works but is deprecated (see below).- overwrite
A logical. Whether to overwrite existing files. Defaults to
FALSE. Note two exceptions:assets/logo.pngis never overwritten, since an existing logo is assumed to be deliberate branding rather than a stale copy of the placeholder; and_quarto.ymlis never governed byoverwriteat all – when it is touched, it is merged rather than replaced, and in some cases (seeuse_purlbelow) it is left untouched entirely regardless ofoverwrite, on purpose.- use_purl
Logical. Defaults to
FALSE. WhenTRUE:Stamps the document's own YAML header with
purl: true.Ensures
R/purl.Rexists inpath(subject tooverwrite, like any other scaffolded file – an existingR/purl.Ris left in place unlessoverwrite = TRUE).Ensures
path/_quarto.ymlhas aproject: post-render:entry pointing atR/purl.R– unless_quarto.ymlalready exists and declaresproject: type:aswebsite,book, ormanuscript, in which case the hook is deliberately not wired automatically. Acli_warn()explains why, reports whetherR/purl.Rwas created or was already present, and names thepost-render:entry to add by hand. This guard exists becauseR/purl.Rpurls each document to a path mirroring its source location underR/– safe within a single project, but the interesting failure mode it's protecting against is deciding whether to opt a multi-document project in at all, since a website or book renders many documents on every full build and the person scaffolding one.qmdmay not be thinking about the other twenty. If_quarto.ymldoes not yet exist at all, the package template is copied in as usual (nothing to guard against yet – a fresh_quarto.ymlwith notype:is not a multi- document project). Outside the guarded types, an existing_quarto.ymlgets the hook merged into its existingproject:block rather than overwritten, sotype,website, and any other project options are left untouched. This merge (when it happens) is unaffected byoverwrite, since appending one line topost-renderis non-destructive.
When
use_purl = FALSE, the document's header is still stamped, withpurl: false, soR/purl.R(in a project where some other document hasuse_purl = TRUE) can positively confirm this document should be skipped rather than merely lacking an opinion.R/purl.Ritself only purls documents whose own header carriespurl: true, so turning this on for one document inside a larger project – a Quarto website, a book – does not cause every other.qmdin that project to be purled whenever the project renders in full. Output paths underR/mirror each source document's path relative to the project root, so two documents that happen to share a filename in different directories (e.g. a directory-per-post convention usingindex.qmd) do not overwrite each other's output.- include_examples
Logical. If
TRUE(the default), copies a sample dataset (sample.csv) intodata-raw/and uses a template.qmdpre-populated with a worked analysis example. The YAML header includes aparamsblock referencing the sample data. IfFALSE, creates a blank.qmdwith only the YAML header and no example content, and skips copying the sample dataset.A placeholder logo (
generic-logo.png, copied aslogo.png) is also copied intoassets/, but only when branding is actually part of the project: ifpathcarries a_toolero.yml(as written byinit_project()) whosefolders:list does not includeassets(i.e. the project was scaffolded withbranding = "none"), the logo is skipped along with it, so a project that declared no branding does not end up with an undeclaredassets/logo.pnganyway. A.qmdcreated outside any toolero-scaffolded project (no_toolero.ymlatpath) always gets the logo, since there is no project-level branding decision to defer to. Ifassets/logo.pngalready exists (e.g. from a priorinit_project()call withbrandingset), it is always left untouched – an existing logo takes precedence over the generic placeholder even whenoverwrite = TRUE.- use_style
Logical or character. Controls whether custom branding assets are wired into the YAML.
FALSE(the default): no custom styling. The YAMLformat: html:block contains only standard Quarto options.TRUE: shorthand for"assets/". Looks inpath/assets/forstyles.css,header.html, andfooter.htmlby name, and wires up whichever of these are present.A directory path (e.g.
"my-branding/"): looks in the given directory for the same three standardized filenames. The caller is responsible for ensuring the directory contains the files it needs under these exact names;create_qmd()does not rename or infer from other file names.
styles.cssis added ascss:,header.htmlasinclude-before-body:, andfooter.htmlasinclude-after-body:. Any subset may be present; only files that exist are wired into the YAML. If none of the three are found, a warning is issued and style injection is skipped. Note thatfavicon.png, though shipped with the branding asset set, is not wired into the document YAML – favicons are a Quarto website-project option rather than an HTML format option, so set it in_quarto.ymlif you need one.- yaml_data
A string or
NULL. Renamed toheader_defaultsin v0.5.1.9000 – the argument still works, and its value is used whenheader_defaultsis not also supplied, but new code should useheader_defaults.
Details
create_qmd() performs the following steps:
Validates that
filenameis supplied andpathexists.If
include_examples = TRUE: createsdata-raw/underpathand copiessample.csvthere. Createsassets/if needed and copies the generic placeholder logo aslogo.png, unless a logo already exists there. Uses the example template for the.qmd.If
include_examples = FALSE: uses the skeleton template for the.qmd. No sample data or logo is copied.If
use_styleisTRUEor a directory path: looks forstyles.css,header.html, andfooter.htmlby name and injects whichever are present into the YAML header.Stamps
purl: trueorpurl: falseinto the document's own YAML header, reflectinguse_purl.If
header_defaults(or the deprecatedyaml_data) is provided, reads the YAML file and substitutes its values into the document header, descending into nested mappings so a sibling key it doesn't mention survives. This runs after style injection and the purl stamp, soheader_defaultscan override any auto-generated YAML key, includingpurlitself.If
use_purl = TRUE, ensuresR/purl.Rexists. Then, unless_quarto.ymlalready exists and declaresproject: type:aswebsite,book, ormanuscript(in which case wiring is skipped with a warning explaining why), ensures_quarto.ymlhas the post-render hook – creating_quarto.ymlfrom the package template if absent, or merging the hook into the existing file'sproject:block if present.The sample dataset bundled with the template,
data-raw/sample.csv, is a subset of the Palmer Archipelago penguin data, taken from an earlier version of thepalmerpenguinspackage than the one on CRAN today. Treat it as teaching material rather than as a citable copy of the data.Since R 4.5.0 the same data ships with base R, so
?datasets::penguinsis the most convenient reference, withdatasets::penguins_rawcarrying the uncleaned form. One difference matters when reading the two side by side: base R shortened four of the column names, sobill_length_mm,bill_depth_mm,flipper_length_mmandbody_mass_ghere arebill_len,bill_dep,flipper_lenandbody_massthere.species,island,sexandyearare spelled the same in both. This template keeps the longer names, which carry their units.Original data: Gorman KB, Williams TD, Fraser WR (2014). Ecological sexual dimorphism and environmental variability within a community of Antarctic penguins (genus Pygoscelis). PLoS ONE 9(3): e90081. doi:10.1371/journal.pone.0090081 . R package: Horst AM, Hill AP, Gorman KB (2020). palmerpenguins: Palmer Archipelago (Antarctica) Penguin Data. doi:10.5281/zenodo.3960218 . Collected by Palmer Station Antarctica LTER, a member of the Long Term Ecological Research Network.
Documents in subfolders
path is the project root; filename may place the document below it,
as in create_qmd("reports/analysis.qmd"). The sample data, the
placeholder logo, R/purl.R, and _quarto.yml still go to the project
root. Paths in the document follow two rules:
Code paths start at the project root. The example template writes its results with
here::here("output", ...), and itsparams: input_file: "data-raw/sample.csv"is read from the root byresolve_input_path(), so the same code works at the root, inreports/, and on an HTCondor execute node.Header paths are relative to the document, because that is how Quarto resolves them. For a document in
reports/,use_stylewritescss: ../assets/styles.css, and likewise for the twoinclude-*entries. When a header include is wired in, the document'sresource-pathalso lists the project root, so the logoheader.htmlrefers to asassets/logo.pngis found.
Every edit to the document's YAML header is made line by line rather than by parsing the header and writing it back out. Keys the edit does not touch keep the template's own quoting, indentation, comments, and ordering, so the document a reader opens is the template we shipped plus the keys they asked for.
Note: filename has no default value and must always be supplied
explicitly. Use tempdir() for temporary output during testing or
exploration.
Examples
# \donttest{
# Minimal blank document -- no examples, no styling, no purl
create_qmd(path = tempdir(), filename = "analysis.qmd",
include_examples = FALSE)
#> ✔ Created /tmp/RtmpLziJOC/analysis.qmd
# Full worked example with sample data and placeholder logo
create_qmd(path = tempdir(), filename = "analysis.qmd",
overwrite = TRUE)
#> ✔ Created /tmp/RtmpLziJOC/data-raw/sample.csv
#> ✔ Created /tmp/RtmpLziJOC/assets/logo.png
#> ✔ Created /tmp/RtmpLziJOC/analysis.qmd
# Opt this document into purl: stamps purl: true and wires up
# R/purl.R + the _quarto.yml post-render hook (merged if the file
# already exists, e.g. inside a larger Quarto website project)
create_qmd(path = tempdir(), filename = "analysis.qmd",
overwrite = TRUE, use_purl = TRUE)
#> ✔ Created /tmp/RtmpLziJOC/data-raw/sample.csv
#> ℹ Skipping /tmp/RtmpLziJOC/assets/logo.png -- existing logo left in place.
#> ✔ Created /tmp/RtmpLziJOC/analysis.qmd
#> ✔ Created /tmp/RtmpLziJOC/R/purl.R
#> ✔ Created /tmp/RtmpLziJOC/_quarto.yml
# Blank document wired to branding assets (assumes assets/ exists,
# e.g. from init_project(branding = "uw-madison"))
create_qmd(path = tempdir(), filename = "report.qmd",
include_examples = FALSE, use_style = TRUE,
overwrite = TRUE)
#> Warning: No styles.css, header.html, or footer.html found in /tmp/RtmpLziJOC/assets.
#> Skipping style injection.
#> ✔ Created /tmp/RtmpLziJOC/report.qmd
# Blank document with custom branding from a different directory
create_qmd(path = tempdir(), filename = "report.qmd",
include_examples = FALSE, use_style = "my-branding/",
overwrite = TRUE)
#> Warning: Style directory /home/runner/work/toolero/toolero/docs/reference/my-branding
#> does not exist. Skipping style injection. Create the directory and add your
#> branding assets, or set `use_style = FALSE`.
#> ✔ Created /tmp/RtmpLziJOC/report.qmd
# Pre-populated YAML header, typically from generate_profile()
profile_file <- tempfile(fileext = ".yml")
writeLines("author:\n - name: 'Your Name'", profile_file)
create_qmd(path = tempdir(), filename = "analysis.qmd",
header_defaults = profile_file, overwrite = TRUE)
#> ✔ Created /tmp/RtmpLziJOC/data-raw/sample.csv
#> ℹ Skipping /tmp/RtmpLziJOC/assets/logo.png -- existing logo left in place.
#> ✔ Created /tmp/RtmpLziJOC/analysis.qmd
# }
