Search code examples
rsweave

Access function from current package when using R CMD check with vignette


I'm putting together a package; for simplicity with one function and one vignette illustrating its use.

I was able to run R CMD check packagename with no difficulties before I tried adding the vignette. The package has a functionfoo.R in the R directory of packagename (it makes a plot with base graphics). The vignette, in the vignettes directory, (an .Rnw file) calls function foo like this:

<<fig1, fig=true, echo=true, include=true>>=
df0 <- data.frame(x1=rnorm(10))
foo(df0)
@

I'm tying to play 'by the rules' but running R CMD check packagename as usual gives:

  When sourcing 'foo.R':
  Error: could not find function "foo"
  Execution halted

I've tried adding the following to the .Rnw file, which didn't help:

\begin{document}
\VignetteDepends{packagename}

I've also tried this with no success:

<<fig1, fig=true, echo=true, include=true>>=
df0 <- data.frame(x1=rnorm(10))
source("foo.R")
foo(df0)
@

Note that the NAMESPACE file already contains the following:

export(foo)

Questions:

Do I need to add a specific source() command in the .Rnw file every time I call a function from the package? If so, how do I specify the path (i.e. where is R CMD check starting from when checking the vignette?)

Or should I take the easy way out by adding the following to the DESCRIPTION file:

BuildVignettes: False

(As I'm able to build a .pdf from the existing .Rnw file).

I'm trying to follow the advice in Writing R extensions.


Solution

  • Your vignette needs to have library("mypkg") at the top so that your own functions, like foo, can be found. I believe this is because the vignette building runs in a clean environment so it doesn't know about your package or any other for that matter unless you bring it up.

    If you have such a line already, put a minimal example of your vignette into your question, and include your sessionInfo() as we may need that to figure it out.