r-lib / coro

Coroutines for R
https://coro.r-lib.org/
Other
160 stars 8 forks source link

How to pass rcmdcheck using generators #40

Closed latot closed 6 days ago

latot commented 1 year ago

Hi all, I'm trying to use generators inside a library, but after declaring a generator and using it this happens:

f_iterator <- coro::generator(function(ret, from, to) {
    for (id in seq(length(from))){
      coro::yield(list(
        from = from[[id]],
        to = to[[id]],
        distance = ret[[id]]
      ))
    }
})
  f_iterator : <anonymous>: no visible
    binding for global variable ‘generator_env’
  f_iterator : <anonymous>: no visible
    binding for global variable ‘exits’
  Undefined global functions or variables:
    exits generator_env id

What is the right way to handle this?

Usually, with rcmdcheck, use global variables are more like a workaround than a proper fix.

Thx!

lionel- commented 1 year ago

For now you'll need to use the globalVariables() workaround. We've working towards a better solution in https://bugs.r-project.org/show_bug.cgi?id=14354#c10

latot commented 1 year ago

Hi!, mmm, reading it, there is something I can add to that bug.

But first, here is the workaround:

utils::globalVariables(c(
  "generator_env",
  "exits"
))

Now, about the declarations on tidyverse, like with .data or similar, you don't need to use the globalVariables, maybe a better alternative for rcmdcheck + tidyverse is the next one:

## manual namespace: end
## dplyr vars
#' @importFrom rlang .data
#' @importFrom rlang .env
## manual namespace: end

Still, is not a perfect solution, because .data and .env should only be valid inside a tidyverse function that uses it.

Thx!