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

久未见 提交于 2019-12-10 13:12:48

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/11421559/writing-an-r-package-vignette-that-reads-in-an-example-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!