R-CoderDotCom / calendR

Ready to print calendars with ggplot2
https://r-coder.com/calendar-plot-r/
MIT License
255 stars 35 forks source link

Unexpected error - conditional in if statement has length gt 1 #30

Open SaintRod opened 1 year ago

SaintRod commented 1 year ago

Hello. The code is from the READ.me. I've encountered an error and was able to reproduce the issue using an example provided in the documentation. The issue is when a value is provided to the year, from, or to parameters calendR errors out.

I think the issue is a result of changes in R versions. Later versions now result in an error when the condition within an if() statement has a length gt 1.

A possible solution could be to perform a multi-stage conditional check, such as:

  1. check if special.days is of length one
  2. then, if ^ is true, check if special.days is equal to weekend

I added tolower(special.days) for robustness

if(length(special.days) == 1 && tolower(special.days) != "weekend") ...

Error Message

Error in if (special.days != "weekend") { : the condition has length > 1

Reproducible Example

# Vector of NA which length is the number of days of the year or month
myfills <- rep(NA, 365)
# myfills <- rep(NA, 366) # For leap years

# Add the events to the desired days
myfills[c(1:4, 50, 300:315)] <- "Holidays"
myfills[16] <- "Birthday"

# works
calendR::calendR(
  special.days = myfills,
  special.col = 2:3,     # Add as many colors as events
  legend.pos = "right"  # Add a legend if desired
)

# fails
calendR::calendR(
  year = "2024",
  special.days = myfills,
  special.col = 2:3,     # Add as many colors as events
  legend.pos = "right"  # Add a legend if desired
)

Session Info

R version 4.3.1 (2023-06-16)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Linux Mint 21.2

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0 
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: America/Chicago
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

loaded via a namespace (and not attached):
 [1] gtable_0.3.4       jsonlite_1.8.7     dplyr_1.1.3        compiler_4.3.1     renv_1.0.3         tidyselect_1.2.0  
 [7] Rcpp_1.0.11        gridGraphics_0.5-1 magick_2.8.0       ggimage_0.3.3      ggplotify_0.1.2    ggfun_0.1.3       
[13] scales_1.2.1       fastmap_1.1.1      ggplot2_3.4.3      R6_2.5.1           generics_0.1.3     yulab.utils_0.1.0 
[19] gggibbous_0.1.1    forcats_1.0.0      tibble_3.2.1       munsell_0.5.0      lubridate_1.9.2    calendR_1.2       
[25] pillar_1.9.0       rlang_1.1.1        utf8_1.2.3         cachem_1.0.8       fs_1.6.3           timechange_0.2.0  
[31] memoise_2.0.1      cli_3.6.1          withr_2.5.0        magrittr_2.0.3     digest_0.6.33      grid_4.3.1        
[37] lifecycle_1.0.3    vctrs_0.6.3        data.table_1.14.8  glue_1.6.2         suncalc_0.5.1      fansi_1.0.4       
[43] colorspace_2.1-0   tools_4.3.1        pkgconfig_2.0.3