From noreply at r-forge.r-project.org Fri Jan 22 18:41:37 2016 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Fri, 22 Jan 2016 18:41:37 +0100 (CET) Subject: [Dplr-commits] r1009 - in pkg/dplR: . R man Message-ID: <20160122174137.524A4187812@r-forge.r-project.org> Author: mvkorpel Date: 2016-01-22 18:41:36 +0100 (Fri, 22 Jan 2016) New Revision: 1009 Modified: pkg/dplR/ChangeLog pkg/dplR/DESCRIPTION pkg/dplR/NAMESPACE pkg/dplR/R/rasterPlot.R pkg/dplR/R/wavelet.plot.R pkg/dplR/man/rasterPlot.Rd pkg/dplR/man/wavelet.plot.Rd pkg/dplR/man/xskel.ccf.plot.Rd pkg/dplR/man/xskel.plot.Rd Log: * Added Cairo graphics support to rasterPlot(): new argument 'Cairo' * wavelet.plot() can now pass ... arguments to rasterPlot() * DESCRIPTION: Cairo (>= 1.5-0) is now in Suggests * NAMESPACE: Importing dev.capture from grDevices * The corresponding Rd manuals were updated accordingly * Small tweaks in other manuals Modified: pkg/dplR/ChangeLog =================================================================== --- pkg/dplR/ChangeLog 2015-12-18 13:30:18 UTC (rev 1008) +++ pkg/dplR/ChangeLog 2016-01-22 17:41:36 UTC (rev 1009) @@ -7,8 +7,13 @@ File: rasterPlot.R ------------------ -- rasterPlot() now reverts to normal plotting if png device is unavailable or - raster images are not supported. Previously an error would be produced. +- Added option 'Cairo' to control the preferred bitmap + device. FALSE (the default) for png() as before, and TRUE for + Cairo() as a new option. If the preferred device is unavailable, + the other one will be tried. +- rasterPlot() now reverts to normal plotting if bitmap device is + unavailable or raster images are not supported. Previously an + error would be produced. - The function now also works if no high level plot exists, i.e. if plot.new() has not been called. - Added option 'draw' to control whether the raster plot is to be drawn (TRUE, @@ -25,9 +30,20 @@ - Made a new function to calculate the mean value of each tree when there are multiple cores. +File: wavelet.plot.R +-------------------- +- wavelet.plot() now passes ... arguments to rasterPlot(). + +File: DESCRIPTION +----------------- +- New Suggested package: Cairo. Used (if available) in + rasterPlot() if the png() device is unavailable or if the user + specifies 'Cairo=TRUE'. + File: NAMESPACE ------------- - Added treeMean function. +- Importing dev.capture() from grDevices. Various .R files ---------------- Modified: pkg/dplR/DESCRIPTION =================================================================== --- pkg/dplR/DESCRIPTION 2015-12-18 13:30:18 UTC (rev 1008) +++ pkg/dplR/DESCRIPTION 2016-01-22 17:41:36 UTC (rev 1009) @@ -3,7 +3,7 @@ Type: Package Title: Dendrochronology Program Library in R Version: 1.6.4 -Date: 2015-12-18 +Date: 2016-01-22 Authors at R: c(person("Andy", "Bunn", role = c("aut", "cph", "cre", "trl"), email = "andy.bunn at wwu.edu"), person("Mikko", "Korpela", role = c("aut", "trl")), person("Franco", "Biondi", @@ -22,8 +22,9 @@ Matrix (>= 1.0-3), digest (>= 0.2.3), gmp (>= 0.5-5), matrixStats (>= 0.50.0), png (>= 0.1-1), R.utils (>= 1.32.0), stringi (>= 0.2-2), stringr (>= 0.4), XML (>= 2.1-0) -Suggests: Biobase, dichromat (>= 1.2-3), foreach, forecast, iterators, - knitr, RColorBrewer, testthat (>= 0.8), tikzDevice, waveslim +Suggests: Biobase, Cairo (>= 1.5-0), dichromat (>= 1.2-3), foreach, + forecast, iterators, knitr, RColorBrewer, testthat (>= 0.8), + tikzDevice, waveslim Description: Perform tree-ring analyses such as detrending, chronology building, and cross dating. Read and write standard file formats used in dendrochronology. Modified: pkg/dplR/NAMESPACE =================================================================== --- pkg/dplR/NAMESPACE 2015-12-18 13:30:18 UTC (rev 1008) +++ pkg/dplR/NAMESPACE 2016-01-22 17:41:36 UTC (rev 1009) @@ -10,7 +10,7 @@ importFrom(gmp, as.bigq, as.bigz, chooseZ, is.bigq) importFrom(grDevices, dev.hold, dev.flush, rainbow, dev.capabilities, - dev.cur, png, dev.off, dev.set, devAskNewPage) + dev.cur, png, dev.off, dev.set, devAskNewPage, dev.capture) importFrom(grid, gpar, grid.lines, grid.newpage, grid.polygon, grid.segments, grid.text, pushViewport, seekViewport, unit, Modified: pkg/dplR/R/rasterPlot.R =================================================================== --- pkg/dplR/R/rasterPlot.R 2015-12-18 13:30:18 UTC (rev 1008) +++ pkg/dplR/R/rasterPlot.R 2016-01-22 17:41:36 UTC (rev 1009) @@ -1,7 +1,8 @@ rasterPlot <- function(expr, res = 150, region=c("plot", "figure"), antialias, bg = "transparent", interpolate = TRUE, draw = TRUE, - ...) { + Cairo = FALSE, ...) { draw2 <- isTRUE(as.logical(draw)[1L]) + Cairo2 <- isTRUE(as.logical(Cairo)[1L]) ## Plotting commands 'expr' will be evaluated in the environment ## of the caller of rasterPlot() pf <- parent.frame() @@ -11,9 +12,42 @@ message("device does not support raster images") fallback <- TRUE } - if (sum(capabilities(c("cairo", "png", "aqua")), na.rm=TRUE) == 0) { - message("png device unavailable") - fallback <- TRUE + cairoRaster <- FALSE + fallback <- TRUE + for (k in 1:2) { + if (Cairo2) { + if (requireNamespace("Cairo", quietly = TRUE)) { + caps <- Cairo::Cairo.capabilities() + if (isTRUE(as.vector(caps["raster"]))) { + fallback <- FALSE + cairoRaster <- TRUE + } else if (isTRUE(as.vector(caps["png"]))) { + fallback <- FALSE + } else { + message("png and raster unsupported in this Cairo library") + } + if (!fallback) { + if (k == 2) { + message("using Cairo device") + } + break + } + } else { + message("Cairo device unavailable") + } + Cairo2 <- FALSE + } else { + if (sum(capabilities(c("cairo", "png", "aqua")), na.rm=TRUE) > 0) { + fallback <- FALSE + if (k == 2) { + message("using png device") + } + break + } else { + message("png device unavailable") + } + Cairo2 <- TRUE + } } if (fallback && !draw2) { return(NULL) @@ -94,7 +128,18 @@ ## in the original device. Resolution (points per inch) is the ## argument 'res'. fname <- tempfile(fileext = ".png") - if (missing(antialias)) { + if (Cairo2) { + if (cairoRaster) { + cairoType <- "raster" + cairoFile <- "" + } else { + cairoType <- "png" + cairoFile <- fname + } + Cairo::Cairo(width = pngWidthHeight[1], height = pngWidthHeight[2], + units = "in", dpi = res, bg = bg, + type = cairoType, file = cairoFile, ...) + } else if (missing(antialias)) { png(fname, width = pngWidthHeight[1], height = pngWidthHeight[2], units = "in", res = res, bg = bg, ...) } else { @@ -104,7 +149,9 @@ ## Record things to do on exit (will be removed from list one-by-one) on.exit(dev.off()) on.exit(dev.set(curDev), add=TRUE) - on.exit(unlink(fname), add=TRUE) + if (!cairoRaster) { + on.exit(unlink(fname), add=TRUE) + } devAskNewPage(FALSE) par(mfcol=c(1,1)) par(omi=rep(0, 4)) @@ -115,24 +162,33 @@ plot.new() par(op) eval(expr, pf) - on.exit(dev.set(curDev)) - on.exit(unlink(fname), add=TRUE) - ## Close the png device - dev.off() - on.exit(unlink(fname)) + if (cairoRaster) { + ## Capture raster data from device before closing + rasterData <- dev.capture(native = TRUE) + on.exit(dev.set(curDev)) + dev.off() + on.exit() + } else { + on.exit(dev.set(curDev)) + on.exit(unlink(fname), add=TRUE) + dev.off() + on.exit(unlink(fname)) + } ## Return to the original plot (device) dev.set(curDev) - ## Read the png image to memory - pngData <- readPNG(fname, native=TRUE) - on.exit() - ## Remove the temporary .png file - unlink(fname) + if (!cairoRaster) { + ## Read the png image to memory + rasterData <- readPNG(fname, native=TRUE) + on.exit() + ## Remove the temporary .png file + unlink(fname) + } if (!draw2) { - return(pngData) + return(rasterData) } if (plotRegion || marzero) { ## Add a raster image to the plot region of the original plot - rasterImage(pngData, xleft = usrLeft, ybottom = usrBottom, + rasterImage(rasterData, xleft = usrLeft, ybottom = usrBottom, xright = usrRight, ytop = usrTop, interpolate = interpolate) } else { @@ -141,7 +197,7 @@ on.exit(par(xpd = op[["xpd"]])) ## Add a raster image to the figure region of the original plot fc <- figCoord() - rasterImage(pngData, xleft = fc[1], ybottom = fc[2], + rasterImage(rasterData, xleft = fc[1], ybottom = fc[2], xright = fc[3], ytop = fc[4], interpolate = interpolate) } Modified: pkg/dplR/R/wavelet.plot.R =================================================================== --- pkg/dplR/R/wavelet.plot.R 2015-12-18 13:30:18 UTC (rev 1008) +++ pkg/dplR/R/wavelet.plot.R 2016-01-22 17:41:36 UTC (rev 1009) @@ -9,7 +9,7 @@ crn.col = "black", crn.lwd = 1,coi.col='black', crn.ylim = range(wave.list$y) * c(0.95, 1.05), side.by.side = FALSE, - useRaster = FALSE, res = 150, reverse.y = FALSE) + useRaster = FALSE, res = 150, reverse.y = FALSE, ...) { ## Wavelet transform variables: @@ -135,8 +135,34 @@ as.double(wavelet.levels), key.cols)) if (useRaster2) { - tryCatch(rasterPlot(cl, res = res, - antialias = "none", interpolate = FALSE), + args <- list(...) + argNames <- names(args) + if (is.null(argNames)) { + args <- NULL + } else { + args <- args[!is.na(argNames) & nzchar(argNames)] + } + if (length(args) == 0L) { + Call <- as.call(c(as.name("rasterPlot"), + alist(expr = cl, res = res, antialias = "none", + interpolate = FALSE))) + } else { + Call <- as.call(c(as.name("rasterPlot"), args)) + Call <- as.list(match.call(rasterPlot, Call)) + anam <- names(Call[-1L]) + Call[["expr"]] <- quote(cl) + Call[["res"]] <- quote(res) + Call[["region"]] <- "plot" + Call[["draw"]] <- TRUE + if (!("antialias" %in% anam)) { + Call[["antialias"]] <- "none" + } + if (!("interpolate" %in% anam)) { + Call[["interpolate"]] <- FALSE + } + Call <- as.call(Call) + } + tryCatch(eval(Call), error = function(e) { message(as.character(e), appendLF = FALSE) message("reverting to useRaster=FALSE") Modified: pkg/dplR/man/rasterPlot.Rd =================================================================== --- pkg/dplR/man/rasterPlot.Rd 2015-12-18 13:30:18 UTC (rev 1008) +++ pkg/dplR/man/rasterPlot.Rd 2016-01-22 17:41:36 UTC (rev 1009) @@ -6,13 +6,14 @@ } \description{ This function takes plotting commands and uses a temporary - \code{\link{png}} bitmap graphics device to capture their output. The + bitmap graphics device to capture their output. The resulting raster image is drawn in the plot or figure region of the active high-level plot. A new plot is started if one does not exist. } \usage{ rasterPlot(expr, res = 150, region = c("plot", "figure"), antialias, - bg = "transparent", interpolate = TRUE, draw = TRUE, \dots) + bg = "transparent", interpolate = TRUE, draw = TRUE, + Cairo = FALSE, \dots) } \arguments{ \item{expr}{ @@ -38,11 +39,12 @@ Antialiasing argument passed to \code{\link{png}}. The default (missing argument) is probably good for line plots but \code{"none"} is preferred for images in which color signifies value - of data. + of data. Unused if a \code{\link[Cairo]{Cairo}} device is used instead of + \code{png}. } \item{bg}{ Background color of the raster plot, an argument passed to - \code{\link{png}}. If the default \code{"transparent"} does not + the bitmap device. If the default \code{"transparent"} does not work, try \code{"white"} or another color. Note that a non-transparent background will mask any previous content in the figure or plot region, depending on the value of \code{\var{region}}. @@ -57,13 +59,19 @@ A \code{logical} flag. Draw the results (\code{TRUE}, the default) or return an image object (\code{FALSE})? } + \item{Cairo}{ + A \code{logical} flag. \code{TRUE} for preferring a + \code{\link[Cairo]{Cairo}} to \code{\link{png}} as the bitmap device, + \code{FALSE} (the default) for the opposite. If the preferred + device cannot be used, the other one will be tried. + } \item{\dots}{ - Other arguments to \code{\link{png}}. + Other arguments to \code{\link{png}} or \code{\link[Cairo]{Cairo}}. } } \details{ The appropriate graphical parameters of the current graphics device - are copied to the temporary \code{\link{png}} device. Therefore the + are copied to the temporary bitmap device. Therefore the appearance of the raster contents should be almost the same as when directly drawn. @@ -85,21 +93,29 @@ If \code{\var{draw}} is \code{FALSE}, an object of class \code{"nativeRaster"} is returned. The object can be used as input for \code{\link{rasterImage}} or \code{\link{grid.raster}}. See - \code{\link{readPNG}}. If no \code{\link{png}} device is available + \code{\link{readPNG}}. If no bitmap device is available (see \sQuote{Note}), \code{NULL} is returned. } \author{ Mikko Korpela } \note{ - The graphics device used for the output must have support for - including raster images. See \code{"rasterImage"} in - \code{\link{dev.capabilities}}. - The \R build must have a functional \code{\link{png}} device, which - requires one of the following \code{\link{capabilities}}: - \code{"png"}, \code{"aqua"} or \code{"cairo"}. + \itemize{ + \item{The graphics device used for the output must have support for + including raster images. See \code{"rasterImage"} in + \code{\link{dev.capabilities}}.} + + \item{The \R build must have a functional \code{\link{png}} device, + which requires one of the following \code{\link{capabilities}}: + \code{"png"}, \code{"aqua"} or \code{"cairo"}. Alternatively, a + \code{\link[Cairo]{Cairo}} device from package Cairo must be + available with \code{\link[Cairo]{Cairo.capabilities}} + \code{"raster"} or \code{"png"}.} + + } + If either of these requirements is not met, at least one \code{\link{message}} is generated and the function reverts to regular plotting. The \code{\var{bg}} argument is then handled by drawing a Modified: pkg/dplR/man/wavelet.plot.Rd =================================================================== --- pkg/dplR/man/wavelet.plot.Rd 2015-12-18 13:30:18 UTC (rev 1008) +++ pkg/dplR/man/wavelet.plot.Rd 2016-01-22 17:41:36 UTC (rev 1009) @@ -18,7 +18,7 @@ crn.col = "black", crn.lwd = 1,coi.col='black', crn.ylim = range(wave.list$y) * c(0.95, 1.05), side.by.side = FALSE, - useRaster = FALSE, res = 150, reverse.y = FALSE) + useRaster = FALSE, res = 150, reverse.y = FALSE, \dots) } \arguments{ \item{wave.list}{A \code{list}. Output from \code{\link{morlet}}.} @@ -61,6 +61,10 @@ \item{reverse.y}{A \code{logical} flag. If \code{TRUE}, the Y-axis will be reversed, i.e. period increasing towards the bottom. The default is \code{FALSE}. } + \item{\dots}{Arguments passed to \code{\link{rasterPlot}}. Only + relevant when the filled contours are drawn as a raster image. See + \code{\var{useRaster}}. + } } \details{ This produces a plot of a continuous wavelet transform and plots the Modified: pkg/dplR/man/xskel.ccf.plot.Rd =================================================================== --- pkg/dplR/man/xskel.ccf.plot.Rd 2015-12-18 13:30:18 UTC (rev 1008) +++ pkg/dplR/man/xskel.ccf.plot.Rd 2016-01-22 17:41:36 UTC (rev 1009) @@ -51,7 +51,7 @@ and second half of the time series using function \code{\link{ccf}} as \code{ccf(x=series,y=master,lag.max=5)}. - The plot is built using the \code{\link[grid]{Grid}} package which + The plot is built using the \link[grid]{Grid} package which allows for great flexibility in building complicated plots. However, these plots look best when they don\enc{?}{'}t cover too wide a range of years (unless the plotting device is wider than is typical). For Modified: pkg/dplR/man/xskel.plot.Rd =================================================================== --- pkg/dplR/man/xskel.plot.Rd 2015-12-18 13:30:18 UTC (rev 1008) +++ pkg/dplR/man/xskel.plot.Rd 2016-01-22 17:41:36 UTC (rev 1009) @@ -51,7 +51,7 @@ and second half of the time series using function \code{\link{ccf}} as \code{ccf(x=series,y=master,lag.max=5)}. - The plot is built using the \code{\link[grid]{Grid}} package which + The plot is built using the \link[grid]{Grid} package which allows for great flexibility in building complicated plots. However, these plots look best when they don\enc{?}{'}t cover too wide a range of years (unless the plotting device is wider than is typical). For