Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the device-copy parameters when initializing a graphics device?

Tags:

r

I'm working on a graphics device in a R package, and need to set some graphical parameters for text / labels on the devices as they are initialized or reset.

This feature is described in R Internals:

The three copies of the GPar structure are used to store the current parameters (accessed via gpptr), the ‘device copy’ (accessed via dpptr) and space for a saved copy of the ‘device copy’ parameters. The current parameters are, clearly, those currently in use and are copied from the ‘device copy’ whenever plot.new() is called (whether or not that advances to the next ‘page’). The saved copy keeps the state when the device was last completely cleared (e.g. when plot.new() was called with par(new=TRUE)), and is used to replay the display list.

How do I, from a package, actually access and initialize the "device copy"?

All I've been able to find is a comment containing an older, copy-pasted comment in GraphicsDevice.h:

  * 2. I found this comment in the doc for dev_Open -- looks nasty
  *    Any known instances of such a thing happening?  Should be
  *    replaced by a function to query the device for preferred gpars
  *    settings? (to be called when the device is initialised)
          *
          * NOTE that it is perfectly acceptable for this
          * function to set generic graphics parameters too
          * (i.e., override the generic parameter settings
          * which GInit sets up) all at the author's own risk
          * of course :)
like image 367
Neal Fultz Avatar asked Nov 07 '22 06:11

Neal Fultz


1 Answers

I don't know if I fully understand what you're trying to do, but I think you may find this guide to be useful. Some key excerpts:

To create a running graphics device with our own functions, we call the graphicsDevice() function. While there are several methods for this, essentially we give it a list of named functions that specify the implementation of some or all of the 21 graphical primitive operations. We might give this as a list or as an instance of RDevDescMethods or of a sub-class that we define for a particular type of device. So we focus on writing these functions.

Then:

Each of the methods is passed an object of class DevDescPtr. This is also the type of the value returned by the top-level function graphicsDevice() . This is a reference the C-level data structure that represents the graphics device. We can use this to query the settings of the graphics device.

Some of these fields in the device are used when initializing the device rather than within the functions (e.g. those whose names are prefixed with "start"). Other fields are structural information about the rendering of different aspects of the device. For example, we can find the dimensions of the drawing area, The DevDescPtr class is essentially an opaque data type in R (containing an external pointer to the C-level data structure) and is intended to be used as if it were an R-level list. We can use the $ operator to access individual fields and we can find the names of these fields with names().

and finally:

Under some rare circumstances, it is convenient to convert the reference to an R object. We can do this by coercing it to the corresponding R class named DevDesc (i.e. with the "Ptr" remove), i.e. as(dev, "DevDesc"). This copies each of the fields in the C-level structure to the corresponding slot in the R class.

For example, the circle method of the device has this signature:

circle ( numeric, numeric, numeric, R_GE_gcontextPtr, DevDescPtr )

R_GE_gcontextPtr is:

...another reference to an instance of a C-level data type. This is the information about the "current" settings of the device. This gives us information about the current pen/foreground color, the background color, the setting for the gamma level, the line width, style, join, the character point size and expansion/magnification, and the font information. The available fields are

names(new("R_GE_gcontextPtr"))
 [1] "col"        "fill"       "gamma"      "lwd"        "lty"       
 [6] "lend"       "ljoin"      "lmitre"     "cex"        "ps"        
[11] "lineheight" "fontface"   "fontfamily"
like image 56
lfalin Avatar answered Nov 15 '22 05:11

lfalin