Package 'prettycode'

Title: 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.
Authors: Gábor Csárdi [aut, cre], Posit Software, PBC [cph, fnd]
Maintainer: Gábor Csárdi <[email protected]>
License: MIT + file LICENSE
Version: 1.1.0.9000
Built: 2024-07-01 05:00:53 UTC
Source: https://github.com/r-lib/prettycode

Help Index


Default prettycode syntax highlighting style

Description

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

  • reserved: reserved words

  • number: numeric literals

  • null: the NULL constant

  • operator: operators, including assignment

  • call: function calls

  • string: character literals

  • comment: comments

  • bracket: brackets: (){}[]

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 these type 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(warn_conflicts = TRUE)

Arguments

warn_conflicts

logical. If TRUE, warnings are printed about conflicts from replacing the standard print method.


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.