Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving 'gp' out of LogicalVector args #29

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/grid-renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
);
}
Expand Down
25 changes: 25 additions & 0 deletions src/grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-grid-constructors.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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"
)
)
Expand Down
Loading