R: Draw to a buffer that can be accessed directly

agg_capture {ragg} | R Documentation

## Draw to a buffer that can be accessed directly

### Description

Usually the point of using a graphic device is to create a file or show the graphic on the screen. A few times we need the image data for further processing in R, and instead of writing it to a file and then reading it back into R the 'agg_capture()' device lets you get the image data directly from the buffer. In contrast to the other devices this device returns a function, that when called will return the current state of the buffer.

### Usage

    agg_capture(
      width = 480,
      height = 480,
      units = "px",
      pointsize = 12,
      background = "white",
      res = 72,
      scaling = 1,
      bg
    )

### Arguments

`width` | The dimensions of the device
`height` | The dimensions of the device
`units` | The unit 'width' and 'height' is measured in, in either pixels (''px'‘), inches ('’in'‘), millimeters ('’mm'‘), or centimeter ('’cm'').
`pointsize` | The default pointsize of the device in pt. This will in general not have any effect on grid graphics (including ggplot2) as text size is always set explicitly there.
`background` | The background colour of the device
`res` | The resolution of the device. This setting will govern how device dimensions given in inches, centimeters, or millimeters will be converted to pixels. Further, it will be used to scale text sizes and linewidths
`scaling` | A scaling factor to apply to the rendered line width and text size. Useful for getting the right dimensions at the resolution that you need. If e.g. you need to render a plot at 4000x3000 pixels for it to fit into a layout, but you find that the result appears to small, you can increase the 'scaling' argument to make everything appear bigger at the same resolution.
`bg` | Same as 'background' for compatibility with old graphic device APIs

### Value

A function that when called returns the current state of the buffer. The return value of the function depends on the 'native' argument. If 'FALSE' (default) the return value is a 'matrix' of colour values and if 'TRUE' the return value is a 'nativeRaster' object.

### Examples

    cap <- agg_capture()
    plot(1:10, 1:10)

    # Get the plot as a matrix
    raster <- cap()

    # Get the plot as a nativeRaster
    raster_n <- cap(native = TRUE)

    dev.off()

    # Look at the output
    plot(as.raster(raster))

[Package ragg version 1.1.2 Index]