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 <[email protected]> [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 fails
knitr::knit2html('vignettes/myvignette.Rmd') and
devtools::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?
The deletion is happening because you need to use devtools:check (".", vignettes=FALSE) and there is no way to set that from the Build Options in RStudio. But you can uncheck the use devtools package functions and stop RStudio from using devtools.
However the vignette itself can be knitted without any problem, and the function caused error has been used in my package for a long time without any problem. I tried to reproduce the error but cannot find how to do it. It looks like devtools used some specific mode to run the code...
tools::buildVignettes (dir = ".", tangle=TRUE) dir.create ("inst/doc") file.copy (dir ("vignettes", full.names=TRUE), "inst/doc", overwrite=TRUE) Then push the changes to GitHub. Also this will mean that when you use Install and Restart on the Build tab, your vignettes will now be there too (e.g. they'll show up on browseVignettes ().
However, in order to be working, your vignette requires your package to be loaded. So, as you said, you'll have to call library (mypackage) and so your package have to be installed. You can install your package without the vignette in the command line with devtools::install (build_vignette = FALSE).
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
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