12

When I run devtools::check() to build my package and the generate the html file of the rmarkdown vignette, I get an error that the data files can't be found. The html file can be built using any of these:

knitr::knit2html('vignettes/myvignette.Rmd') # works fine
devtools::build_vignettes() # works fine
devtools::build() # works fine

But when I run devtools:check() I get:

mydata <- read.csv("data/mycsv.csv")
Warning in file(file, "rt") :
  cannot open file 'data/mycsv.csv': No such file or directory

  When sourcing 'myvignette.R':
Error: cannot open the connection
Execution halted

How can I get devtools::check() to work? system.file may be relevant but I haven't been able to adapt it to solve my problem. I realise that using rda data files might be a workaround, but I want to use plain text files to store the data in this case.

Here's the myvignette.Rmd, in /vignettes

<!--
%\VignetteEngine{knitr::rmarkdown}
%\VignetteIndexEntry{Supplementary materials}
-->

```{r setup, message=FALSE, echo=FALSE}
library(knitr)
# This is necessary to direct knitr to find the 
# 'data', and other directories that contain
# files needed to execute this document
# thanks to https://stackoverflow.com/a/24585750/1036500
opts_knit$set(root.dir=normalizePath('../'))
```

```{r}
library(mypackage)
myfunc()
```

```{r}
mydata <- read.csv("data/mycsv.csv", header = FALSE)
mydata
```

Here are the key bits of my example package (the rest is auto-generated by devtools::check and I haven't altered them):

DESCRIPTION

Package: mypackage
Title: What the package does (short line)
Version: 0.1
Authors@R: "First Last <first.last@example.com> [aut, cre]"
Description: What the package does (paragraph)
Depends:
    R (>= 3.1.1)
License: MIT
LazyData: true
VignetteBuilder: knitr
Suggests:
    knitr

R/myfunction.r

#' my function
#' An example function
#' @export
#' 
my_func <- function() Sys.time()

R/docfordata.r

#' @title mycsv
#' @docType data
#' @keywords dataset
#' @format csv
#' @name mycsv
NULL

data/mycsv.csv

1,2,3
11,12,13
22,23,23

I'm working in RStudio 0.98.953, here's the session info

sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] mypackage_0.1

loaded via a namespace (and not attached):
 [1] devtools_1.5      digest_0.6.4      evaluate_0.5.5   
 [4] httr_0.3          memoise_0.2.1     packrat_0.3.0.107
 [7] parallel_3.1.1    Rcpp_0.11.2       RCurl_1.95-4.1   
[10] roxygen2_4.0.1    stringr_0.6.2     tools_3.1.1      
[13] whisker_0.3-2  

UPDATE

Following Andrie's helpful comments I've moved my csv file to inst/extdata and put this line in the vignette read.csv(system.file("extdata/mycsv.csv", package="mypackage"), header = FALSE) and that allows my package to pass both devtools::check and devtools::build. But now it failsknitr::knit2html('vignettes/myvignette.Rmd') anddevtools::build_vignettes()` and at the console with the error messages:

For knit2html:

Quitting from lines 22-29 (vignettes/myvignette.Rmd) 
Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
  no lines available in input

For build_vignettes:

Building mypackage vignettes
Quitting from lines 22-29 (myvignette.Rmd) 
Error: processing vignette 'myvignette.Rmd' failed with diagnostics:
no lines available in input

For read.csv(system.file("extdata/mycsv.csv", package = "mypackage"), header = FALSE)

Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
  no lines available in input
In addition: Warning message:
In file(file, "rt") :
  file("") only supports open = "w+" and open = "w+b": using the former

This must be something to do with the wandering inst/ directory that gets moved around when the package is built. So it's fair enough that knit2html and the console might not work, but surely build_vignettes should still work?

Also related: How do I refer to files in the inst directory of an R package from a script in the data directory?

Community
  • 1
  • 1
Ben
  • 41,615
  • 18
  • 132
  • 227
  • Your problem is that the vignette runs in a clean R session, and doesn't know the location of your data. You are correct in that `system.file()` may help. Try something like `read.csv(system.file("data/mycsv.csv", package="mypackage"), header = FALSE)` – Andrie Jul 21 '14 at 14:26
  • 1
    Another possible solution: Add your data as an `.rda` file (i.e. a data frame) and export it. Then you can refer to the data without using `read.csv`. – Andrie Jul 21 '14 at 14:28
  • Thanks, `system.file` as you suggest works in the console, but not for `build` or `check`. I want to keep the data in plain text format as much as possible, it's not very big and I want others to be able to get to it without using `R`. – Ben Jul 21 '14 at 14:42
  • This question is related to http://stackoverflow.com/questions/8129485/using-inst-extdata-with-vignette-during-package-checking-r-2-14-0 and http://stackoverflow.com/questions/13463103/inst-and-extdata-folders-in-r-packaging – Andrie Jul 21 '14 at 14:46
  • That question indicates you should use `inst/extdata`. You seem to have used `inst/data` – Andrie Jul 21 '14 at 14:49
  • No, I haven't used `inst/` at all. I just gave it a go with `inst/data` and your `system.file` line and it's worked ok for both `build` and `check` but not for knitr::knit2html('vignettes/myvignette.Rmd') or devtools::build_vignettes() . On reading R-exts (for the nth time) I see that `inst/extdata` is preferred, so I'll change to that. I'll update my Q. – Ben Jul 21 '14 at 15:05
  • 1
    The vignette for `tidyr` contains an example of reading csv files. Try putting the csv directly in the vignette folder. https://github.com/hadley/tidyr/blob/master/vignettes/tidy-data.Rmd – Andrie Jul 21 '14 at 16:06
  • That's done the trick, thanks very much. It works for all five of my tests. Can you add that as an answer so I can accept it? – Ben Jul 21 '14 at 16:55

1 Answers1

9

To use a file in the vignette, you can add the file to the vignette folder.

An example of this is in the package tidyr at https://github.com/hadley/tidyr/blob/master/vignettes/tidy-data.Rmd

Try putting your csv file directly to the vignette folder

Andrie
  • 176,377
  • 47
  • 447
  • 496
  • I think I may be right in saying that, if you include the data in the installed package, e.g. in inst/extdata, it would be available to the vignette code. – Jack Wasey Oct 04 '15 at 15:20