Search code examples
rnamespacesmagrittr

R: use magrittr pipe operator in self written package


I would like to use the pipe-operator %>% introduced in the magrittr package in a package I wrote myself to chain dplyr data transformations. magrittr is listed as Import in the DESCRIPTION file. After loading my own package and testing the function which uses the pipe-operator I get the following error message:

Error in functionname(parameter, : could not find function "%>%"

Changing %>% to magrittr::%>% in the function source code does not help either because the package cannot be built anymore.


Solution

  • It should have worked correctly if you had magrittr listed in Depends. However, this is not advised. Instead, you leave magrittr in Imports and add the following line to NAMESPACE:

    importFrom(magrittr,"%>%")
    

    I suggest reading Writing R extensions. Your question is covered in paragraphs 1.1.3 and 1.5.1.