This template demonstrates many of the bells and whistles of the reprex::reprex_document() output format. The YAML sets many options to non-default values, such as using #;-) as the comment in front of output.

Code style

Since style is TRUE, this difficult-to-read code (look at the .Rmd source file) will be restyled according to the Tidyverse style guide when it's rendered. Whitespace rationing is not in effect!

x=1;y=2;z=x+y;z

Quiet tidyverse

The tidyverse meta-package is quite chatty at startup, which can be very useful in exploratory, interactive work. It is often less useful in a reprex, so by default, we suppress this.

However, when tidyverse_quiet is FALSE, the rendered result will include a tidyverse startup message about package versions and function masking.

library(tidyverse)

Chunks in languages other than R

Remember: knitr supports many other languages than R, so you can reprex bits of code in Python, Ruby, Julia, C++, SQL, and more. Note that, in many cases, this still requires that you have the relevant external interpreter installed.

Let's try Python!

```{python, eval = Sys.which("python") != "", python.reticulate = requireNamespace("reticulate", quietly = TRUE)} x = 'hello, python world!' print(x.split(' '))

And bash!

```{bash, eval = Sys.which("bash") != ""}
echo "Hello Bash!";
pwd;
ls | head;

Write a function in C++, use Rcpp to wrap it and ...

```{Rcpp, eval = requireNamespace("Rcpp", quietly = TRUE)}

include

using namespace Rcpp;

// [[Rcpp::export]] NumericVector timesTwo(NumericVector x) { return x * 2; }

then immediately call your C++ function from R!

```r
timesTwo(1:4)

Standard output and error

Some output that you see in an interactive session is not actually captured by rmarkdown, when that same code is executed in the context of an .Rmd document. When std_out_err is TRUE, reprex::reprex_render() uses a feature of callr:r() to capture such output and then injects it into the rendered result.

Look for this output in a special section of the rendered document (and notice that it does not appear right here).

system2("echo", args = "Output that would normally be lost")

Session info

Because session_info is TRUE, the rendered result includes session info, even though no such code is included here in the source document.



Try the reprex package in your browser

Any scripts or data that you put into this service are public.

reprex documentation built on Aug. 17, 2022, 9:07 a.m.