Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing an R package vignette that reads in an example file?

Tags:

r

sweave

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!)

like image 309
user1440196 Avatar asked Jul 10 '12 20:07

user1440196


People also ask

How do you make a package vignette in R?

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.

How do I code an R package?

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.

What do you mean by Cran in R programming?

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.


1 Answers

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.

like image 105
Yihui Xie Avatar answered Oct 12 '22 23:10

Yihui Xie