How to use imported functions in examples
I have a package that imports functions from another package. I can use these functions without qualifier in normal code, but not in examples. To demonstrate:
Here is my low-level package that will export the function a
.
library(devtools)
library(roxygen2)
create("lowlevel")
cat(
"#' A function in lowlevel pacakge.
#'
#' Nothing interesting.
#' @return 1
#' @export
a <- function() 1
",
file = "lowlevel/R/a.R"
)
Here is my high-level package that I want to use a
in my example.
create("highlevel")
cat(
"#' A function in highlevel package.
#'
#' Nothing interesting.
#' @return 2
#' @examples
#' a() + b()
#' @importFrom lowlevel a
#' @export
b <- function() 2
",
file = "highlevel/R/b.R"
)
Now we create and install packages:
roxygenize("lowlevel")
roxygenize("highlevel")
install.packages(build("lowlevel"), repos = NULL, type = "source")
install.packages(build("highlevel"), repos = NULL, type = "source")
I get an error when running the example.
library(highlevel)
example(b)
## Error in eval(expr, envir, enclos) : could not find function "a"
highlevel
knows about this function as the file NAMESPACE
contains the line:
importFrom(lowlevel,a)
I can make the example work by specifying the fully qualified name lowlevel::a
, but that makes it awkward to read.
Is there a way to use imported functions in examples without specifying their names?
source to share
The point of importing a function is that it is available to other functions in your package, so you have no problem using it a
in your package code.
If you want to use this function in the examples, you must make the function available to the user in the global environment, that is, you need to export or use it library(lowlevel)
.
source to share
Exporting as Heather suggested and from Jenny seems to be the way forward.
You also need to create a help page in the package highlevel
for the re-export function. A good example is packet retransmission of a tidyr
packet magrittr
.
#' Pipe operator
#'
#' See \code{\link[magrittr]{\%>\%}} for more details.
#'
#' @name %>%
#' @rdname pipe
#' @keywords internal
#' @export
#' @importFrom magrittr %>%
#' @usage lhs \%>\% rhs
NULL
source to share