Could not find function in Roxygen examples during CMD check
My guess is that you need to #' @export
the function in the Roxygen comment, otherwise the function is not exported to the namespace of the package and it cannot be found.
could not find function when running examples in package check
You should also have :
importFrom(dplyr,inner_join)
in NAMESPACE
you can get Roxygen to create this in NAMESPACE by having :
#' @ImportFrom dplyr inner_join
NULL
in one of your package's R scripts
Could not find function %% during CMD check
Add this to the script that contains the function(unless you imported the entire dplyr).
@importFrom magrittr "%>%"
If you intend to use dplyr
functions maybe explicitly call them like:
dplyr::group_by
dplyr::filter_at
CRAN checks errors on roxygen @examples: base::assign and missing reshape2
As @stefan suggested in the comments, I had to add reshape2
into the Suggested packages in the description file. I added using:
usethis::usepackage("reshape2", "Suggests")
Followed by regenerating the docs:
devtools:document()
Package is on its way to CRAN!
R CMD check gives warning when using @includeRmd for examples using Roxygene
Your usage of the @includeRmd
tag does not appear to be supported. You can use it to generate a Description or (sub)sections of Details, but not Examples. The documentation states:
It is currently not possible to document function arguments, return values, etc. in external Rmd documents.
That isn't too surprising, for a couple of reasons:
R CMD check
expects the\examples{}
block to contain verbatim R code, not Markdown, so injecting Markdown into the block without preprocessing is doomed to fail. Due to the chunk header and footer, the following is not valid R code:```{r adder_examples}
adder(3, 5, 6)
```That said,
roxygen2
seems to do some preprocessing, since the above generates the following\examples{}
block:\examples{
\if{html}{\out{<div class="sourceCode r">}}\preformatted{adder(3, 5, 6)
}\if{html}{\out{</div>}}\preformatted{## [1] 14
}The injected text would be valid inside of
\description{}
or\details{}
, but isn't valid inside of\examples{}
as it is not verbatim R code.As exceptions to the "verbatim" rule, the
\examples{}
block permits the\dontrun
,\dontshow
, and\donttest
macros. They are handled exceptionally byR CMD check
, but not by theknitr
vignette engine. You'd get a build failure if you tried to insert this chunk into a vignette:```{r adder_examples}
adder(3, 5, 6)
\dontrun{
adder(3, 5, "6")
}
```With chunk option
eval = 1L
, the parser still throws an error. (eval = FALSE
seems OK, but then the first line isn't evaluated.) Hence, even if@includeRmd
could be used to generate a valid\examples{}
block, you wouldn't necessarily be able to share the code with a vignette.
If you really want to externalize your Examples, then you can try doing it the old-fashioned way, i.e., with a shell script and sed
, using @examples
as a marker for text insertion (some examples here). I doubt the complexity is worth it, but you can be the judge...
Update: I've just tried to hack things a bit with my own minimal package...
pkgname <- "foo"
usethis::create_package(pkgname, rstudio = FALSE, open = FALSE)
setwd(pkgname)
usethis::use_directory(file.path("inst", "examples"))
text <- "
#' @title A title
#' @description A description.
#' @param a,b Arguments.
#' @includeRmd inst/examples/add.Rmd examples
#' @export
add <- function(a, b) a + b
"
cat(text, file = file.path("R", "add.R"))
text <- "
add(1, 1)
"
cat(text, file = file.path("inst", "examples", "add.Rmd"))
roxygen2::roxygenize(".")
writeLines(readLines(file.path("man", "add.Rd")))
\examples{
add(1, 1)
}
So it seemed like deleting the chunk header and footer might allow roxygen2
to generate a sensible \examples{}
block. But I was wrong:
text <- "
add(1, 1)
add(2, 2)
"
cat(text, file = file.path("inst", "examples", "add.Rmd"))
roxygen2::roxygenize(".")
x <- readLines(file.path("man", "add.Rd"))
writeLines(tail(x, -(grep("^\\\\examples", x) - 1L)))
\examples{
add(1, 1) add(2, 2)
}
The generated text is invalid R code when the example spans two or more lines, because Markdown treats newlines like spaces.
### ** Examples ... Error: could not find function building packages in R
The following achieves what you want, I believe:
lad <- function(y, x, data) {
dat <- setNames(read.csv(data)[, c(x, y)], c('x', 'y'))
sum.abs.dev <- function(beta, data) {
with(data, sum(abs(y - beta[1] - beta[2] * x)))
}
fit <- lm(y ~ x, dat)
optim(par=coef(fit), sum.abs.dev, data=dat)$par
}
lad(y = "farm", x = "land", data="FarmLandArea.csv")
# (Intercept) x
# -605.2293682 0.3642028
r: errors creating package with devtools & roxygen2
Best answer I've found is in this blog post: Hilary Parker R-Package Blog Post
The steps I follow to document and install are as follows:
- Within the project that contains my package, open a new R Script and run
setwd('..')
- Run
devtools::document()
- Run
devtools::install()
This works for me when initially installing my package and also updating it.
Related Topics
Print R-Squared for All of the Models Fit with Lmlist
Ggplot2 and Geom_Density: How to Remove Baseline
How to Reverse Legend (Labels and Color) So High Value Starts at Bottom
How to Transpose a Tibble() in R
Fast Way to Group Variables Based on Direct and Indirect Similarities in Multiple Columns
How to Create a Line Plot with Groups in Base R Without Loops
Ggplot2: How to Transparently Shade Alternate Days on a Plot
R, Sweave, Latex - Escape Variables to Be Printed in Latex
How to Access Browser Session/Cookies from Within Shiny App
Replace a Subset of a Data Frame with Dplyr Join Operations
Intersecting Points and Polygons in R
R Plots: How to Draw a Border, Shadow or Buffer Around Text Labels
R - Cumulative Sum by Condition
Ggplot2: Fill Color Behaviour of Geom_Ribbon
How to Create a Vector of Functions
Create Line Graph with Ggplot2, Using Time Periods as X-Variable