Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default parameters for a graphical device?

Tags:

r

I like to read white on black. So, in R I would do something along the lines of:

par (bg = "black")
par (fg = "ivory1")

I would like these options to be set by default. However, one does not simply write these lines in .Rprofile because, as I understand, at the time it gets executed, the graphical device is not yet initialized. Rather, as suggested in another answer, one should re-assign options()$device to include the necessary option setting. I did not have success in that.

 

This is what I tried:

~/.Rprofile

f_device <- options()$device

blackdevice <- function (...) {

    f_device(...)

    par (bg       = "black")
    par (fg       = "ivory1")
}

options (device = blackdevice)

The idea here is to save the original device function to another variable, and then call it from my new device function. What I get is:

Error in f_device(...) : could not find function "f_device"

— At the time I run plot (something).

 

Another idea I had is to go like that:

~/.Rprofile

.First <- function () {

    options(f_device = options()$device)

    blackdevice <- function (...) {

        options()$f_device(...)

        par (bg       = "black")
        par (fg       = "ivory1")
    }

    options (device = blackdevice)
}

— Assigning the original device someplace else in options. But this leads to:

Error in (function (...)  : attempt to apply non-function

 

I'm out of ideas. Can you help me figure this out?

like image 403
Ignat Insarov Avatar asked Feb 17 '18 08:02

Ignat Insarov


1 Answers

One solution seems to be to define a 'hook' that is called when a new plot is created. From the documentation of plot.new:

There are two hooks called ‘"before.plot.new"’ and ‘"plot.new"’ (see ‘setHook’) called immediately before and after advancing the frame. The latter is used in the testing code to annotate the new page. The hook function(s) are called with no argument. (If the value is a character string, ‘get’ is called on it from within the ‘graphics’ namespace.)

The following seems to work:

setHook("before.plot.new", function(...) {
  par(bg = "black",
    fg = "ivory1",
    col.axis = "ivory1",
    col.lab = "ivory1",
    col.main = "ivory1",
    col.sub = "ivory1")
})
like image 67
Jan van der Laan Avatar answered Sep 22 '22 17:09

Jan van der Laan