[R-pkg-devel] Package broke with R 4.3.0

Iñaki Ucar |uc@r @end|ng |rom |edor@project@org
Tue Jun 27 16:50:17 CEST 2023


On Tue, 27 Jun 2023 at 16:31, <arilamstein using gmail.com> wrote:
>
> It appears that my R package choroplethr broke due to this change in R
> 4.3.0:
>
> CHANGES IN R 4.3.0:
>
> SIGNIFICANT USER-VISIBLE CHANGES:
>
> Calling && or || with LHS or (if evaluated) RHS of length greater than one
> is now always an error, with a report of the form
>
> 'length = 4' in coercion to 'logical(1)'
> Environment variable R_CHECK_LENGTH_1_LOGIC2 no longer has any effect.
>
> The specific line which broke is this:
> https://github.com/arilamstein/choroplethr/blob/master/R/usa.R#L24
>
> The bug can be reproduced like this:
>
> zoom = c("arizona", "arkansas", "louisiana", "minnesota", "mississippi",
>       "montana", "new mexico", "north dakota", "oklahoma", "pennsylvania",
>       "tennessee", "virginia", "california", "delaware", "west virginia",
>       "wisconsin", "wyoming", "alabama", "alaska", "florida", "idaho",
>       "kansas", "maryland", "colorado", "new jersey", "north carolina",
>       "south carolina", "washington", "vermont", "utah", "iowa",
> "kentucky",
>       "maine", "massachusetts", "connecticut", "michigan", "missouri",
>       "nebraska", "nevada", "new hampshire", "new york", "ohio", "oregon",
>       "rhode island", "south dakota", "district of columbia", "texas",
>       "georgia", "hawaii", "illinois", "indiana")
>
> if (zoom == "alaska" || zoom == "hawaii") {}
> Error in zoom == "alaska" || zoom == "hawaii" :
>   'length = 51' in coercion to 'logical(1)'
>
> I have two questions:
>
> 1. Can someone explain why this change was introduced to the language?

This was incorrect code before, and should be fixed; this didn't
change. The change is that now it always triggers an error.

> 2. Can someone tell me if there is a preferred, idiomatic way to rewrite my
> code?

Option 1:
if (identical(zoom, "alaska") || identical(zoom, "hawaii")) {}

Option 2:
if (isTRUE(zoom == "alaska") || isTRUE(zoom == "hawaii")) {}

> I came up with two solutions that work. The first checks whether zoom is of
> length 1:
>
> if ((length(zoom) == 1) && (zoom %in% c("alaska", "hawaii"))) { }

This works too.

> The second keeps the vector recycling, but then uses all to coerce the
> vector to be a single value. In retrospect, I think I likely assumed that
> this is what R < 4.3.0 was doing when the code worked. (But I wrote this
> code many years ago, so I can't be sure!):

Before R 4.3.0, there was a warning, and the string was compared with
the first value only.

Iñaki


> if (all(zoom == "alaska") || all(zoom == "hawaii")) {}
>
> Thanks in advance.
>
> Ari
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-package-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel



-- 
Iñaki Úcar



More information about the R-package-devel mailing list