简体   繁体   中英

How to set default parameters for a graphical device?

I like to read white on black. So, in RI 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?

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")
})

Your problem here is that the device option is not yet set at the time .Rprofile is processed.
Therefore, options()$device is NULL at that point, which leads to the error you are observing.

The .First() function also gets executed before the packages from options("defaultPackages") are attached, so that gives the same error.

If you want to put something in .Rprofile , it would have to be like:

setHook(
    packageEvent("grDevices", "onLoad"),
    function(...) {
        f_device <- getOption("device")

        blackdevice <- function(...) {
            f_device(...)
            par(bg = "black")
            par(fg = "ivory1")
        }

        options(device = blackdevice)
    }
)

Note that this is a solution for the standard R GUI , but not for eg RStudio, which sets its own "RStudioGD" device afterwards (and requires that grDevices is already loaded).

(This also explains the awkward approach (from the question you linked) of re-defining "RStudioGD" in .Rprofile to get it ahead of "tools:rstudio" on the search path)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM