r - 初始化图形设备时如何设置设备复制参数?

标签 r

我正在 R 包中的图形设备上工作,需要设置一些
设备上的文本/标签在初始化或重置时的图形参数。
这个特性在 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.


我如何从包中实际访问和初始化“设备副本”?
我能找到的只是一个评论,其中包含一个较旧的,
复制粘贴评论于 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 :)

最佳答案

我不知道我是否完全理解你想要做什么,但我想你可能会发现 this guide有用。一些关键摘录:

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.


然后:

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().


最后:

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.


例如,circle设备的方法具有以下签名:
circle ( numeric, numeric, numeric, R_GE_gcontextPtr, DevDescPtr )
R_GE_gcontextPtr 是:

...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"

关于r - 初始化图形设备时如何设置设备复制参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62598424/

相关文章:

r - 当我们查找向量的所有元素的所有匹配索引时,R 中是否有一个函数可以避免使用循环?

r - R-x 中的 Wilcoxon 检验必须是数字错误

r - 如何在R中反转反双曲正弦变换?

r - openxlsx 将if公式从R写入excel

r - 如何不删除 R 交叉表中的因子水平?

r - 如何在 r 中从 gridExtra 主题化 tableGrob 对象

r - 在 R 代码中使用嵌套 for 循环以减少冗余

多核中的R caret nnet软件包

r - 在 R 中与 dplyr 连接时嵌套重复变量

r - 将长表转换为仅包含一列计数的宽格式