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 ) ); } diff --git a/src/grid.cpp b/src/grid.cpp index bb176f0..0dce8e3 100644 --- a/src/grid.cpp +++ b/src/grid.cpp @@ -61,6 +61,31 @@ 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(); + } + + /* + 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()) { 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" ) )