klmr / box

Write reusable, composable and modular R code
https://klmr.me/box/
MIT License
833 stars 47 forks source link

How to use names from a different package inside a module? #280

Closed EKtheSage closed 2 years ago

EKtheSage commented 2 years ago

Error description

Hi,

I just started learning about box, so I may not know the right way to use it yet. I ran into this error the other day, hopefully you can show me how to do it.

I wrote a test function in test.R script in the R folder in my project

test <- function(dat) {
  dat %>% 
    filter(x == 3)

  dat
}

In a separate file, I called the function using box:

box::use(
  R/test[test],
  dplyr[...]
)

In search() I can see both dplyr and test.R in the environment:

> search()
 [1] ".GlobalEnv"        "mod:dplyr"         "mod:R/test"        "tools:rstudio"     "package:stats"    
 [6] "package:graphics"  "package:grDevices" "package:utils"     "package:datasets"  "package:methods"  
[11] "Autoloads"         "org:r-lib"         "package:base"     

But if I call test(mtcars) it resulted in the following error:

> test(mtcars)
Error in dat %>% filter(x == 3) : could not find function "%>%"

R version

> R.version
               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          4                           
minor          1.2                         
year           2021                        
month          11                          
day            01                          
svn rev        81115                       
language       R                           
version.string R version 4.1.2 (2021-11-01)
nickname       Bird Hippie

‘box’ version

1.1.2