Search code examples
rdplyrr-box

Loading data mask manually when using box


I have an R code that I'm changing to use R's box package.

But I noticed that a function I had no longer has the dataframes columns as variables in the environment, so when I do dplyr's filter I get object 'verified' not found.

What's the easiest way to solve this? I want to load the dataframe columns as variables in the function environment.

Simplified version of my code, yes the verified column does exist

box::use(
  tibble[...],
  dplyr[...],
  lubridate[...],
  r/core[...]
)

myFunction <- function(df){
  df = df %>%
    filter(verified == TRUE)
  return(df)
}

Solution

  • Because you load r/core after dplyr, stats::filter() is masking dplyr::filter(). If you load r/core first, it works as intended:

    box::use(
      r/core[...],
      tibble[...],
      dplyr[...],
      lubridate[...]
    )
    
    myFunction <- function(df){
      df = df %>%
        filter(verified == TRUE)
      return(df)
    }
    
    dat <- tibble(
      x = 1:2,
      verified = c(F, T)
    )
    
    myFunction(dat)
    
    # A tibble: 1 × 2
          x verified
      <int> <lgl>   
    1     2 TRUE    
    

    Alternatively, you could specify dplyr::filter() in your function.

    (Finally, I’m not that familiar with box, but do you really have to explicitly load r/core at all?)