[Dplr-commits] r1007 - in pkg/dplR: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Nov 13 15:31:25 CET 2015


Author: mvkorpel
Date: 2015-11-13 15:31:25 +0100 (Fri, 13 Nov 2015)
New Revision: 1007

Modified:
   pkg/dplR/ChangeLog
   pkg/dplR/DESCRIPTION
   pkg/dplR/R/rasterPlot.R
   pkg/dplR/man/rasterPlot.Rd
Log:
* Added option 'draw' (default TRUE) to rasterPlot(). If FALSE, does
  not draw the raster image but returns it as a "nativeRaster" object.

* Improved examples of rasterPlot(). Should now work even if device
  does not support transparency.


Modified: pkg/dplR/ChangeLog
===================================================================
--- pkg/dplR/ChangeLog	2015-11-08 22:57:30 UTC (rev 1006)
+++ pkg/dplR/ChangeLog	2015-11-13 14:31:25 UTC (rev 1007)
@@ -6,6 +6,8 @@
   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,
+  the default) or returned (FALSE)
 - png plot is initialized with plot.new() instead of the previous
   plot(type = "n", ...) arrangement.
 

Modified: pkg/dplR/DESCRIPTION
===================================================================
--- pkg/dplR/DESCRIPTION	2015-11-08 22:57:30 UTC (rev 1006)
+++ pkg/dplR/DESCRIPTION	2015-11-13 14:31:25 UTC (rev 1007)
@@ -3,7 +3,7 @@
 Type: Package
 Title: Dendrochronology Program Library in R
 Version: 1.6.4
-Date: 2015-11-09
+Date: 2015-11-13
 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",

Modified: pkg/dplR/R/rasterPlot.R
===================================================================
--- pkg/dplR/R/rasterPlot.R	2015-11-08 22:57:30 UTC (rev 1006)
+++ pkg/dplR/R/rasterPlot.R	2015-11-13 14:31:25 UTC (rev 1007)
@@ -1,10 +1,13 @@
 rasterPlot <- function(expr, res = 150, region=c("plot", "figure"), antialias,
-                       bg = "transparent", interpolate = TRUE, ...) {
+                       bg = "transparent", interpolate = TRUE, draw = TRUE,
+                       ...) {
+    draw2 <- isTRUE(as.logical(draw)[1L])
     ## Plotting commands 'expr' will be evaluated in the environment
     ## of the caller of rasterPlot()
     pf <- parent.frame()
     fallback <- FALSE
-    if (identical(dev.capabilities("rasterImage")[["rasterImage"]], "no")) {
+    if (draw2 &&
+        identical(dev.capabilities("rasterImage")[["rasterImage"]], "no")) {
         message("device does not support raster images")
         fallback <- TRUE
     }
@@ -12,6 +15,9 @@
         message("png device unavailable")
         fallback <- TRUE
     }
+    if (fallback && !draw2) {
+        return(NULL)
+    }
     region2 <- match.arg(region)
     plotRegion <- region2 == "plot"
     ## Start new plot if one does not exist
@@ -121,6 +127,9 @@
     on.exit()
     ## Remove the temporary .png file
     unlink(fname)
+    if (!draw2) {
+        return(pngData)
+    }
     if (plotRegion || marzero) {
         ## Add a raster image to the plot region of the original plot
         rasterImage(pngData, xleft = usrLeft, ybottom = usrBottom,

Modified: pkg/dplR/man/rasterPlot.Rd
===================================================================
--- pkg/dplR/man/rasterPlot.Rd	2015-11-08 22:57:30 UTC (rev 1006)
+++ pkg/dplR/man/rasterPlot.Rd	2015-11-13 14:31:25 UTC (rev 1007)
@@ -12,7 +12,7 @@
 }
 \usage{
 rasterPlot(expr, res = 150, region = c("plot", "figure"), antialias,
-           bg = "transparent", interpolate = TRUE, \dots)
+           bg = "transparent", interpolate = TRUE, draw = TRUE, \dots)
 }
 \arguments{
   \item{expr}{
@@ -53,6 +53,10 @@
     Analogously to \code{\var{antialias}}, \code{FALSE} is preferred
     when color maps to value.
   }
+  \item{draw}{
+    A \code{logical} flag.  Draw the results (\code{TRUE}, the default)
+    or return an image object (\code{FALSE})?
+  }
   \item{\dots}{
     Other arguments to \code{\link{png}}.
   }
@@ -75,7 +79,14 @@
   symbols maintain their size when the device is resized.
 }
 \value{
-  No return value.  Used for the side effects.
+  If \code{\var{draw}} is \code{TRUE}, there is no return value.  The
+  function is used for the side effects.
+
+  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
+  (see \sQuote{Note}), \code{NULL} is returned.
 }
 \author{
   Mikko Korpela
@@ -138,25 +149,32 @@
 axis(2)
 
 ## The same picture with higher resolution but no antialiasing
-plot(x, y, type = "n", axes = FALSE, ylab = ylab)
-## region = "figure" makes no difference here
+plot(y, type = "n", axes = FALSE, ann = FALSE)
+## No content in margin, but region = "figure" and bg = "white"
+## paints margin white
 rasterPlot(expr, antialias = "none", interpolate = FALSE,
-           region = "figure")
-box()
-axis(1)
-axis(2)
+           region = "figure", bg = "white")
+## Draw box, axes, labels
+parnew <- par(new = TRUE)
+plot(x, y, type = "n", ylab = ylab)
+par(parnew)
 
-## A part of plot(1:5) is drawn normally, the rest with rasterPlot.
-## Resize to see stretching.
+## Draw plot(1:5) with adjusted margins and additional axes.  Some parts
+## are drawn with rasterPlot, others normally.  Resize to see stretching.
 op <- par(no.readonly = TRUE)
 par(mar = c(5.1, 4.1, 2.1, 2.1))
-plot(c(1, 3, 5), c(1, 3, 5), axes = FALSE)
+plot(1:5, type = "n", axes = FALSE, ann = FALSE)
+expr2 <- quote({
+    points(c(2, 4), c(2, 4))
+    axis(2)
+    axis(3)
+})
+rasterPlot(expr2, region = "figure", bg = "white")
+points(c(1, 3, 5), c(1, 3, 5))
 box()
 axis(1)
 axis(4)
-rasterPlot(quote(points(c(2, 4), c(2, 4))), region = "figure")
-rasterPlot(quote(axis(2)), region = "figure")
-rasterPlot(quote(axis(3)), region = "figure")
+title(xlab = "Index", ylab = "1:5")
 par(op)
 }
 \keyword{ aplot }



More information about the Dplr-commits mailing list