Skip to content
Framework

Publishing

fw_snippet()

Run code in a script and publish it as a code snippet

Runs a braced block of code, prints its output as usual, and publishes the code and its printed text to Framework Cloud as a code snippet. Use it in R scripts. In Quarto and R Markdown notebooks, use publish_snippet().

Usage

fw_snippet(label, code, output = TRUE, folded = NULL, links = NULL, ...)

Arguments

Name Default Description
label

A name for the snippet, used as its label and default slug.

code

A braced block of code: { ... }.

output TRUE

FALSE publishes the code without its output. The output is still printed.

folded NULL

TRUE folds the output blocks and FALSE opens them. The default, NULL, lets Framework Cloud fold output longer than 12 lines.

links NULL

Links shown with the snippet. See publish_snippet().

...

Passed to publish_snippet()'s publishing arguments: slug, title, description, visibility, password, collection, share_unlisted, to, repository, and include_private.

Value

The artifact URL, invisibly.

Details

The block runs in the calling environment, so its assignments stay available afterward. It runs through evaluate::evaluate(), which splits it into expressions and keeps each one's output, messages, warnings, and errors in order. After the block finishes, its output is printed, its messages and warnings are signaled again, and its plots are drawn on the current device. Plots are left out of the snippet. When the block stops with an error, nothing is published and the error is signaled again.

The code text comes from the block's source references, so comments and layout are kept. Rscript keeps source references only with options(keep.source = TRUE) set before the code is parsed, as in Rscript -e 'options(keep.source = TRUE); source("script.R")'. Without them, the code is deparsed, which drops comments and reformats it, and the snippet's source records that. Source lines and the file come from the source references. The commit and repository follow publish_snippet().

Examples

Not run

if (FALSE) {
fw_snippet("fit-model", {
  fit <- lm(mpg ~ wt + hp, data = mtcars)
  summary(fit)
})
coef(fit)
}

Related reference

Search guides and reference for R, Python, Stata, the CLI, and Framework Cloud.