I have a polygon grid (epsg:24381) from which I want to randomly select some polygons. Whenever I try to perform the sampling I'm getting the following error:

Error in h(simpleError(msg, call)): error in evaluating the argument 'obj' in selecting a method for function 'coordinates': cannot allocate vector of size 5 Kb Error during wrapup: cannot allocate vector of size 1.7 Mb Error: no more error handlers available (recursive errors?); invoking 'abort' restart.

Here is the code and the package I'm utilizing is spatialEco, R v4.4.1, RStudio v1.4.1717, Windows 10.

shp <- ("xxx/grid.shp") %>%

#convert shp to spatialpolygondataframe
spdf <- as_Spatial(shp)

#random polygon selection from a grid polygon
random_poly = sample.poly(spdf, n = 300, type = "random")

What might cause the problem?

  • That's a weird one, can you post your data up somewhere so I can figure out what may be going on? Aug 11, 2021 at 19:59
  • easyupload.io/xfv7d7 password: covid21
    – Nikos
    Aug 11, 2021 at 21:01

2 Answers 2


I cannot replicate this error so, I imagine, as the error indicates, you are actually running out of memory. Besides reading in the grid, with 229,374 polygons you are trying to create 68,812,200 sample points. A few things to check are how much RAM you have and if you are running the 64-bit version of R (within RStudio). I would note that a computer with even a relatively small amount of RAM (4GB) should be able to hand this problem leading me to think that you are running 32-bit R or having RAM allocated elsewhere (another process).

Here is the code that I used and it is running fine with R 4.1.0 x86_64-w64-mingw32/x64, sf_1.0-2, sp_1.4-5 and spatialEco_1.3-7.


shp <- as(sf::st_read("C:/test/grid.shp"), "Spatial")
random_poly = sample.poly(shp, n = 300, type = "random")

You can proof the code by reducing the size of your problem.

( random_poly = sample.poly(shp[sample(1:nrow(shp), 10),], 
                            n = 10, type = "random") ) 

This opens the door to subsampling your problem down. In a loop, you can grab a few thousand polygons at a time, create a sample and write them out. The sf::st_write function has an append argument that will allow you to add to iteratively append a shapefile on disk. Something along these lines should work, will take awhile but control memory usage.

( n=round(nrow(shp) /20, 0) )
g <- split(1:nrow(shp), ceiling(seq_along(1:nrow(shp)) / n))

st_write(as(sample.poly(shp[g[[1]],], n = 300, type = "random"),
         "sf"), "sample_pts.shp")  

lapply(g[-1], function(x) {  
  st_write(as(sample.poly(shp[x,], n = 300, 
           type = "random"), "sf"),
           append=TRUE) })
  • Dear Jeffrey, first of all you were right about the R version on my laptop. I had the 32bit installed. So I re-installed it and I dl the R 4.1.1 x86_64-w64-mingw32/x64. Unfortunately the same error persists despite the control memory usage you told me to try. I will try and tune the parameters and I'll come back again with an answer.
    – Nikos
    Aug 12, 2021 at 9:25
  • 1
    @niktziok by default both versions are installed. If you are using RStudio, you have to configure the software to aim at the correct version. I would highly recommend trying to run this problem in R. I have had numerous experiences with RStudio not playing well with sp class objects, with very strange behavior. It seems to be better with more recent versions but, I still expect occasional issues to arise. When running into issues, always revert to R to test solutions thus, taking an additional software out of the equation. Make sure that you are launching R x64. Aug 12, 2021 at 14:23

I can not access the file you made available (network issues). But maybe the following helps - selecting the polygons in sf before converting to SPDF

library (sf)
library (sp)
library (dplyr)

sourceFile <- 'grid.shp'
sampleSize <- 300

#read in the source file
dt <- st_read (sourceFile)

# generate a sample of size 'sampleSize' from the row numbers of the polygons in the grid
s <- sample(c(1:nrow (dt)), sampleSize)

# copy over the polygons into your sample and convert to SPDF
random_poly <- dt[s,] %>% as_Spatial()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.