I'm trying to write a vignette for a package in R. I've been following a tutorial from Vanderbilt University as well as the offical documentation.
I made a .Rnw
Sweave file and put it into a subdirectory inst/doc
inside my package. Inside the same subdirectory inst/doc
, I put a folder example
containing some example text files. My package has a function myparser(path)
that I want to demonstrate in the vignette; myparser(path)
creates several data frames by reading in the text files inside the folder with absolute path name path
.
Then I checked the package using R CMD CHECK
, and got this error:
* checking running R code from vignettes ...
‘mypackage-vignette.Rnw’ using ‘UTF-8’ ... failed
ERROR
Errors in running code in vignettes:
when running code in ‘mypackage-vignette.Rnw’
...
> library(mypackage)
Loading required package: ggplot2
> myparser("/example/")
Warning in file(file, "rt") :
cannot open file '/example/': No such file or directory
When sourcing ‘mypackage-vignette.R’:
Error: cannot open the connection
Execution halted
I see my attempt to use a relative pathway to the folder didn't work (probably should have been obvious to me), but I'm still not sure how to fix this situation. I don't want to replace path
with an absolute pathway to the folder on my computer, because then the vignette's code won't be reproducible on other people's computers.
How can I include the example files in the package so that the vignette's code is reproducible? Am I even approaching this problem in the right way?
(Sorry this question isn't itself more reproducible!)
You can use system.file('doc', 'example', package = 'mypackage')
to refer to that directory, because R will install the package before building vignettes, as you can see when you run R CMD build mypackage
.