Где recordPlot () хранит par () и layout () для первого графика? - PullRequest
1 голос
/ 28 декабря 2011

На следующем записанном графике я не могу найти инструкцию, которая создает макет и устанавливает параметры par () до первого вызова plot.new ()

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 () из необработанных данных?

Редактировать
Предупреждение: грязный хак.чтобы закодировать начальное состояние в списке команд, вот как:

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 Ответ

3 голосов
/ 28 декабря 2011

Вы, вероятно, правы относительно того, что в recorded[[2]]. Я подозреваю, что он содержит SEXP, который "красиво скрывает внутренности", на которые ссылается этот комментарий из источников R:

/****************************************************************
 * 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)
 */
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...