Trying to build my first R
package using roxygen2
and devtools
. I have added a function that uses %>%
and mutate
in the @examples
section. When I run check()
it fails, because it cannot find the function %>%
or mutate
.
Based on this, this, and this I have tried the following:
I have #' importFrom magrittr %>%
and #' importFrom dplyr mutate
in the function's .R
file. I also have magrittr
and dplyr
under Imports:
in the DESCRIPTION
file. After running document()
, my NAMESPACE
file contains importFrom(dplyr,mutate)
and importFrom(magrittr,"%>%")
.
minimal R/test.R
file:
#' Conditional mutate
#'
#' \code{mutate_cond} mutates the \code{data.frame} only on the rows that
#' satisfy the condition.
#'
#' @param .data \code{data.frame}
#' @param condition expression with the condition to be evaluated
#' @param ... arguments passed to \code{mutate}
#' @param envir environment inherited from \code{parent.frame()}
#'
#' @return \code{data.frame}
#' @importFrom dplyr mutate
#' @importFrom magrittr %>%
#'
#' @examples
#' data(iris)
#' iris %>%
#' mutate(aux = 0) %>%
#' mutate_cond(Petal.Length > 1.3,aux = 3)
#'
#' @export
mutate_cond <- function(.data, condition, ..., envir = parent.frame()) {
condition <- eval(substitute(condition), .data, envir)
.data[condition, ] <- .data[condition, ] %>% mutate(...)
.data
}
minimal DESCRIPTION
file:
Package: test
Version: 0.1
Date: 2019-06-07
Title: Functions
Description: Some functions I use.
Author: me
Maintainer: me <[email protected]>
Encoding: UTF-8
License: GPL-3
Imports: dplyr, magrittr
NAMESPACE
generated with document()
:
# Generated by roxygen2: do not edit by hand
export(mutate_cond)
importFrom(dplyr,mutate)
importFrom(magrittr,"%>%")
I expect this example code to run successfully and pass check()
. Instead I get this error message:
❯ checking examples ... ERROR
Running examples in ‘test-Ex.R’ failed
The error most likely occurred in:
> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: mutate_cond
> ### Title: Conditional mutate
> ### Aliases: mutate_cond
>
> ### ** Examples
>
> data(iris)
> iris %>%
+ mutate(aux = 0) %>%
+ mutate_cond(Petal.Length > 1.3,aux = 3)
Error in iris %>% mutate(aux = 0) %>% mutate_cond(Petal.Length > 1.3, :
could not find function "%>%"
Execution halted
1 error ✖ | 0 warnings ✔ | 0 notes ✔
Also, if I add require(dplyr)
and require(magrittr)
to the @examples
section the error goes away or if I remove the whole @examples
section the error goes away.
Why won't this package pass check()
?
Thank you!
This error usually occurs when a package has not been loaded into R via library . R does not know where to find the specified function. It's a good habit to use the library functions on all of the packages you will be using in the top R chunk in your R Markdown file, which is usually given the chunk name setup .
One error you may encounter in R is: Error: could not find function "%>%" This error often occurs when you attempt to use the “%>%” function in R without first loading the dplyr package. To fix this error, you simply need to load the dplyr package first: library(dplyr)
The devtools package contains functions that help with R package development. These functions include create , which creates the initial structure for a new package, as well as a number of functions for adding useful infrastructure to the package directory and functions to load and document the package.
The @export line is critical. #' @export. This tells Roxygen2 to add this function to the NAMESPACE file, so that it will be accessible to users. For your first R package, you'll probably want to include @export for each of your functions.
Running usethis::use_pipe()
in the console will do the trick as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With