Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

expr_print

Print an expression


Description

expr_print(), powered by expr_deparse(), is an alternative printer for R expressions with a few improvements over the base R printer.

  • It colourises quosures according to their environment. Quosures from the global environment are printed normally while quosures from local environments are printed in unique colour (or in italic when all colours are taken).

  • It wraps inlined objects in angular brackets. For instance, an integer vector unquoted in a function call (e.g. expr(foo(!!(1:3)))) is printed like this: foo(<int: 1L, 2L, 3L>) while by default R prints the code to create that vector: foo(1:3) which is ambiguous.

  • It respects the width boundary (from the global option width) in more cases.

Usage

expr_print(x, width = peek_option("width"))

expr_deparse(x, width = peek_option("width"))

Arguments

x

An object or expression to print.

width

The width of the deparsed or printed expression. Defaults to the global option width.

Examples

# It supports any object. Non-symbolic objects are always printed
# within angular brackets:
expr_print(1:3)
expr_print(function() NULL)

# Contrast this to how the code to create these objects is printed:
expr_print(quote(1:3))
expr_print(quote(function() NULL))

# The main cause of non-symbolic objects in expressions is
# quasiquotation:
expr_print(expr(foo(!!(1:3))))


# Quosures from the global environment are printed normally:
expr_print(quo(foo))
expr_print(quo(foo(!!quo(bar))))

# Quosures from local environments are colourised according to
# their environments (if you have crayon installed):
local_quo <- local(quo(foo))
expr_print(local_quo)

wrapper_quo <- local(quo(bar(!!local_quo, baz)))
expr_print(wrapper_quo)

rlang

Functions for Base Types and Core R and 'Tidyverse' Features

v0.4.11
MIT + file LICENSE
Authors
Lionel Henry [aut, cre], Hadley Wickham [aut], mikefc [cph] (Hash implementation based on Mike's xxhashlite), Yann Collet [cph] (Author of the embedded xxHash library), RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.