Expand description

Graphic Device Operations

Control an existing graphic device


Implement a new graphic device

The following two things are needed to implement a graphic device.

  • DeviceDriver trait: the actual implementation of graphic device methods.
  • DeviceDescriptor struct: the parameters that might differ per device instance (e.g. sizes, and colors).

For example, the following code implements a simple graphic device that shows a message when it’s activated (and ignores everything else).

use extendr_api::{
    graphics::{DeviceDescriptor, DeviceDriver, DevDesc},

struct MyDevice<'a> {
    welcome_message: &'a str,

impl<'a> DeviceDriver for MyDevice<'a> {
    fn activate(&mut self, _dd: DevDesc) {
        let welcome_message = self.welcome_message;
        rprintln!("message from device: {welcome_message}");

/// Create a new device.
/// @export
fn my_device(welcome_message: String) {
    let device_driver = MyDevice {
        welcome_message: welcome_message.as_str(),
    let device_descriptor = DeviceDescriptor::new();
    let device = device_driver.create_device::<MyDevice>(device_descriptor, "my device");

This can be called from R.

my_device("I'm so active!!!")
#> message from device: I'm so active!!!


pub use device_descriptor::*;
pub use device_driver::*;



A row-major array of pixels. One pixel is 32-bit, whose each byte represents alpha, blue, green, and red in the order.


Type Definitions