From plroebuck at mdanderson.org  Fri May 30 22:33:00 2008
From: plroebuck at mdanderson.org (Paul Roebuck)
Date: Fri, 30 May 2008 15:33:00 -0500 (CDT)
Subject: [Rd] R-2.8.0 : get platform device with get(getOption("device"))
In-Reply-To: <Pine.OSF.4.58.0805082328330.130895@wotan.mdacc.tmc.edu>
References: <8613cc220805070831w5e52a1cvf9ff93e8a33c16c6@mail.gmail.com>
	<alpine.LFD.1.10.0805072004010.32764@gannet.stats.ox.ac.uk>
	<482233E8.5090605@biostat.ku.dk>
	<alpine.LFD.1.10.0805080530350.3094@gannet.stats.ox.ac.uk>
	<48229EBB.3060903@biostat.ku.dk>
	<Pine.OSF.4.58.0805082328330.130895@wotan.mdacc.tmc.edu>
Message-ID: <Pine.OSF.4.58.0805301513570.289204@wotan.mdacc.tmc.edu>

On Fri, 9 May 2008, Paul Roebuck wrote:

> On Thu, 8 May 2008, Peter Dalgaard wrote:
>
> > >> [SNIP Ripley conversation]
> >
> > o    The "device" option now is a function in some standard setups.
> > Consequentially, get(getOption("device")) will fail; programmers should
> > usually use dev.new() instead. (Assuming that the device is a character
> > string has been incorrect since 2.5.0.)
>
> Prof Brian Ripley noted [in another recent message] that
> it was also wrong to assume ANY arguments for dev.new
> method. If we are only concerned with interactive screen
> devices (i.e., x11, windows, quartz), would it be safe to
> come up with a method that can take basic dimension arguments
> (width & height)? Suggestions for an R-Core approved method
> of doing so?
>
> We use this functionality every day for creating multiple
> onscreen windows with specified dimensions.

Prof. Ripley replied to original note but not to my main
concern. Are there problems with this alternate approach
to creating new interactive devices (R-2.8+)?
Comments/Suggestions appreciated.


##----------------------------------------------------------
## Create new interactive device. Arguments must be named.
intdev.new <- function(...) {
    dev <- getOption("device")
    if (is.character(dev)) {
        if (exists(dev, .GlobalEnv))
            dev <- get(dev, .GlobalEnv)
        else if (exists(dev, asNamespace("grDevices")))
            dev <- get(dev, asNamespace("grDevices"))
        else {
            stop(gettextf("device '%s' not found", dev),
                 domain = NA)
        }
    }

    if (!is.function(dev)) {
        stop("invalid setting for 'getOption(\"device\")'")
    } else if (!dev.interactive(TRUE)) {
        stop("device must be interactive")
    }

    arglist <- as.list(formals(dev))
    extras <- match.call(expand.dots=FALSE)$...
    if (length(extras) > 0) {
        matched <- !is.na(match(names(extras), names(arglist)))
        for (a in names(extras)[matched]) {
            arglist[[a]] <- extras[[a]]
            extras[[a]] <- NULL
        }
        if (length(extras) > 0) {
            stop(sprintf("unnamed argument(s) not allowed: %s",
                         paste(extras, collapse=", ")))
        }
        if (any(!matched)) {
            warning(sprintf("arguments ignored: %s",
                            paste(names(extras[!matched]),
                                  collapse=", ")))
        }
    }

    do.call(dev, arglist)
}

## Create interactive device using common arguments
R> intdev.new(width=5, height=5, title="Test")


TIA

----------------------------------------------------------
SIGSIG -- signature too long (core dumped)



[Rd] R-2.8.0 : get platform device with get(getOption("device"))
Paul Roebuck