recordPlot()在哪里存储第一个绘图的par()和layout()?

时间:2011-12-27 22:42:28

标签: r

在下面的记录图中,我找不到构造布局的指令,并在第一个plot.new调用()

之前设置par()设置。
library( PerformanceAnalytics )
data( managers )

# write the plot to the open device
suppressWarnings(charts.RollingRegression(managers[, 1:6], managers[, 8, drop=FALSE], Rf = .04/12, colorset = rich6equal, legend.loc="topleft"))

# record = investigate the primitive calls.  notice no par() nor layout() before the first call to plot.new()
recorded = recordPlot()
lapply(recorded[[1]], "[[", 1 )

# as a result, the following creates one plot per page, not 3 on the same page:
lapply( recorded[[ 1 ]] , function( x ) { do.call( x[[ 1 ]] , as.list( x[[ 2 ]] ) ) } )

也许这是在recorded[[ 2 ]]中编码的,看起来像是某种编码的原始数据?如果是这样,我怎么能从原始数据中的第一个plot.new()之前获取insructions?

修改
警告:肮脏的黑客。
如果要在指令列表中对初始状态进行编码,请按以下步骤进行:

tryCatch( dev.off() , error = function( e ) {} )
plot.new()
par( new = TRUE )
originalLayoutFunction = graphics:::layout
graphicsEnvironment = as.environment( "package:graphics" )
newLayoutFunction = function( ... )
{
    originalLayoutFunction( ... )
    par( mfg = c( 1 , 1 ) )
}

unlockBinding( "layout" , env = graphicsEnvironment )
assign( "layout" , newLayoutFunction , envir = graphicsEnvironment )
lockBinding( "layout" , env = graphicsEnvironment )

tryCatch( YOUR_PLOT_CALL_HERE , finally = 
                                { 
                                    unlockBinding( "layout" , env = graphicsEnvironment )
                                    assign( "layout" , originalLayoutFunction , env = graphicsEnvironment )
                                    lockBinding( "layout" , env = graphicsEnvironment )                                    
                                } )
recordedPlot = recordPlot()
dev.off()

1 个答案:

答案 0 :(得分:3)

你对recorded[[2]]中的内容可能是正确的。我怀疑它包含来自R来源的评论中引用的SEXP“很好地隐藏内部”:

/****************************************************************
 * GEcreateSnapshot
 ****************************************************************
 */

/* Create a recording of the current display,
 * including enough information from each registered
 * graphics system to be able to recreate the display
 * The structure created is an SEXP which nicely hides the
 * internals, because noone should be looking in there anyway
 * The product of this call can be stored, but should only
 * be used in a call to GEplaySnapshot.
 */

在同一个文件中稍微向下($SRC_HOME/src/main/engine.c)是另一个可能有启发性的段落。

与上面的评论一样(和recordPlot帮助文件一样),它也带有强烈的警告,不要尝试使用recordPlot()存储的对象。他们都说,“这里有龙”,看起来你们开始遇到被警告过的人了;)

/****************************************************************
 * GEplaySnapshot
 ****************************************************************
 */

/* Recreate a saved display using the information in a structure
 * created by GEcreateSnapshot.
 *
 * The graphics engine assumes that it is getting a snapshot
 * that was created in THE CURRENT R SESSION
 * (Thus, it can assume that registered graphics systems are
 *  in the same order as they were when the snapshot was
 *  created -- in patricular, state information will be sent
 *  to the appropriate graphics system.)
 * [With only two systems and base registered on each device at
 * creation, that has to be true: and grid does not save any state.]
 *
 *  It also assumes that the system that created the snapshot is
 *  still loaded (e.g. the grid namespace has not been unloaded).
 *
 * It is possible to save a snapshot to an R variable
 * (and therefore save and reload it between sessions and
 *  even possibly into a different R version),
 * BUT this is strongly discouraged
 * (in the documentation for recordPlot() and replayPlot()
 *  and in the documentation for the Rgui interface on Windows)
 */