Creating large environment object - protect(): protection stack overflow

When I create an environment (intended as hash table) like this

set.seed(1)
key <- do.call(paste0, Map(stringi::stri_rand_strings, n=2e5, length = 16)) # error on n > 49991
val <- sample.int(750, size = 2e5, replace = T)

make_dict <- function(keys, values){
  require(rlang)
  e <- new.env(size = length(keys))
  l <- list2(!!!setNames(values, keys)) 
  list2env(l, envir = e, hash = T)
}

d <- make_dict(key, val)

.rs.describeObject throws an error protect(): protection stack overflow when it tries to generate a summary in the environments pane for an environment d that contains more than 50 000 key value pairs. When I grab some values like this

`%||%` <- function(x,y) if(is.null(x)) y else x
grab <- function(...){
  vector("integer", length(..2)) |>
    (\(.){. = Vectorize(\(e, x) e[[x]] %||% NA_integer_, list("x"), T, F)(..1, ..2); .})()
}
out <- vector("integer", length(key))
out <- grab(d, sample(key))

It seems the object is fine. Object d exists and can be accessed ( d$GNZuCtwed3CAgNlU ) and

anyNA(out) | !lobstr::obj_size(out) == lobstr::obj_size(val)
[1] FALSE

Use case

Intend to use this function as a utility function in a package. A minimalistic package is available using remotes::install_github("D-Se/minimal"). This dummy package passes R CMD check but throws that error when make_dict is run.

Question

How to properly handle this error?

N.B this question was originally asked on stackoverflow.

Take a look at this issue thread. I’m on a tablet, so I can’t check, but there seems to be an option to set.

1 Like

That is indeed seem to be the same issue. Seeing a response there mentioning a specific RStudio version - after updating to 2021.09.1 Build 372 it seems to be solved, ! I.e, loading the minimal package does no longer result in an error. Thanks!

Github repo issue queries :face_with_raised_eyebrow: wonder if there's an R package for that

For future searchers:
I used RStudio version 1.3 on one machine - updating it. To update, navigate to Help - Check for updates in RStudio or go here directly.

1 Like

Possible recipe

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.