Title: Pretty Print R Code in the Terminal
Version: 1.1.0
Author: Gábor Csárdi
Maintainer: Gábor Csárdi <csardi.gabor@gmail.com>
Description: Replace the standard print method for functions with one that performs syntax highlighting, using ANSI colors, if the terminal supports them.
License: MIT + file LICENSE
LazyData: true
URL: https://github.com/r-lib/prettycode#readme
BugReports: https://github.com/r-lib/prettycode/issues
RoxygenNote: 6.1.0
Imports: crayon, utils
Suggests: covr, mockery, rstudioapi, testthat, withr
Encoding: UTF-8
NeedsCompilation: no
Packaged: 2019-12-16 13:01:04 UTC; gaborcsardi
Repository: CRAN
Date/Publication: 2019-12-16 14:00:06 UTC

Colored brackets

Description

Add color to brackets. Brackets will be coloured consecutively with the colors provided in color_seq by scope.

Usage

color_brackets(x, color_seq = list(yellow, blue, cyan))

Arguments

x

a character vector of brackets consisting of a valid sequence of any of the following: '[[', '[', ']', '(', ')', '{', '}'

color_seq

a list of functions that take and return a character scalar

Details

Meant for coloring brackets encountered within highlight. Note that occurrences of 'orphan' brackets are not taken into account mainly due to the fact that cases such as

foo <- function(x){ `[`(x, 1) }

will either be converted to

foo <- function(x){ x[1]}

before the brackets are coloured if passed in as highlight(deparse(foo)) or will be identified as a 'SYMBOL_FUNCTION_CALL' token instead of 'LBB' if passed in as

highlight("foo <- function(x){ `[`(x, 1) }")

Similarly, invalid code that would lead to orphaned brackets is not taken into account as this would be caught before hand in highlight.


Default prettycode syntax highlighting style

Description

A style function must return a named list of functions. Possible entries:

Usage

default_style()

Details

Each entry in a list must be a function that takes a character scalar, and returns a character scalar with the exception of bracket which should be a list of functions defining a color sequence. The default style adds ANSI formatting to the code.

Note that you can also change the code if you like, e.g. to include a unicode arrow character instead of the two-character assignment operator.

Examples

highlight(deparse(get), style = default_style())

Syntax highlight R code

Description

Syntax highlight R code

Usage

highlight(code, style = default_style())

Arguments

code

Character vector, each element is one line of code.

style

Style functions, see default_style().

Value

Character vector, the highlighted code.

Examples

highlight(deparse(ls))
cat(highlight(deparse(ls)), sep = "\n")

Pretty Print R Code in the Terminal

Description

Replace the standard print method for functions with one that performs syntax highlighting, using ANSI colors, if the terminal supports them.

Usage

prettycode()

Print a function with syntax highlighting

Description

To turn on pretty printing of functions, you need to call prettycode::prettycode(). It might be a good idea to call it from your .Rprofile.

Usage

## S3 method for class 'function'
print(x, useSource = TRUE, style = default_style(),
  ...)

Arguments

x

Function to print.

useSource

Whether to use the stored source code, if available.

style

The highlight style to use, see default_style().

...

Not used currently, for compatibility with the print generic.

Value

The function, invisibly.