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

Possible conflict sf and tmap #2043

Closed
donojazz opened this issue Nov 18, 2022 · 6 comments
Closed

Possible conflict sf and tmap #2043

donojazz opened this issue Nov 18, 2022 · 6 comments

Comments

@donojazz
Copy link

donojazz commented Nov 18, 2022

I love tmap and have been using it constantly for the 30 Day Map Challenge. It has been working great until yesterday on code that ran great 2 days ago, I now get:

Error: Shape contains invalid polygons. Please fix it or set tmap_options(check.and.fix = TRUE) and rerun the plot

constantly. After playing with marmap and having it hose my R environmant, especially tidyverse. I had to delete my entire R environment along with libraries. I worked with Jenny Bryan and she helped with the tidyverse issue tidyverse/tidyverse#316 (comment) We agreed about the fresh start with R. But, now, I am getting errors with tmap. I am getting the same errors off your sample vignettes (tmap: version changes). Can you assist? Thanks. Jim

Here is my sys info.

sessionInfo()

R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8
[3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8

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

other attached packages:
[1] DT_0.26 mapview_2.11.0 tigris_1.6.1 spdplyr_0.4.0 readxl_1.4.1
[6] rgdal_1.6-2 sp_1.5-1 sf_1.0-9 tmaptools_3.1-1 tmap_3.3-3
[11] leaflet_2.1.1 forcats_0.5.2 stringr_1.4.1 dplyr_1.0.10 purrr_0.3.5
[16] readr_2.1.3 tidyr_1.2.1 tibble_3.1.8 ggplot2_3.4.0 tidyverse_1.3.2

loaded via a namespace (and not attached):
[1] googledrive_2.0.0 leafem_0.2.0 colorspace_2.0-3
[4] ellipsis_0.3.2 class_7.3-20 satellite_1.0.4
[7] base64enc_0.1-3 fs_1.5.2 dichromat_2.0-0.1
[10] rstudioapi_0.14 proxy_0.4-27 bit64_4.0.5
[13] fansi_1.0.3 lubridate_1.9.0 xml2_1.3.3
[16] codetools_0.2-18 cachem_1.0.6 knitr_1.40
[19] jsonlite_1.8.3 broom_1.0.1 dbplyr_2.2.1
[22] png_0.1-7 compiler_4.2.2 httr_1.4.4
[25] backports_1.4.1 assertthat_0.2.1 fastmap_1.1.0
[28] gargle_1.2.1 cli_3.4.1 leaflet.providers_1.9.0
[31] htmltools_0.5.3 tools_4.2.2 gtable_0.3.1
[34] glue_1.6.2 rappdirs_0.3.3 Rcpp_1.0.9
[37] jquerylib_0.1.4 cellranger_1.1.0 raster_3.6-3
[40] vctrs_0.5.0 leafsync_0.1.0 crosstalk_1.2.0
[43] lwgeom_0.2-9 xfun_0.34 rvest_1.0.3
[46] timechange_0.1.1 lifecycle_1.0.3 XML_3.99-0.12
[49] googlesheets4_1.0.1 terra_1.6-17 scales_1.2.1
[52] vroom_1.6.0 hms_1.1.2 parallel_4.2.2
[55] RColorBrewer_1.1-3 curl_4.3.3 yaml_2.3.6
[58] sass_0.4.2 stringi_1.7.8 maptools_1.1-5
[61] e1071_1.7-12 rlang_1.0.6 pkgconfig_2.0.3
[64] spbabel_0.5.5 evaluate_0.18 lattice_0.20-45
[67] htmlwidgets_1.5.4 bit_4.0.4 tidyselect_1.2.0
[70] magrittr_2.0.3 R6_2.5.1 generics_0.1.3
[73] DBI_1.1.3 pillar_1.8.1 haven_2.5.1
[76] foreign_0.8-83 withr_2.5.0 units_0.8-0
[79] stars_0.5-6 abind_1.4-5 modelr_0.1.10
[82] crayon_1.5.2 uuid_1.1-0 KernSmooth_2.23-20
[85] utf8_1.2.2 tzdb_0.3.0 rmarkdown_2.18
[88] grid_4.2.2 reprex_2.0.2 digest_0.6.30
[91] classInt_0.4-8 webshot_0.5.4 stats4_4.2.2
[94] munsell_0.5.0 viridisLite_0.4.1 bslib_0.4.1

Originally posted by @donojazz in #1649 (comment)

@donojazz
Copy link
Author

donojazz commented Nov 18, 2022

Not sure if related to my tmap issue. I found something on sf and tested with their code demonstrations using their
sf::basic |   | simple feature test script Could newest versions of R and RStudio be creating sf issues that affect tmap? BTW, plot seems to work fine on those polygon objects.

http://127.0.0.1:31629/doc/html/Search?package=sf&agrep=0&types.demo=1&pattern=

Test:
(p1 = st_point(c(1,2)))

Return:
POINT (1 2)

Test:
hole1 = matrix(c(1,1,1,2,2,2,2,1,1,1),ncol=2, byrow=TRUE)
hole2 = matrix(c(5,5,5,6,6,6,6,5,5,5),ncol=2, byrow=TRUE)
pts = list(outer, hole1, hole2)
(pl1 = st_polygon(pts))

Retturn:
Error in FUN(X[[i]], ...) : is.numeric(x) is not TRUE

Test:
pts3 = lapply(pts, function(x) cbind(x, 0))
(pl2 = st_polygon(pts3))

Return
Error in FUN(X[[i]], ...) : is.numeric(x) is not TRUE
sf::basic simple feature test script

@mtennekes
Copy link
Contributor

Could you share some reproducible code @donojazz ?

Have you tried to apply st_make_validon your sf object, and feed the returned object to tmap?

@donojazz
Copy link
Author

Here it is, I tried adding in this code but it didn't help : tmap_options(check.and.fix = TRUE)

Not sure why the problems. Initial problem was tidyverse will not load. Once I deleted old versions, and files, then reloaded R and RStudio and libraries tidyverse worked but my older (2 day old) containing tmap would not. The sample below was from your tmap documentation.

I will try your suggestion. Thanks.

library(tigris)
#> To enable caching of data, set `options(tigris_use_cache = TRUE)`
#> in your R script or .Rprofile.
library(sf)
#> Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tmap)
data(World, metro)
tmap_mode("view")
#> tmap mode set to interactive viewing
tm_basemap(leaflet::providers$CartoDB.PositronNoLabels, group = "CartoDB basemap") +
  tm_shape(World) +
  tm_polygons("HPI", group = "Countries") +
  tm_tiles(leaflet::providers$CartoDB.PositronOnlyLabels, group = "CartoDB labels") +
  tm_shape(metro) +
  tm_dots(col = "red", group = "Metropolitan areas")
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> Error: Shape contains invalid polygons. Please fix it or set tmap_options(check.and.fix = TRUE) and rerun the plot

Created on 2022-11-18 with reprex v2.0.2

@edzer
Copy link
Member

edzer commented Nov 18, 2022

Works fine for me, with

> sessionInfo()
R version 4.2.2 Patched (2022-11-10 r83330)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.1 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] tmap_3.3-3   dplyr_1.0.9  sf_1.0-10    tigris_1.6.1

loaded via a namespace (and not attached):
 [1] terra_1.6-40            tidyselect_1.2.0        lattice_0.20-45        
 [4] leaflet.providers_1.9.0 vctrs_0.5.1             generics_0.1.3         
 [7] htmltools_0.5.3         stars_0.6-0             viridisLite_0.4.1      
[10] yaml_2.3.5              s2_1.1.0                base64enc_0.1-3        
[13] utf8_1.2.2              XML_3.99-0.10           rlang_1.0.6            
[16] e1071_1.7-12            pillar_1.8.1            foreign_0.8-82         
[19] glue_1.6.2              DBI_1.1.3               rappdirs_0.3.3         
[22] sp_1.5-0                RColorBrewer_1.1-3      wk_0.7.0               
[25] uuid_1.1-0              lifecycle_1.0.3         stringr_1.4.0          
[28] raster_3.6-3            htmlwidgets_1.5.4       codetools_0.2-18       
[31] leafsync_0.1.0          fastmap_1.1.0           maptools_1.1-4         
[34] crosstalk_1.2.0         parallel_4.2.2          class_7.3-20           
[37] fansi_1.0.3             leafem_0.2.0            Rcpp_1.0.9             
[40] KernSmooth_2.23-20      classInt_0.4-8          lwgeom_0.2-10          
[43] jsonlite_1.8.3          leaflet_2.1.1           abind_1.4-5            
[46] png_0.1-7               digest_0.6.30           stringi_1.7.8          
[49] tmaptools_3.1-1         grid_4.2.2              rgdal_1.5-32           
[52] cli_3.4.0.9000          tools_4.2.2             magrittr_2.0.3         
[55] proxy_0.4-27            tibble_3.1.8            dichromat_2.0-0.1      
[58] pkgconfig_2.0.3         assertthat_0.2.1        httr_1.4.4             
[61] R6_2.5.1                units_0.8-0             compiler_4.2.2         

@donojazz
Copy link
Author

Any ideas where I should start digging? Think it may be a windows thing causing it. Jenny said to remove all the extraneous vctrs files. I got rid of 7 and only one left is the one I installed on 11/17.

@donojazz
Copy link
Author

I kept searching around for ideas and someone here suggested I make sure geos is installed. I tryied installing from R Studio and it failed. Then I installed directly from console directly. These files finally loaded:

package ‘libgeos’ successfully unpacked and MD5 sums checked
package ‘wk’ successfully unpacked and MD5 sums checked
package ‘geos’ successfully unpacked and MD5 sums checked

Part of the error message was:
Error in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]) :
there is no package called ‘wk’

Bottom line: tmap runs that reprex code I sent. I think the problem is fixed. Thanks.

Jim

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