3
votes

I want to build a package with some functions i wrote. Now my problem is, that i cannot use the pipe-operator %>% with dplyr. I create the package with roxygen2.

If i write the dplyr-commands without %>%, everything works fine.

inside the code:

#'
#' @import dplyr readr mailR writexl
#' @importFrom dplyr %>%
#' @name %>%
#' 
#' @export
#'

I wrote:

DESCRIPTION

LazyData: true
RoxygenNote: 6.0.1
Imports: dplyr 

roxygen2 generates:

NAMESPACE

...
importFrom(dplyr,"%>%")
...
2
%>% is from magrittr if I'm not mistaking. See also stackoverflow.com/questions/27947344/…Roman Luštrik

2 Answers

5
votes

Usually you would import the pipe operator from magrittr.

You could add a file to the R dir of your package that looks somewhat like this:

#' Pipe
#'
#' Put description here
#'
#' @importFrom magrittr %>%
#' @name %>%
#' @rdname pipe
#' @export
#' @param lhs,rhs specify what lhs and rhs are
#' @examples
#' # some examples if you want to highlight the usage in the package
NULL

In addition you have to add magrittr to your imports in the description file of your package.

1
votes

The solution posed by @clemens is flexible; it allows you to write your own documentation of the imported command. If you don't want to write your own documentation, but instead, want your documentation to automatically link to the documentation from the magrittr package, use the following code in a file in the R directory of your package to import the pipe:

#' @importFrom magrittr %>%
#' @export
magrittr::`%>%`

If you have the dplyr package installed, you can see an example of the default documentation for imported files by entering the following in the R console:

?dplyr::`%>%`