Description Usage Arguments Default Output Behavior Pagers and Styles Pager Configuration Objects Custom Pager Configurations See Also Examples
Initializers for pager configuration objects that modify pager behavior.
These objects can be used as the pager
argument to the
diff*
methods, or as the pager
slot for
Style
objects. In this documentation we use the “pager”
term loosely and intend it to refer to any device other than the terminal
that can be used to render output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | Pager(
pager = function(x) writeLines(readLines(x)),
file.ext = "",
threshold = 0L,
ansi = FALSE,
file.path = NA_character_,
make.blocking = FALSE
)
PagerOff(...)
PagerSystem(pager = file.show, threshold = -1L, file.ext = "", ...)
PagerSystemLess(
pager = file.show,
threshold = -1L,
flags = "R",
file.ext = "",
ansi = TRUE,
...
)
PagerBrowser(
pager = view_or_browse,
threshold = 0L,
file.ext = "html",
make.blocking = NA,
...
)
|
pager |
a function that accepts at least one parameter and does not
require a parameter other than the first parameter. This function will be
called with a file path passed as the first argument. The referenced file
will contain the text of the diff. By default this is a temporary file that
will be deleted as soon as the pager function completes evaluation.
|
file.ext |
character(1L) an extension to append to file path passed to
|
threshold |
integer(1L) number of lines of output that triggers the use
of the pager; negative values lead to using
|
ansi |
TRUE or FALSE, whether the pager supports ANSI CSI SGR sequences. |
file.path |
character(1L), if not NA the diff will be written to this
location, ignoring the value of |
make.blocking |
TRUE, FALSE, or NA. Whether to wrap |
... |
additional arguments to pass on to |
flags |
character(1L), only for |
diff*
methods use “pagers” to help
manage large outputs and also to provide an alternative colored diff when the
terminal does not support them directly.
For OS X and *nix systems where less
is the pager and the
terminal supports ANSI escape sequences, output is colored with ANSI escape
sequences. If the output exceeds one screen height in size (as estimated by
console_lines
) it is sent to the pager.
If the terminal does not support ANSI escape sequences, or if the system
pager is not less
as detected by pager_is_less
, then the
output is rendered in HTML and sent to the IDE viewer
(getOption("viewer")
) if defined, or to the browser with
browseURL
if not. This behavior may seem sub-optimal for
systems that have ANSI aware terminals and ANSI aware pagers other than
less
, but these should be rare and it is possible to configure
diffobj
to produce the correct output for them (see examples).
There is a close relationship between pagers and Style
. The
Style
objects control whether the output is raw text, formatted
with ANSI escape sequences, or marked up with HTML. In order for these
different types of outputs to render properly, they need to be sent to the
right device. For this reason Style
objects come with a
Pager
configuration object pre-assigned so the output can render
correctly. The exact Pager
configuration object depends on the
Style
as well as the system configuration.
In any call to the diff*
methods you can always
specify both the Style
and Pager
configuration object
directly for full control of output formatting and rendering. We have tried
to set-up sensible defaults for most likely use cases, but given the complex
interactions involved it is possible you may need to configure things
explicitly. Should you need to define explicit configurations you can save
them as option values with
options(diffobj.pager=..., diffobj.style=...)
so that you do not need
to specify them each time you use diffobj
.
The Pager
configuration objects allow you to specify what device to
use as the pager and under what circumstances the pager should be used.
Several pre-defined pager configuration objects are available via
constructor functions:
Pager
: Generic pager just outputs directly to terminal; not
useful unless the default parameters are modified.
PagerOff
: Turn off pager
PagerSystem
: Use the system pager as invoked by
file.show
PagerSystemLess
: Like PagerSystem
, but provides
additional configuration options if the system pager is less
.
Note this object does not change the system pager; it only allows you to
configure it via the $LESS
environment variable which will have
no effect unless the system pager is set to be less
.
PagerBrowser
: Use getOption("viewer")
if defined, or
browseURL
if not
The default configuration for PagerSystem
and PagerSystemLess
leads to output being sent to the pager if it exceeds the estimated window
size, whereas PagerBrowser
always sends output to the pager. This
behavior can be configured via the threshold
parameter.
PagerSystemLess
's primary role is to correctly configure the
$LESS
system variable so that less
renders the ANSI escape
sequences as intended. On OS X more
is a faux-alias to less
,
except it does not appear to read the $LESS
system variable.
Should you configure your system pager to be the more
version of
less
, pager_is_less
will be tricked into thinking you
are using a “normal” version of less
and you will likely end up
seeing gibberish in the pager. If this is your use case you will need to
set-up a custom pager configuration object that sets the correct system
variables.
In most cases the simplest way to generate new pager configurations is to use
a list specification in the diff*
call.
Alternatively you can start with an existing Pager
object and change
the defaults. Both these cases are covered in the examples.
You can change what system pager is used by PagerSystem
by changing it
with options(pager=...)
or by changing the $PAGER
environment
variable. You can also explicitly set a function to act as the pager when
you instantiate the Pager
configuration object (see examples).
If you wish to define your own pager object you should do so by extending the
any of the Pager
classes. If the function you use to handle the
actual paging is non-blocking (i.e. allows R code evaluation to continue
after it is spawned, you should set the make.blocking
parameter to
TRUE to pause execution prior to deleting the temporary file that contains
the diff.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | ## We `dontrun` these examples as they involve pagers that should only be run
## in interactive mode
## Not run:
## Specify Pager parameters via list; this lets the `diff*` functions pick
## their preferred pager based on format and other output parameters, but
## allows you to modify the pager behavior.
f <- tempfile()
diffChr(1:200, 180:300, format='html', pager=list(file.path=f))
head(readLines(f)) # html output
unlink(f)
## Assuming system pager is `less` and terminal supports ANSI ESC sequences
## Equivalent to running `less -RFX`
diffChr(1:200, 180:300, pager=PagerSystemLess(flags="RFX"))
## If the auto-selected pager would be the system pager, we could
## equivalently use:
diffChr(1:200, 180:300, pager=list(flags="RFX"))
## System pager is not less, but it supports ANSI escape sequences
diffChr(1:200, 180:300, pager=PagerSystem(ansi=TRUE))
## Use a custom pager, in this case we make up a trivial one and configure it
## always page (`threshold=0L`)
page.fun <- function(x) cat(paste0("| ", readLines(x)), sep="\n")
page.conf <- PagerSystem(pager=page.fun, threshold=0L)
diffChr(1:200, 180:300, pager=page.conf, disp.width=getOption("width") - 2)
## Set-up the custom pager as the default pager
options(diffobj.pager=page.conf)
diffChr(1:200, 180:300)
## A blocking pager (this is effectively very similar to what `PagerBrowser`
## does); need to block b/c otherwise temp file with diff could be deleted
## before the device has a chance to read it since `browseURL` is not
## blocking itself. On OS X we need to specify the extension so the correct
## program opens it (in this case `TextEdit`):
page.conf <- Pager(pager=browseURL, file.ext="txt", make.blocking=TRUE)
diffChr(1:200, 180:300, pager=page.conf, format='raw')
## An alternative to a blocking pager is to disable the
## auto-file deletion; here we also specify a file location
## explicitly so we can recover the diff text.
f <- paste0(tempfile(), ".html") # must specify .html
diffChr(1:5, 2:6, format='html', pager=list(file.path=f))
tail(readLines(f))
unlink(f)
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.