geocompx / geocompr

Geocomputation with R: an open source book
https://r.geocompx.org/
Other
1.59k stars 584 forks source link

tmap-related build fail #1103

Closed Robinlovelace closed 5 months ago

Robinlovelace commented 5 months ago

The latest version of tmap seems to be causing an error message:

 134/176 [raster-intro-plot]       
Error in `!legs_aes$legend[[k]]$active`:
! invalid argument type
Backtrace:
  1. base::source(...)
  3. tmap:::print.tmap_arrange(yy$value)
  4. tmap:::print_tmap_arrange(x, knit = knit, ..., options = options)
  6. tmap:::print.tmap(tm, vp = viewport(layout.pos.col = nc, layout.pos.row = nr))
  7. tmap:::step4_plot(...)
     ...
 11. tmap (local) FUN(X[[i]], ...)
 13. base::lapply(...)
 14. tmap (local) FUN(X[[i]], ...)
 15. base::mapply(...)
 16. tmap (local) `<fn>`(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]])

Quitting from lines 888-889 [raster-intro-plot] (02-spatial-data.Rmd)
Execution halted
Error in Rscript_render(f, render_args, render_meta, add1, add2) : 
  Failed to compile 02-spatial-data.Rmd
Calls: <Anonymous> -> render_new_session -> Rscript_render

See https://github.com/geocompx/geocompr/actions/runs/9475795989/job/26107667743#step:4:786 for the log and below for the bit of code. Can you try to reproduce @mtennekes and suggest work-around or upstream fix?

Thanks, happy providers of an unofficial tmap test suite : )

Robinlovelace commented 5 months ago

Failing lines:

https://github.com/geocompx/geocompr/blob/67219a0affea4fdc559bc424837c945c822559b0/code/02-raster-intro-plot.R#L1-L34

Robinlovelace commented 5 months ago

Cannot reproduce on my local version of tmap and I think the below reprex is the desired result. Can you reproduce Martijn/anyone?

# first intro plot -----------------------------------------------------------
 library(terra)
#> terra 1.7.71
 library(sf)
#> Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
 library(tmap)
#>
#> Attaching package: 'tmap'
#> The following object is masked from 'package:datasets':
#>
#>     rivers
 library(spData)
 set.seed(2021-09-09)
 small_ras = rast(matrix(1:16, 4, 4, byrow = TRUE))
 crs(small_ras) = "EPSG:4326"
 polys = st_as_sf(as.polygons(small_ras, na.rm = FALSE))
 polys$lyr.1 = as.character(polys$lyr.1)
 polys$vals = sample.int(100, 16)
 polys$vals[c(7, 9)] = "NA"
 suppressWarnings({polys$valsn = as.numeric(polys$vals)})

 tm1 = tm_shape(polys) +
   tm_borders(col = "black") +
   tm_text(text = "lyr.1") +
   tm_title("A. Cell IDs") +
   tm_layout(frame = FALSE)

 tm2 = tm_shape(polys) +
   tm_borders(col = "black") +
   tm_text(text = "vals")  +
   tm_title("B. Cell values") +
   tm_layout(frame = FALSE)

 tm3 = tm_shape(polys) +
   tm_fill(fill = "valsn",
           fill.scale = tm_scale(values = "RdBu", value.na = "white"),
           fill.legend = tm_legend(show = FALSE)) +
   tm_title("C. Colored values") +
   tm_layout(frame = FALSE)

 tmap_arrange(tm1, tm2, tm3, nrow = 1)
#> [cols4all] color palettes: use palettes from the R package cols4all. Run 'cols4all::c4a_gui()' to explore them. The old palette name "RdBu" is named "rd_bu" (in long format "brewer.rd_bu")

Created on 2024-06-12 with reprex v2.1.0

Nowosad commented 5 months ago

@Robinlovelace -- I can reproduce this error. I think it was introduced to tmap in the last few days. @mtennekes could you take a look at it? Simplified reprex is:

library(spData)
library(sf)
library(terra)
library(tmap)

set.seed(2021-09-09)
small_ras = rast(matrix(1:16, 4, 4, byrow = TRUE))
crs(small_ras) = "EPSG:4326"
polys = st_as_sf(as.polygons(small_ras, na.rm = FALSE))
polys$lyr.1 = as.character(polys$lyr.1)

tm1 = tm_shape(polys) +
  tm_borders(col = "black") +
  tm_text(text = "lyr.1") 
tm1
#> Error in !legs_aes$legend[[k]]$active: invalid argument type

Created on 2024-06-12 with reprex v2.1.0

Robinlovelace commented 5 months ago

Looks like a regression to me, good to know it can be reproduced locally @Nowosad, clearly a {tmap} version issue that has been picked up quick in this case.

Nowosad commented 5 months ago

Related: https://github.com/r-tmap/tmap/issues/888#event-13131015065

mtennekes commented 5 months ago

Working now