If(zoom %in% c(“alaska”,  “hawaii”)…

Göran

> 27 juni 2023 kl. 16:32 skrev arilamst...@gmail.com:
> 
> 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?
> 2. Can someone tell me if there is a preferred, idiomatic way to rewrite my
> code?
> 
> 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"))) { }
> 
> 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!):
> 
> if (all(zoom == "alaska") || all(zoom == "hawaii")) {}
> 
> Thanks in advance.
> 
> Ari
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-package-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel

______________________________________________
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel

Reply via email to