Movatterモバイル変換


[0]ホーム

URL:


Title:Dynamically Generate Quarto Syntax
Version:0.1.0
Description: Provides helper functions to work programmatically within a quarto document. It allows the user to create section headers, tabsets, divs, and spans, and formats these objects into quarto syntax when printed into a document.
License:MIT + file LICENSE
Encoding:UTF-8
RoxygenNote:7.3.2
URL:https://github.com/djnavarro/quartose,https://quartose.djnavarro.net/
BugReports:https://github.com/djnavarro/quartose/issues
Imports:cli, knitr, purrr, rlang, utils
Suggests:ggplot2, quarto, rmarkdown, spelling, testthat (≥ 3.0.0)
Config/testthat/edition:3
Config/Needs/website:rmarkdown
Language:en-US
NeedsCompilation:no
Packaged:2025-07-06 11:28:44 UTC; danielle
Author:Danielle Navarro [aut, cre, cph]
Maintainer:Danielle Navarro <djnavarro@protonmail.com>
Repository:CRAN
Date/Publication:2025-07-09 12:50:02 UTC

Format a quarto object

Description

Creates a formatted representation of a quarto object in aform suitable for printing. When callingknitr::knit_print()on a quarto object, the relevantformat() method is calledfirst, and the formatted version is printed to the document.Note that the baseprint() method for quarto objects doesnot callformat().

Usage

## S3 method for class 'quarto_section'format(x, ...)## S3 method for class 'quarto_tabset'format(x, ...)## S3 method for class 'quarto_div'format(x, ...)## S3 method for class 'quarto_span'format(x, ...)## S3 method for class 'quarto_markdown'format(x, ...)## S3 method for class 'quarto_group'format(x, ...)

Arguments

x

A quarto object.

...

Other arguments (ignored).

Details

The intent behind theformat() methods for quarto objectsis to create a ready-to-print representation of that is almostidentical to what will be printed into the quarto documentwhenknitr::knit_print() is called. Because of this, theformatted version of a quarto object is a string or a list ofstrings, but it may also include plot objects that have notyet been rendered. The resulting representation isn't alwaysvery pretty, though it is generally fairly readable.

Value

A formatted quarto object. Forquarto_section,quarto_span,andquarto_markdown objects, the formatted output is always astring (character vector of length 1). Forquarto_tabset andquarto_group objects, the output is always a list whose elementsare either strings or plot objects. Forquarto_div objects,the output is currently a string, but this may change to listoutput in future if divs are permitted to contain plots.

Examples

# formatted sections, spans and divs ----------------------------------sec <- quarto_section("Header", level = 2L)spn <- quarto_span("Content", class = "underline")div <- quarto_div("Content", class = "content-margin")format(sec)format(spn)format(div)# formatted tabsets ---------------------------------------------------tbs <- quarto_tabset(  content = list(tab1 = 1:10, tab2 = "hello"),  title = "Header",  level = 2L)format(tbs)# formatted groups and markdown ---------------------------------------mkd <- quarto_markdown(list("- this is a", "- markdown list"), sep = "\n")gps <- quarto_group(list(div, mkd))format(mkd)format(gps)

Dynamically generate quarto syntax

Description

Define quarto objects for insertion into a document.Intended to be used inside a quarto document, within a knitrcode chunk with theresults: asis option set.

Usage

quarto_section(title, level)quarto_tabset(content, level, title = NULL, names = NULL)quarto_div(content, class = NULL, sep = "")quarto_span(content, class = NULL, sep = "")quarto_group(content, sep = "")quarto_markdown(content, sep = "")

Arguments

title

Character string specifying the text to use as a section title.Forquarto_section() this is a required argument. Forquarto_tabset()it is permitted to usetitle = NULL, in which case the tabset willbe printed without a section header above it. This is the defaultbehavior for tabsets.

level

Numeric header level applied to section title or tabset names.Thelevel argument must be a whole number between 1 and 6. Onlyrelevant to quarto objects that produce section headings, specificallyquarto_section() andquarto_tabset().

content

List or character vector containing content to be included withinthe quarto object. The expected format of thecontent argumentdiffers slightly depending on which function is used. See the"details" section for more information.

names

Character vector of names to be applied to the tabs in a tabset. Onlyrelevant toquarto_tabset(). Ifnames = NULL, the names will betaken from the names of thecontent argument.

class

Character vector specifying CSS classes to be applied to the content.Only relevant toquarto_div() andquarto_span(). Defaults toclass = NULL, in which case the formatted text written to the documentwill have a dummy CSS class "quartose-null" applied.

sep

Character string specifying the separator to be used when merging contentfor printing to the document. Defaults tosep = "" for all functions.

Details

The purpose of these functions is to allow the user to dynamicallygenerate quarto syntax from R. When used within a quarto document theyallow the user to generate callouts, margin text, tabsets, sectionheaders, and other kinds of quarto output. At the current state ofdevelopment the functionality is somewhat limited, discussed below.

The⁠quarto_*()⁠ functions supplied by the quartose package have a commondesign: argument values supplied by the user are stored internally as alist, with only a minimum of processing done at the time that the functionis called. The object is assigned to two S3 classes, the "quarto_object"shared by all objects, and a specific class associated with the callingfunction. These objects can be inspected and manipulated programmaticallylike any other R objects prior to printing.

When creating a quarto object, note that most⁠quarto_*()⁠ functions takeacontent argument, which differs slightly depending on the context:

Creating a quarto object only defines the data structure, it does notperform any formatting. Similarly, if the object is printed usingprint(), no formatting will be applied. A brief summary of thedata structure will be printed to the console, no more. However, whenknitr::knit_print() is called, the quarto object is first passed tothe relevantformat() method, which is responsible for constructingthe appropriate quarto syntax. Callingformat() will return acharacter vector or a list. If it returns a list all elements willeither be character strings with the appropriate quarto syntax, or aplot object that has not yet been rendered. After formatting is appliedtheknitr::knit_print() method will pass the strings (or plots) tothe document. For more detail on the formatting and printing methodsseeknit_print.quarto_object() andformat.quarto_object().

Value

These functions always return an object with parent S3 class"quarto_object", in addition to a specific S3 class correspondingto the function. For example,quarto_section() objects also possess the"quarto_section" class.

Examples

# quarto_section ------------------------------------------------------sec <- quarto_section("A level-two header", level = 2L)# quarto objects have two classes, a general purpose class shared by # all quarto objects, and a class specific to the functionclass(sec)  # base::print() displays an abstract summary of the object print(sec)# knitr::knit_print() produces the rendered quarto syntaxknitr::knit_print(sec)# quarto_span ---------------------------------------------------------spn1 <- quarto_span("This is plain text")spn2 <- quarto_span("This is underlined text", class = "underline")print(spn1)print(spn2)knitr::knit_print(spn1)knitr::knit_print(spn2)# quarto_div ----------------------------------------------------------# quarto_div objects are flexible: they can take a character vector as# the content argument, but can also take lists of other objects; note# that internally the content is always represented as a listdiv1 <- quarto_div("This is a callout note", class = "callout-note")div2 <- quarto_div(  content = list(    quarto_span(content = "You can wrap multiple spans in a div so that"),    quarto_span(content = "some text is highlighted", class = "mark"),    quarto_span(content = "and some is underlined", class = "underline")  ),  class = c("column-margin", "callout-tip"),  sep = " ")print(div1)print(div2)knitr::knit_print(div1)knitr::knit_print(div2)# quarto_tabset -------------------------------------------------------tbs <- quarto_tabset(list(tab1 = 1:10, tab2 = "hello"), level = 3L)print(tbs)knitr::knit_print(tbs)# quarto_markdown -----------------------------------------------------mkd <- quarto_markdown(list("- a markdown", "- list"), sep = "\n")print(mkd)knitr::knit_print(mkd)# quarto_group --------------------------------------------------------grp <- quarto_group(list(  quarto_div("This is a callout note", class = "callout-note"),  quarto_div("This is a callout tip", class = "callout-tip")))print(grp)knitr::knit_print(grp)

Print a quarto object

Description

Prints a quarto object. When callingknitr::knit_print()on a quarto object, the relevantformat() method is calledfirst, and the formatted version is printed to the document.When callingprint(), a summary of the object structure isprinted.

Usage

## S3 method for class 'quarto_object'knit_print(x, ...)## S3 method for class 'quarto_object'print(x, ...)

Arguments

x

A quarto object.

...

Other arguments (ignored).

Details

There are two print methods supplied for quarto objects, one forbase::print() and another forknitr::knit_print(). The regularprint method behaves similarly to any other print method: it printsa summary of the object to the R console, and invisibly returns theobject itself.

Whenknitr::knit_print() is called on a quarto object, the behavioris quite different. The object is first passed toformat(), whichconstructs the required quarto syntax, then the object is printedto the document (or console, if called interactively) using theappropriate syntax. In this case, the function invisibly returnsNULL.

Value

knitr::knit_print() invisibly returnsNULL;print()invisibly returns the quarto object itself.

Examples

# a quarto_section objectsec <- quarto_section("A level-two header", level = 2L) # base::print() displays a summary of the object print(sec)# knitr::knit_print() displays the rendered quarto syntaxknitr::knit_print(sec) # a quarto_span objectspn <- quarto_span("This is underlined", class = "underline")print(spn)knitr::knit_print(spn)

[8]ページ先頭

©2009-2025 Movatter.jp