From 4a2d0865f816da069c49caffbdf643c3e32f2b3d Mon Sep 17 00:00:00 2001 From: Paul Murrell Date: Fri, 30 Aug 2024 16:26:31 +1200 Subject: [PATCH] handle empty input to xyPathToList() et al; thanks to James Ward for th bug report --- DESCRIPTION | 2 +- R/grob.R | 8 ++++++++ inst/NEWS.Rd | 7 +++++++ tests/test.R | 12 ++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 9332157..a7654c1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: gridGeometry Type: Package Title: Polygon Geometry in 'grid' -Version: 0.4-0 +Version: 0.4-1 Authors@R: c(person("Paul", "Murrell", role = c("aut", "cre"), email = "paul@stat.auckland.ac.nz"), person("Jack", "Wong", role = "aut")) diff --git a/R/grob.R b/R/grob.R index 486b47e..579f563 100644 --- a/R/grob.R +++ b/R/grob.R @@ -4,6 +4,10 @@ ## Convert (closed) 'polyclip' polygon result to 'grid' path xyListPath <- function(x, rule, name=NULL, gp=gpar()) { + ## Handle empty 'x' + if (length(x) == 0) { + return(nullGrob(name=name)) + } if (missing(rule)) { if (is.null(attr(x, "rule"))) rule <- "winding" @@ -28,6 +32,10 @@ xyListToPath <- xyListPath ## Convert (closed) 'polyclip' polygon result to 'grid' polygons xyListPolygon <- function(x, name=NULL, gp=gpar()) { + ## Handle empty 'x' + if (length(x) == 0) { + return(nullGrob(name=name)) + } ## Remove any coordinate sets that are too short x <- x[sapply(x, function(c) length(c$x) > 1)] if (length(x) == 0) { diff --git a/inst/NEWS.Rd b/inst/NEWS.Rd index 50e68da..5442540 100755 --- a/inst/NEWS.Rd +++ b/inst/NEWS.Rd @@ -2,6 +2,13 @@ \title{NEWS file for the gridGeometry package} \encoding{UTF-8} +\section{Changes in version 0.4-1}{ + \itemize{ + \item Handle empty list or \code{NULL} input in + \code{xyListToPolygon()} and \code{xyListToPath()}. + } +} + \section{Changes in version 0.4-0}{ \itemize{ \item New functions \code{grid.polyoffset()} and diff --git a/tests/test.R b/tests/test.R index 0314306..996e3d4 100644 --- a/tests/test.R +++ b/tests/test.R @@ -217,3 +217,15 @@ gt <- gTree(children=gList(path, rect)) grid.newpage() grid.reduce(gt, gp=gpar(fill="grey")) +################################################################################ +## xyListTo*() + +## Check can handle empty list + +xyListToLine(NULL) +xyListToPolygon(NULL) +xyListToPath(NULL) + +xyListToLine(list()) +xyListToPolygon(list()) +xyListToPath(list())