how to import with roxygen packages the pipe operator %>% from dplyr
Asked Answered
T

2

4

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,"%>%")
...
Then answered 27/10, 2017 at 11:55 Comment(1)
%>% is from magrittr if I'm not mistaking. See also #27947844Modification
L
7

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.

Lovage answered 27/10, 2017 at 12:11 Comment(0)
C
2

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::`%>%`
Cohbert answered 19/10, 2020 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.