Use of `<<-` in closure-like functions and CRAN submission

I've the following helpers in my package (only mind the use of iwalk() and <<-):

add_suffixes <- function(data, cols, symbols) {
  .cols <- predot(cols)
  iwalk(symbols[names(cols)], \(value, key) {
    if (is.null(value)) {
      return()
    }
    if (key == "orcid") {
      data <<- add_orcid_icons(data, value)
    } else {
      data <<- add_symbols(data, .cols[key], value)
    }
  })
  data
}

crt_assign <- function(data) {
  iwalk(.names$protected$crt, \(value, key) {
    if (!has_name(data, key)) {
      return()
    }
    data[key] <<- if_else(data[[key]] == 1L, value, NA)
  })
  data
}

I got the following comment from CRAN:

Please do not modify the global environment (e.g. by using <<-) in your
functions. This is not allowed by the CRAN policies.

I'm pretty certain that <<- only modifies the parent environment (i.e. add_suffixes and crt_assign's environment here, not the global environment as stated in the comment).

Any advice on how to handle this comment?

This is my first submission so I've no clue how useful arguing is. I could certainly emulate iwalk() with a pair of for loops but that's my least favorite option because it really makes the code less readable…

You can just tell them that your <<- does not modify the global environment. I did this several times in similar situations.

Personally, I would write this functions with an explicit data "call", to force an evaluation. I think your functions are fine without it, but in general that is good practice, whenever your are creating functions within functions. I.e.:

add_suffixes <- function(data, cols, symbols) {
  data
  .cols <- predot(cols)
  iwalk(symbols[names(cols)], \(value, key) {
    if (is.null(value)) {
      return()
    }
    if (key == "orcid") {
      data <<- add_orcid_icons(data, value)
    } else {
      data <<- add_symbols(data, .cols[key], value)
    }
  })
  data
}
1 Like

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.