Creates a new Quarto document in the specified directory. Optionally copies a sample dataset and a worked analysis example, wires up custom CSS and header styling from a directory of assets, and scaffolds a post-render purl hook for extracting R code.
Usage
create_qmd(
filename = NULL,
path = ".",
yaml_data = NULL,
overwrite = FALSE,
use_purl = TRUE,
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.- overwrite
A logical. Whether to overwrite existing files. Defaults to
FALSE.- use_purl
Logical. If
TRUE(the default), creates a_quarto.ymlfile with a post-render hook and apurl.Rscript insideR/that extracts R code from the rendered document into a.Rfile. The target document is resolved dynamically by scanning the project root for.qmdfiles, so the samepurl.Rworks regardless of the document name.- include_examples
Logical. If
TRUE(the default), copies a sample dataset (sample.csv) intodata-raw/, a placeholder logo (logo.png) intoassets/, 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 and logo.- use_style
Logical or character. Controls whether custom CSS and header 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/". Scanspath/assets/for.cssand.htmlfiles and adds them to the YAML.A directory path (e.g.
"my-branding/"): scans the given directory for.cssand.htmlfiles and adds them to the YAML.
If the directory contains exactly one
.cssfile, it is added ascss:in the YAML. If exactly one.htmlfile is found, it is added asinclude-before-body:. If multiple.cssor.htmlfiles are found, the function errors and asks the user to specify which file to use viayaml_data. If neither is found, a warning is issued.
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 a placeholderlogo.png. 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: scans the directory for.cssand.htmlfiles and injects them into the YAML header.If
yaml_datais provided, reads the YAML file and substitutes values into the document header. This runs after style injection, soyaml_datacan override any auto-generated YAML keys.If
use_purl = TRUE, writes_quarto.ymlwith a post-render hook and copiespurl.Rintopath/R/.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
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
create_qmd(path = tempdir(), filename = "analysis.qmd",
include_examples = FALSE)
#> ✔ Created /tmp/RtmpCGYXJ9/analysis.qmd
#> ✔ Created /tmp/RtmpCGYXJ9/_quarto.yml
#> ✔ Created /tmp/RtmpCGYXJ9/R/purl.R
# Full worked example with sample data and placeholder logo
create_qmd(path = tempdir(), filename = "analysis.qmd",
overwrite = TRUE)
#> ✔ Created /tmp/RtmpCGYXJ9/data-raw/sample.csv
#> ✔ Created /tmp/RtmpCGYXJ9/assets/logo.png
#> ✔ Created /tmp/RtmpCGYXJ9/analysis.qmd
#> ✔ Created /tmp/RtmpCGYXJ9/_quarto.yml
#> ✔ Created /tmp/RtmpCGYXJ9/R/purl.R
# Blank document wired to UW branding assets (assumes assets/ exists)
create_qmd(path = tempdir(), filename = "report.qmd",
include_examples = FALSE, use_style = TRUE,
overwrite = TRUE)
#> Warning: No .css or .html files found in /tmp/RtmpCGYXJ9/assets. Skipping style
#> injection.
#> ✔ Created /tmp/RtmpCGYXJ9/report.qmd
#> ✔ Created /tmp/RtmpCGYXJ9/_quarto.yml
#> ✔ Created /tmp/RtmpCGYXJ9/R/purl.R
# 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, use_purl = FALSE)
#> Warning: Style directory my-branding/ does not exist. Skipping style injection. Create
#> the directory and add your .css and/or .html assets, or set `use_style =
#> FALSE`.
#> ✔ Created /tmp/RtmpCGYXJ9/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/RtmpCGYXJ9/data-raw/sample.csv
#> ✔ Created /tmp/RtmpCGYXJ9/assets/logo.png
#> ✔ Created /tmp/RtmpCGYXJ9/analysis.qmd
#> ✔ Created /tmp/RtmpCGYXJ9/_quarto.yml
#> ✔ Created /tmp/RtmpCGYXJ9/R/purl.R
# }
