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

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.

Value

Single-element character vector with formatted code

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.

Examples


options(shiny.suppressMissingContextError = TRUE)

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

code <- expandChain(x())

deparseCode(code)
#> # Here's a comment
#> x <- sample(5) %>%
#>   sum()
#> x
formatCode(code)
#> # Here's a comment
#> x <- sample(5) %>%
#>   sum()
#> x
formatCode(code, formatter = styler::style_text)
#> # Here's a comment
#> x <- sample(5) %>% sum()
#> x