Last data update: 2014.03.03

R: Captures a plot such that it can be redrawn later/elsewhere
capturePlotR Documentation

Captures a plot such that it can be redrawn later/elsewhere

Description

Captures a plot such that it can be redrawn later/elsewhere.

This feature is only supported in R (>= 3.3.0).

Usage

capturePlot(expr, envir=parent.frame(), type=pdf, ...)

Arguments

expr

The expression of graphing commands to be evaluated.

envir

The environment where expr should be evaluated.

type

The type of graphics device used in the background. The choice should not matter since the result should be identical regardless.

...

Additional arguments passed to the graphics device.

Details

Note that plot dimensions/aspect ratios are not recorded. This means that one does not have to worry about those when recording the plot. Instead, they are specified when setting up the graphics device(s) in which the recorded plot is replayed (see example).

Value

Returns a recordedplot object, which can be replayPlot():ed. If replayed in an interactive session, the plot is displayed in a new window. For conveniency, the object is also replayed when print():ed.

Author(s)

Henrik Bengtsson

References

[1] Paul Murrell et al., Recording and Replaying the Graphics Engine Display List, December 2015. https://www.stat.auckland.ac.nz/~paul/Reports/DisplayList/dl-record.html

See Also

Internally recordPlot() is used.

Examples

if (getRversion() >= "3.3.0") {

g <- capturePlot({
  plot(1:10)
})

## Display
print(g)

## Display with a 2/3 height-to-width aspect ratio
toDefault(aspectRatio=2/3, { print(g) })

## Redraw to many output formats
devEval(c("png", "eps", "pdf"), aspectRatio=2/3, print(g))

} ## if (getRversion() >= "3.3.0")

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(R.devices)
R.devices v2.14.0 (2016-03-08) successfully loaded. See ?R.devices for help.
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/R.devices/capturePlot.Rd_%03d_medium.png", width=480, height=480)
> ### Name: capturePlot
> ### Title: Captures a plot such that it can be redrawn later/elsewhere
> ### Aliases: capturePlot
> ### Keywords: device
> 
> ### ** Examples
> 
> if (getRversion() >= "3.3.0") {
+ 
+ g <- capturePlot({
+   plot(1:10)
+ })
+ 
+ ## Display
+ print(g)
+ 
+ ## Display with a 2/3 height-to-width aspect ratio
+ toDefault(aspectRatio=2/3, { print(g) })
+ 
+ ## Redraw to many output formats
+ devEval(c("png", "eps", "pdf"), aspectRatio=2/3, print(g))
+ 
+ } ## if (getRversion() >= "3.3.0")
$png
[1] "figures/Rplot.png"

$eps
[1] "figures/Rplot.eps"

$pdf
[1] "figures/Rplot.pdf"

> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>