uclahs-cds / package-BoutrosLab-plotting-general

Functions to Create Publication-Quality Plots
https://uclahs-cds.github.io/package-BoutrosLab-plotting-general
12 stars 4 forks source link

Improve error message when a list containing empty vectors is passed to `legend.grob()` #167

Open tyamaguchi-ucla opened 1 year ago

tyamaguchi-ucla commented 1 year ago

when print(legend.empty) is

$levels
character(0)

$colours
character(0)

due to a user error,

legend.test<- list(
    legend = list(
        colours = legend.empty$colours,
        labels = legend.empty$levels,
        title = expression(bold(underline('Test'))),
        lwd = 0.5
        ),
     legend = list(...   
        );

bitmap.type = getOption('bitmapType')
if (capabilities('cairo')) {
    bitmap.type <- 'cairo';
    }

# create a legend grob using defaults
legend.grob1 <- legend.grob(
    legends = legend.test
    );

tiff(
    filename = "test.png",
    type = bitmap.type,
    width = 2,
    height = 10,
    units = 'in',
    res = 800,
    compression = 'lzw'
    );
grid.draw(legend.grob1);

will fail. The getCharCE error message is not helpful to identify the issue. Maybe consider making the warning an error here?

Warning messages:
1: In FUN(X[[i]], ...) : 'x' is NULL so the result will be NULL
2: In FUN(X[[i]], ...) : 'x' is NULL so the result will be NULL
3: In FUN(X[[i]], ...) : 'x' is NULL so the result will be NULL
4: In FUN(X[[i]], ...) : 'x' is NULL so the result will be NULL
5: In FUN(X[[i]], ...) : 'x' is NULL so the result will be NULL
6: In FUN(X[[i]], ...) : 'x' is NULL so the result will be NULL
7: In FUN(X[[i]], ...) : 'x' is NULL so the result will be NULL
8: In FUN(X[[i]], ...) : 'x' is NULL so the result will be NULL
Error in grid.Call.graphics(C_setviewport, vp, TRUE) :
  'getCharCE' must be called on a CHARSXP
Calls: grid.draw ... lapply -> FUN -> push.vp.viewport -> grid.Call.graphics
Execution halted