Here is how I spend more hours - sheer frustration, maybe it will be for use for others also.

Starting point: fix a bug in a package, write extensive tests, one particular test file  run successfully when run locally, but fails when running in parallel  (note: running in parallel is done a separate, new environment).  The error: "The R session crashed with exit code 2".

An internet search indicated that this exit code indicates a "suicide"  (assuming my employer watches my browser history, this might cause some eye brow raises...)

Rewriting the code did not help, the same failure.  Adding log lines after practically each line of code showed that the execution abruptly stops,  but still no obvious reason why. Then, this morning, the idea: disable running in parallel, just run it normally, it takes longer, but maybe we get an error message....  and voila, the error message is now there:

---- failure: length > 1 in coercion to logical

Still confused? Well, now it's easy. Here are the code lines that trigger the error:

`%>%` <- magrittr::`%>%`
df_invalid df %>% 
  dplyr::filter(x < x_min || x > x_max)

Shall I give you a few moments to spot the mistake?

Maybe this helps?

df <- data.frame(
  stringsAsFactors = FALSE,
  x = c(1, 5, 7, 10)
)
print(df)
#    x
# 1  1
# 2  5
# 3  7
# 4 10

df %>% 
  dplyr::filter(x < 5 || x > 7)
#    x
# 1  1
# 2  5
# 3  7
# 4 10

df %>% 
  dplyr::filter(x < 5 | x > 7)
#    x
# 1  1
# 2 10

R manual: "||" is the logical OR operator used for length-one logical vectors, "|" is the logical OR operator used to compare vectors with more than one element...

If one uses "||" for vectors with multiple elements, you don't get, by default,  any error or warnings, just possibly wrong results, as in the example above...

While testing in the package, the test failed, because there is an environment variable which can be set,  which triggers this:

Sys.setenv("_R_CHECK_LENGTH_1_LOGIC2_" = TRUE)
df %>% 
  dplyr::filter(x < 5 || x > 7)

# Error in `dplyr::filter()`:
#   ℹ In argument: `x < 5 || x > 7`.
# Caused by error in `x < 5 || x > 7`:
#   ! 'length(x) = 4 > 1' in coercion to 'logical(1)'
# Run `rlang::last_trace()` to see where the error occurred.

I insisted on writing the tests because we had multiple complaints due to a bug. It took me several hours, I was about to give up, and finally stumbled upon yet another bug...

I naively expected the error to be correctly shown also when running in parallel... in the end, we all do mistakes, luckily not all at the same time...

Take away: if working with R, you'd better set this environment variable:

Sys.setenv("_R_CHECK_LENGTH_1_LOGIC2_" = TRUE)

Or make sure you use a newer R version, since starting with R version 4.3.0, this is not an issue anymore, as described here.

One could argue why was this not set by default in the first place anyway... You noticed that annoying text like "now 75% less plastic" on products in the supermarket? Every time I see this I think: why on earth you did not produce them with less plastic from the beginning?...

Unfortunately I realized that the code with the bug went through my hands, I should have noticed the mistake... Let me think of a plausible excuse...

Meanwhile, I leave with a scene that filled my heart: One afternoon, going for a walk, a sudden rain, I hurried up home, on the way I hear screams of joy, I stop running and look around. In front of a house, three children, one girl and two boys, playing in a puddle....

https://sketchplanations.com/complaining-at-the-weather

complaining-at-the-weather

Make a promise. Show up. Do the work. Repeat.