[R] MakeActiveBinding help needed

Bert Gunter gunter.berton at gene.com
Thu Jan 7 00:42:32 CET 2010


This one made my head spin!

I think the problem might be that you are calling the dev.new() command in
your script before loading the grDevice (not graphics) package. The easiest
way to deal with this -- documented in ?Startup, btw -- is just to modify
your original function to:

invisible(makeActiveBinding('newdev', function(...) 
grDevices::dev.new(restoreConsole=T), .GlobalEnv))

(or explicitly load the grDevices -- not graphics -- package before the
above in your script)

Actually, I think why any of this happens -- as ordinarily, defining a
function that uses functions from not yet loaded packages makes no
difference until the function is actually called -- has to do with how
makeActiveBinding() works. For that, you'd have to go the the underlying C
code. But if the above still opens the graphics windows, you could close
them by defining a .First function in your Rprofile.site file that issues
the graphics.off() function, as this function will be executed AFTER
Rprofile.site is executed and .Rdata is loaded.


Bert Gunter
Genentech Nonclinical Biostatistics
 
 

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Carl Witthoft
Sent: Wednesday, January 06, 2010 2:30 PM
To: r-help at r-project.org
Subject: [R] MakeActiveBinding help needed

Hi,
I wanted a Q&D way to open a new graphics window but keep the focus in 
the console window (under Windows and the Rgui), so I wrote a line into 
my Rprofile.site file as follows:

invisible(makeActiveBinding('newdev', function(...) 
dev.new(restoreConsole=T), .GlobalEnv))

(That is all on one line, incase the mailer re-parses it). This gives me 
a command "newdev" which does not require me to enter a pair of 
parentheses to execute it.

This worked just fine so far as the command goes.  Here's the problem: 
this definition is stored as part of my .Rdata file, and the next time I 
start up R, I get a crash.   I tracked that down to an odd fact:  while 
R is loading this definition from .Rdata, it tries to execute the 
command, i.e. open a graphics window!  Since the graphics library hasn't 
been loaded yet, crash happens.

I re-wrote my definition to

invisible(makeActiveBinding('newdev', function(...) {library(graphics); 
dev.new(restoreConsole=T)}, .GlobalEnv))

and then R booted up just fine, except that two new graphics windows 
were created -- one from loading "newdev" from .Rdata and one from 
executing the Rprofile script with the "newdev" definition in it.

So my question is:  is there some way I can define this 
makeActiveBinding item without R trying to execute it at the point of 
loading (either from Rprofile or from .Rdata)?

thanks
Carl

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list