在devtools :: check期间,Vignette无法找到数据文件

时间:2014-07-21 14:06:59

标签: r knitr devtools r-markdown

当我运行devtools::check()来构建我的包并生成rmarkdown vignette的html文件时,我收到一条错误,指出无法找到数据文件。可以使用以下任何一个构建html文件:

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

但是当我跑devtools:check()时,我得到了:

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

如何让devtools::check()工作? system.file可能是相关的,但我无法通过它来解决我的问题。我意识到使用rda数据文件可能是一种解决方法,但我想在这种情况下使用纯文本文件来存储数据。

这里是myvignette.Rmd,位于/ 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
```

以下是我的示例包的关键部分(其余部分由devtools::check自动生成,我还没有改变它们):

说明

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

数据/ mycsv.csv

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

我在RStudio 0.98.953工作,这里是会议信息

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  

更新

根据Andrie的有用评论我已将我的csv文件移至inst / extdata并将此行放在插图read.csv(system.file("extdata/mycsv.csv", package="mypackage"), header = FALSE)中,这样我的包就可以通过devtools::check和devtools :: build . But now it fails knitr :: knit2html(&#39; vignettes / myvignette.Rmd&#39; ) and devtools :: build_vignettes()`并在控制台上显示错误消息:

对于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

对于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

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

这必须与在构建包时移动的漫游inst/目录有关。因此knit2html和控制台可能无法正常运行,但build_vignettes肯定会有效吗?

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

1 个答案:

答案 0 :(得分:7)

要使用晕影中的文件,您可以将文件添加到晕影文件夹中。

此示例位于https://github.com/hadley/tidyr/blob/master/vignettes/tidy-data.Rmd

tidyr包中

尝试将csv文件直接放到晕影文件夹