This builds on the reprex::reprex() function to take a chunk of code, run it and copy the formatted code and output to the clipboard. Note that this does NOT run in a clean session and is thus unlikely to create reproducible examples. Instead, it is intended to be used to quickly get a formatted bit of code and output that is not self-contained, e.g., for teaching materials.

run_and_format(code = NULL)

Arguments

code

The code to run - can be NULL, then the code is retrieved from the clipboard.

Examples

if (interactive()) {
  name <- "Lukas"
  run_and_format(paste("My name is", name))
}