【发布时间】:2014-09-12 01:54:48
【问题描述】:
当我运行 devtools::check() 来构建我的包并生成 rmarkdown 小插图的 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 的有用 cmets 之后,我已将 csv 文件移动到 inst/extdata 并将这一行放在小插图 read.csv(system.file("extdata/mycsv.csv", package="mypackage"), header = FALSE) 中,这样我的包就可以通过 devtools::check 和 devtools::build. But now it failsknitr:: knit2html('vignettes/myvignette.Rmd') anddevtools::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?
【问题讨论】:
-
您的问题是小插图在干净的 R 会话中运行,并且不知道数据的位置。你是对的,
system.file()可能会有所帮助。试试read.csv(system.file("data/mycsv.csv", package="mypackage"), header = FALSE) -
另一种可能的解决方案:将您的数据添加为
.rda文件(即数据框)并导出。然后就可以不用read.csv来引用数据了。 -
谢谢,
system.file正如您所建议的在控制台中工作,但不适用于build或check。我想尽可能将数据保持为纯文本格式,它不是很大,我希望其他人能够在不使用R的情况下获取它。 -
tidyr的小插图包含读取 csv 文件的示例。尝试将 csv 直接放在小插图文件夹中。 github.com/hadley/tidyr/blob/master/vignettes/tidy-data.Rmd
标签: r knitr devtools r-markdown