formatCode: Deparse and format shinymeta expressions

View source: R/format.R

formatCodeR Documentation

Deparse and format shinymeta expressions

Description

Turn unevaluated shinymeta expressions into (formatted or styled) text.

Usage

formatCode(code, width = 500L, formatter = styleText, ...)

styleText(code, ...)

deparseCode(code, width = 500L)

Arguments

code

Either an unevaluated expression or a deparsed code string.

width

The width.cutoff to use when deparse()-ing the code expression.

formatter

a function that accepts deparsed code (a character string) as the first argument.

...

arguments passed along to the formatter function.

Details

Before any formatting takes place, the unevaluated expression is deparsed into a string via deparseCode(), which ensures that shinymeta comment strings (i.e., literal strings that appear on their own line, and begin with one or more ⁠#⁠ characters.) are turned into comments and superfluous ⁠\{⁠ are removed. After deparsing, the formatCode() function then calls the formatter function on the deparsed string to format (aka style) the code string. The default formatter, styleText(), uses styler::style_text() with a couple differences:

  • Pipe operators (⁠%>%⁠) are always followed by a line break.

  • If the token appearing after a line-break is a comma/operator, the line-break is removed.

Value

Single-element character vector with formatted code

Examples


options(shiny.suppressMissingContextError = TRUE)

x <- metaReactive({
  "# Here's a comment"
  sample(5) %>% sum()
})

code <- expandChain(x())

deparseCode(code)
formatCode(code)
formatCode(code, formatter = styler::style_text)

rstudio/shinymeta documentation built on Oct. 25, 2023, 7:12 p.m.