render_code: Generate a file with formatted code

View source: R/format_code.R

render_codeR Documentation

Generate a file with formatted code

Description

Format a piece of code to copy it into an email, a pdf, a document, etc.

Usage

render_code(
  output = "word",
  browser = getOption("browser"),
  font_size = 12,
  code = TRUE,
  ...
)

Arguments

output

a string. The output format ("pdf", "html" or "word" are accepted)

browser

a string. The path to the browser which will open the generated file format

font_size

a numeric. The font size in pdf format.

code

a boolean. Does the copied content is

...

other arguments passed to R chunk (for example eval = TRUE, echo = FALSE...)

Details

This function allows the user to generate formatted code (for email, document, copy, message, etc.) on the fly.

It accepts mainly word, pdf and html formats, but any format accepted by rmarkdown on the computer.

To use this function, simply copy a piece of code and run render_code() with the arguments that interest us. If you want content that is not R code, use the code argument to FALSE. In pdf format, you can change the font size using the font_size argument. Also, you can change the browser that opens the file by default with the browser argument. With the argument ..., you can specify knitr arguments to be included in the chunk. For example, you can add eval = TRUE (if you want the R code to be evaluated (and the result displayed)), echo = FALSE (if you don't want to display the code)... More information in the function opts_chunk or directly https://yihui.org/knitr/options/#chunk-options to see all available options and their descriptions.

Value

This function returns invisibly (with invisible()) NULL.

Examples

# Copy a snippet of code
if (clipr::clipr_available()) {
    clipr::write_clip("plot(AirPassengers)", allow_non_interactive = TRUE)
}

render_code(
    output = "word",
    echo = TRUE
)

render_code(
    output = "html",
    eval = FALSE
)


render_code(
    output = "pdf",
    eval = TRUE,
    font_size = 16
)


TBox documentation built on June 22, 2024, 9:51 a.m.