rsplitsegmentation-faultfatal-errorterra

terra::split | R session aborts when splitting polygon fatal error


When trying to split a polygon R runs into a fatal error and aborts. This happens when using R Studio and R on itself. I wrote the code on an earlier version of R on a different machine (also a mac), but have no access to it currently. This happened when I tried to run the code on my current machine. The code is part of a function, which worked fine on the other machine. I was not able to find a solution. Somewhere it was recommended to reinstall the packages and dependencies and to update R and R Studio to their latest version, which I did. But it did not help. The raster on which the vector data is based on is only about 60MB, should not be the problem, that it is too large.

library(landscapetools)
library(tidyverse)
library(raster)
library(terra)

raster <- rast("some_raster.tif")
raster <- util_classify(raster, n = 3, level_names = c(1, 2, 3))
polygons <- as.polygons(raster, dissolve = T)
polygon.split <- split(polygons,c(1, 2, 3))

This results in this:

    *** caught segfault ***
    address 0x0, cause 'invalid permissions'
    
    Traceback:
     1: .External(list(name = "CppMethod__invoke_notvoid", address = <pointer: 0x6000024030a0>,     dll = list(name = "Rcpp", path = "/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/Rcpp/libs/Rcpp.so",         dynamicLookup = TRUE, handle = <pointer: 0x2043f2000>,         info = <pointer: 0x600000091380>, forceSymbols = FALSE),     numParameters = -1L), <pointer: 0x60000189d900>, <pointer: 0x6000025e5660>,     .pointer, ...)
     2: x@ptr$split(f)
     3: methods::.hasSlot(x, "ptr")
     4: messages(x@ptr$split(f), "split")
     5: .local(x, f, ...)
     6: split(polygons, c(1, 2, 3))
     7: split(polygons, c(1, 2, 3))
    
    Possible actions:
    1: abort (with core dump, if enabled)
    2: normal R exit
    3: exit R without saving workspace
    4: exit R saving workspace

Here is also the session info:

    > sessionInfo()
    R version 4.4.1 (2024-06-14)
    Platform: aarch64-apple-darwin20
    Running under: macOS Monterey 12.0.1
    
    Matrix products: default
    BLAS:   /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib 
    LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.0
    
    locale:
    [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
    
    time zone: Europe/Zurich
    tzcode source: internal
    
    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods   base     
    
    other attached packages:
     [1] terra_1.7-78         raster_3.6-26        sp_2.1-4            
     [4] lubridate_1.9.3      forcats_1.0.0        stringr_1.5.1       
     [7] dplyr_1.1.4          purrr_1.0.2          readr_2.1.5         
    [10] tidyr_1.3.1          tibble_3.2.1         ggplot2_3.5.1       
    [13] tidyverse_2.0.0      landscapetools_0.5.0
    
    loaded via a namespace (and not attached):
     [1] gtable_0.3.5     compiler_4.4.1   tidyselect_1.2.1 Rcpp_1.0.12     
     [5] scales_1.3.0     lattice_0.22-6   R6_2.5.1         generics_0.1.3  
     [9] munsell_0.5.1    pillar_1.9.0     tzdb_0.4.0       rlang_1.1.4     
    [13] utf8_1.2.4       stringi_1.8.4    timechange_0.3.0 cli_3.6.3       
    [17] withr_3.0.0      magrittr_2.0.3   grid_4.4.1       hms_1.1.3       
    [21] lifecycle_1.0.4  vctrs_0.6.5      glue_1.7.0       codetools_0.2-20
    [25] fansi_1.0.6      colorspace_2.1-0 tools_4.4.1      pkgconfig_2.0.3 
    > 

As suggested by Chris I wrote a new script using terra only. I try to make it reproducible. split still causes session abort.

library(tidyverse)
library(terra)
library(BAMMtools)
raster <- rast(system.file("ex/elev.tif", package="terra"))
rvals <- values(raster, mat=F, dataframe=F, na.rm=T)
jenks <- getJenksBreaks(rvals, 4)
m <- c(jenks[1], jenks[2],1,
       jenks[2], jenks[3], 2,
       jenks[3], jenks[4], 3)
rclmat <- matrix(m, ncol=3, byrow=TRUE)
rc1 <- classify(raster, rclmat, include.lowest=TRUE)
jenks.poly <- as.polygons(rc1, dissolve = T)
polygon.split <- split(jenks.poly,c(1, 2, 3))

here the error message:

 *** caught segfault ***
address 0x0, cause 'invalid permissions'

Traceback:
 1: .External(list(name = "CppMethod__invoke_notvoid", address = <pointer: 0x600001f056c0>,     dll = list(name = "Rcpp", path = "/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/Rcpp/libs/Rcpp.so",         dynamicLookup = TRUE, handle = <pointer: 0x204d9a520>,         info = <pointer: 0x600003b943c0>, forceSymbols = FALSE),     numParameters = -1L), <pointer: 0x6000023ab600>, <pointer: 0x600001efb000>,     .pointer, ...)
 2: x@ptr$split(f)
 3: methods::.hasSlot(x, "ptr")
 4: messages(x@ptr$split(f), "split")
 5: .local(x, f, ...)
 6: split(jenks.poly, c(1, 2, 3))
 7: split(jenks.poly, c(1, 2, 3))

Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace

Solution

  • This is clearly a bug (R should never crash), and these are best reported where the software is developed (e.g., github).

    In fact, this issue was reported before and fixed in the development version. The github site has instructions to install the development version:

    install.packages('terra', repos='https://rspatial.r-universe.dev') 
    

    With that version the example works for me:

    library(terra)
    #terra 1.7.80
    
    r <- rast(system.file("ex/elev.tif", package="terra"))
    rclmat <- matrix(c(141, 306, 403, 306, 403, 547, 1, 2, 3), ncol=3)
    rc1 <- classify(r, rclmat, include.lowest=TRUE)
    p <- as.polygons(rc1, dissolve = T)
    
    # I would do this
    s1 <- split(p, "elevation")
    # but you can do
    s2 <- split(p, 1:3)
    
    str(s1)
    #List of 3
    # $ 1:S4 class 'SpatVector' [package "terra"]
    # $ 2:S4 class 'SpatVector' [package "terra"]
    # $ 3:S4 class 'SpatVector' [package "terra"]