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!)
To create a package vignette in R Markdown, the easiest way is through the RStudio menu File -> New File -> R Markdown -> From Template (see Figure 16.4). Then you select “Package Vignette” from the rmarkdown package, and you will get a vignette template.
Open a new project in RStudio. Go to the 'File' menu and click on 'New Project. ' Then select 'New Directory,' and 'R Package' to create a new R package.
The Comprehensive R Archive Network (CRAN) is R's central software repository, supported by the R Foundation. It contains an archive of the latest and previous versions of the R distribution, documentation, and contributed R packages. It includes both source packages and pre-compiled binaries for Windows and macOS.
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
.
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