From 6f7d50dd886fc5f7be4a95bf4768eb0262af2157 Mon Sep 17 00:00:00 2001 From: Brian Fox Date: Thu, 10 Nov 2022 13:59:51 -0800 Subject: [PATCH 1/4] Moving 'gp' out of LogicalVector args For bugs #22 and #7 and #11 --- src/grid-renderer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grid-renderer.h b/src/grid-renderer.h index 8216469..c41da4f 100644 --- a/src/grid-renderer.h +++ b/src/grid-renderer.h @@ -58,7 +58,7 @@ class GridRenderer { m_grobs.push_back( raster_grob( image, NumericVector(1, x), NumericVector(1, y), - NumericVector(1, width), NumericVector(1, height), LogicalVector(1, interpolate, gp) + NumericVector(1, width), NumericVector(1, height), LogicalVector(1, interpolate), gp ) ); } From 4273ef714c266de490b799faef2faac9c17b24ec Mon Sep 17 00:00:00 2001 From: Brian Fox Date: Thu, 10 Nov 2022 14:44:52 -0800 Subject: [PATCH 2/4] added check if gp.isNULL to raster_grob function --- src/grid.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/grid.cpp b/src/grid.cpp index bb176f0..24eea6c 100644 --- a/src/grid.cpp +++ b/src/grid.cpp @@ -61,6 +61,10 @@ List raster_grob(RObject image, NumericVector x_pt, NumericVector y_pt, NumericV stop("Function raster_grob() is not vectorized.\n"); } + if (gp.isNULL()) { + gp = gpar_empty(); + } + // need to produce a unique name for each grob, otherwise grid gets grumpy static int tg_count = 0; if (name.isNULL()) { From fa5c26a3278e2fc91761888de3c6a2d22598269c Mon Sep 17 00:00:00 2001 From: Brian Fox Date: Fri, 4 Aug 2023 09:03:36 -0700 Subject: [PATCH 3/4] added a check to raster_grob if gp param is an empty list to set it to gb_empty so test will work --- src/grid.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/grid.cpp b/src/grid.cpp index 24eea6c..0dce8e3 100644 --- a/src/grid.cpp +++ b/src/grid.cpp @@ -65,6 +65,27 @@ List raster_grob(RObject image, NumericVector x_pt, NumericVector y_pt, NumericV gp = gpar_empty(); } + /* + During the test in line 93 of test-grid-renderer.R: + "grid_renderer_raster(r, logo, 10, 10, width, height)" + that call will end up here; and when there is no "gp" param, then gp ends up + here as an empty list rather than NULL. But when this function returns an + empty R list in the "gp" element (line 114 below), then grid::grid.draw will + fail because it is expecting that gp element to be a gpar object instead. + So we need to set gp to gpar_empty(). + [I made this code to test for an empty list using chatgpt] + */ + if(TYPEOF(gp) == VECSXP) { + // Convert the variable to a list + List list_gp(gp); + // Check if the list is empty + if(list_gp.size() == 0) { + // uncomment this to see that grid_renderer_raster will send an empty list + // Rcout << "gp is an empty list\n"; + gp = gpar_empty(); + } + } + // need to produce a unique name for each grob, otherwise grid gets grumpy static int tg_count = 0; if (name.isNULL()) { From 2a8b94bc6b8b3794c4db07590a1f2c9a5dc0f117 Mon Sep 17 00:00:00 2001 From: Brian Fox Date: Fri, 4 Aug 2023 09:04:55 -0700 Subject: [PATCH 4/4] modified a test in test-grid-constructors.R to expect gpar() instead of NULL --- tests/testthat/test-grid-constructors.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testthat/test-grid-constructors.R b/tests/testthat/test-grid-constructors.R index 2d058bb..d805e4b 100644 --- a/tests/testthat/test-grid-constructors.R +++ b/tests/testthat/test-grid-constructors.R @@ -100,7 +100,7 @@ test_that("raster_grob", { ) ) - # interpolate is set as requested, gp default is NULL + # interpolate is set as requested, gp is set to gpar() if not provided expect_identical( raster_grob(image, 10, 20, 50, 40, interpolate = FALSE, name = "abc"), rasterGrob( @@ -109,7 +109,7 @@ test_that("raster_grob", { width = unit(50, "pt"), height = unit(40, "pt"), hjust = 0, vjust = 0, interpolate = FALSE, - gp = NULL, + gp = gpar(), name = "abc" ) )