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

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

Open
SaintRod opened this issue Oct 10, 2023 · 8 comments
Open

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

SaintRod opened this issue Oct 10, 2023 · 8 comments

Comments

@SaintRod
Copy link

SaintRod commented Oct 10, 2023

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   
@SaintRod SaintRod changed the title Unexpected error Unexpected error - conditional in if statement has length gt 1 Oct 10, 2023
@parkerbailey
Copy link

I am also seeing this error. The reproducible example given by @SaintRod is directly from the README.

This happens anytime I attempt to pass a list to the special.days argument that has a length greater than 1. The only workaround I've found is to create the events variable like so,

events <- c(126:129, 145, 217)

calendR(
   special.days = events,
   special.col = "red"
)

This unfortunately forfeits the users ability to have separate coloration for different events and have named events, so I would consider that an unacceptable workaround.

@mschilli87
Copy link

@parkerbailey: Can you reproduce this error with calendRio?

I tried @SaintRod's example and got the following result:
image

If you encounter issues with calendRio, please let me know. I'll happily fix things there and provide the solution as a PR here, if there is interest.

@parkerbailey
Copy link

@mschilli87 Yes, I can replicate this error with CalendRio.

I did some more digging, and it has something to do with how if handles arrays. According to this post, if the if statement that handles this error on line 393 is replaced with an ifelse, it may remedy this issue, or at least give us the next one down stream.

if(special.days != "weekend") {
        stop("special.days must be a numeric vector, a character vector of the length of the number of days of the year or month or 'weekend'")
      } else {
        wend <- FALSE
      }

could perhaps be changed to

ifelse(special.days != "weekend") {
        stop("special.days must be a numeric vector, a character vector of the length of the number of days of the year or month or 'weekend'"),
        wend <- FALSE
}

I'm not certain if this would work as I have no way to test it at the moment, but it's the best idea I've found so far.

@mschilli87
Copy link

Could you provide me with some copy/paste-able code that triggers this in calendRio, along with a full session info? Then I'll have a look and push a fix over the weekend. Once I can reproduce it, it should be easy to fix.

@parkerbailey
Copy link

Example code:

events <- rep(NA, 365)

events[222:226] <- "Event 1"
events[245] <- "Event 2"

calendR(
  special.days = events,
  special.col = c("pink", "lightblue")
)

Session info:

R version 4.4.1 (2024-06-14 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 10 x64 (build 19045)

Matrix products: default


locale:
[1] LC_COLLATE=English_United States.utf8  LC_CTYPE=English_United States.utf8    LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                           LC_TIME=English_United States.utf8    
system code page: 65001

time zone: America/New_York
tzcode source: internal

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

other attached packages:
 [1] calendRio_0.2.0  calendR_1.2      xts_0.14.1       zoo_1.8-12       dygraphs_1.1.1.6 scales_1.3.0     sf_1.0-19       
 [8] spData_2.3.3     tmap_3.99.9002   ggthemes_5.1.0   kableExtra_1.4.0 lubridate_1.9.3  forcats_1.0.0    stringr_1.5.1   
[15] dplyr_1.1.4      purrr_1.0.2      readr_2.1.5      tibble_3.2.1     tidyverse_2.0.0  tidyr_1.3.1      ggpubr_0.6.0    
[22] ggplot2_3.5.1    readxl_1.4.3    

loaded via a namespace (and not attached):
  [1] RColorBrewer_1.1-3      rstudioapi_0.17.1       jsonlite_1.8.9          wk_0.9.4                magrittr_2.0.3         
  [6] magick_2.8.5            farver_2.1.2            rmarkdown_2.29          fs_1.6.5                vctrs_0.6.5            
 [11] base64enc_0.1-3         terra_1.7-83            rstatix_0.7.2           htmltools_0.5.8.1       leafsync_0.1.0         
 [16] polynom_1.4-1           broom_1.0.7             raster_3.6-30           cellranger_1.1.0        s2_1.1.7               
 [21] gridGraphics_0.5-1      Formula_1.2-5           sass_0.4.9              KernSmooth_2.23-24      bslib_0.8.0            
 [26] htmlwidgets_1.6.4       stars_0.6-7             cachem_1.1.0            lifecycle_1.0.4         pkgconfig_2.0.3        
 [31] cols4all_0.8            Matrix_1.7-1            R6_2.5.1                fastmap_1.2.0           digest_0.6.37          
 [36] colorspace_2.1-1        leafem_0.2.3            pkgload_1.4.0           crosstalk_1.2.1         lwgeom_0.2-14          
 [41] labeling_0.4.3          spacesXYZ_1.3-0         fansi_1.0.6             timechange_0.3.0        abind_1.4-8            
 [46] mgcv_1.9-1              compiler_4.4.1          microbenchmark_1.5.0    proxy_0.4-27            bit64_4.5.2            
 [51] withr_3.0.2             backports_1.5.0         carData_3.0-5           DBI_1.2.3               ggsignif_0.6.4         
 [56] tmaptools_3.1-1         leaflet_2.2.2           classInt_0.4-10         tools_4.4.1             units_0.8-5            
 [61] leaflegend_1.2.1        glue_1.8.0              nlme_3.1-166            grid_4.4.1              rsconnect_1.3.3        
 [66] ggimage_0.3.3           generics_0.1.3          gggibbous_0.1.1         gtable_0.3.6            leaflet.providers_2.0.0
 [71] tzdb_0.4.0              class_7.3-22            data.table_1.16.2       hms_1.1.3               sp_2.1-4               
 [76] xml2_1.3.6              car_3.1-3               utf8_1.2.4              pillar_1.9.0            yulab.utils_0.1.8      
 [81] vroom_1.6.5             splines_4.4.1           lattice_0.22-6          bit_4.5.0               tidyselect_1.2.1       
 [86] knitr_1.49              suncalc_0.5.1           svglite_2.1.3           xfun_0.49               stringi_1.8.4          
 [91] ggfun_0.1.7             yaml_2.3.10             evaluate_1.0.1          codetools_0.2-20        ggplotify_0.1.2        
 [96] cli_3.6.3               systemfonts_1.1.0       munsell_0.5.1           jquerylib_0.1.4         dichromat_2.0-0.1      
[101] Rcpp_1.0.13-1           png_0.1-8               XML_3.99-0.17           parallel_4.4.1          viridisLite_0.4.2      
[106] e1071_1.7-16            crayon_1.5.3            rlang_1.1.4

@mschilli87
Copy link

Reproduced. Will work on a fix.

@mschilli87
Copy link

@parkerbailey:

I have a fix that 'works'.

However, your code would't work on this either because (as per the documentation and error message) 'special.days must be a numeric vector, a character vector of the length of the number of days of the year or month or 'weekend''.
In your case, events has a length of 365, but the default (i.e. current) year, 2024, is a leap year with 366 days.

The following would work with my fixed version:

calendR(
  year = 2025,
  special.days = events,
  special.col = c("pink", "lightblue")
)

image

As would the following:

events <- rep(NA, 366)

events[222:226] <- "Event 1"
events[245] <- "Event 2"

calendR(
  special.days = events,
  special.col = c("pink", "lightblue")
)

image

Note, however, that both of the above also work on the latest release of both, calendRio and calendR itself. The 'bug' only masks the 'correct' error message telling you why it doesn't work.

I'll release calendRio v 0.2.1 with the fix either way these days and will open a PR here as well, but you shouldn't need to wait for that to generate your calendar(s) as long as you provide the correct number of days for the year of interest.

Could you please test and confirm?

mschilli87 added a commit to mschilli87/calendR that referenced this issue Dec 21, 2024
Ensure `length(special.days) == 1L` before comparing to `"weekends"`.
This is requried for newer versions of R.

As far as I can tell, this does not limit any functionality, but masks
the informative error message when specifying the wrong number of days
(i.e. 365 for a leap year) with an uninformative one.

This commit fixes issue R-CoderDotCom#30.

Signed-off-by: Marcel Schilling <[email protected]>
mschilli87 added a commit to mschilli87/calendR that referenced this issue Dec 21, 2024
Ensure `length(special.days) == 1L` before comparing to `"weekends"`.
This is requried for newer versions of R.

As far as I can tell, this does not limit any functionality, but masks
the informative error message when specifying the wrong number of days
(i.e. 365 for a leap year) with an uninformative one.

This commit fixes issue R-CoderDotCom#30.

Signed-off-by: Marcel Schilling <[email protected]>
@parkerbailey
Copy link

Gah! It always comes down to a leap year or a timezone with these things. I appreciate the help, this fix works perfectly. Thanks much!
image

events_2024 <- rep(NA, 366) # 2024 is a leap year, thus 366 days

events_2024[122] <- "Event1"
events_2024[145] <- "Event2"

calendR(
  special.days = events_2024,
  special.col = c("pink", "lightblue")
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants