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 = ".",
yaml_data = NULL,
overwrite = FALSE,
use_purl = FALSE,
include_examples = TRUE,
use_style = FALSE
)Arguments
- filename
A string or
NULL. Name of the generated.qmdfile. Must be supplied explicitly, e.g."analysis.qmd".- path
A string. Path to the directory where the document will be created. Defaults to
"."(the current working directory).- yaml_data
A string or
NULL. Path to a YAML file containing metadata to pre-populate the document header. IfNULL(the default), the template is copied as-is with placeholder prompts intact. Each top-level key in the file replaces the template's key of the same name; keys the file does not mention are left exactly as the template wrote them.- 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 the header declares a
paramsblock with aninput_fileentry, addingparams: input_file: "data-raw/data.csv"as a placeholder when the template does not already declare one. See the note below on why this is tied touse_purl.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. Theparamsblock is not stamped in that case.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.On the
paramsstamp: the input-resolution pattern recommended throughout this family of packages – seedetect_execution_context()andsubmitr::htc_gen_submit()– readsparams$input_filein itsquartobranch, which requires the document to declareparams:. The example template does; the skeleton deliberately does not, sinceinclude_examples = FALSEasks for a bare document and padding it to serve a cluster workflow the user may never reach would be the wrong trade.use_purl = TRUEis the user saying this document is destined to become a script, and a script is exactly the artifact that runs underRscripton an execute node. So the skeleton stays bare for the local case and acquires what it needs at the moment it announces where it is going. An existinginput_fileis never overwritten.- include_examples
Logical. If
TRUE(the default), copies a sample dataset (sample.csv) intodata-raw/, a placeholder logo (generic-logo.png, copied aslogo.png) intoassets/, and uses a template.qmdpre-populated with a worked analysis example. 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. 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 and logo.- 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.
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, and whenuse_purl = TRUEalso ensures aparams: input_file:entry is present.If
yaml_datais provided, reads the YAML file and substitutes values into the document header. This runs after style injection and the purl stamp, soyaml_datacan override any auto-generated YAML key, includingpurlandparamsthemselves.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 is a subset of the Palmer Penguins dataset. Citation: Horst AM, Hill AP, Gorman KB (2020). palmerpenguins: Palmer Archipelago (Antarctica) Penguin Data. R package version 0.1.0. doi:10.5281/zenodo.3960218
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/RtmpAyNNfD/analysis.qmd
# Full worked example with sample data and placeholder logo
create_qmd(path = tempdir(), filename = "analysis.qmd",
overwrite = TRUE)
#> ✔ Created /tmp/RtmpAyNNfD/data-raw/sample.csv
#> ✔ Created /tmp/RtmpAyNNfD/assets/logo.png
#> ✔ Created /tmp/RtmpAyNNfD/analysis.qmd
# Opt this document into purl: stamps purl: true and a params block,
# 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/RtmpAyNNfD/data-raw/sample.csv
#> ℹ Skipping /tmp/RtmpAyNNfD/assets/logo.png -- existing logo left in place.
#> ✔ Created /tmp/RtmpAyNNfD/analysis.qmd
#> ✔ Created /tmp/RtmpAyNNfD/R/purl.R
#> ✔ Created /tmp/RtmpAyNNfD/_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/RtmpAyNNfD/assets.
#> Skipping style injection.
#> ✔ Created /tmp/RtmpAyNNfD/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/RtmpAyNNfD/report.qmd
# Pre-populated YAML overrides
yaml_file <- tempfile(fileext = ".yml")
writeLines("author:\n - name: 'Your Name'", yaml_file)
create_qmd(path = tempdir(), filename = "analysis.qmd",
yaml_data = yaml_file, overwrite = TRUE)
#> ✔ Created /tmp/RtmpAyNNfD/data-raw/sample.csv
#> ℹ Skipping /tmp/RtmpAyNNfD/assets/logo.png -- existing logo left in place.
#> ✔ Created /tmp/RtmpAyNNfD/analysis.qmd
# }
