【发布时间】:2012-07-11 09:43:02
【问题描述】:
我正在尝试为 R 中的一个包编写一个小插图。我一直在关注 a tutorial from Vanderbilt University 和 the offical documentation。
我创建了一个.Rnw Sweave 文件并将其放入我的包内的子目录inst/doc。在同一子目录inst/doc 中,我放置了一个文件夹example,其中包含一些示例文本文件。我的包有一个函数myparser(path),我想在小插图中演示它; myparser(path) 通过读入绝对路径名为path 的文件夹内的文本文件来创建多个数据框。
然后我用R CMD CHECK检查了包,得到了这个错误:
* 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
我发现我尝试使用文件夹的相对路径没有奏效(可能对我来说应该很明显),但我仍然不确定如何解决这种情况。我不想将path 替换为我的电脑 上文件夹的绝对路径,因为那样小插曲的代码将无法在其他人的电脑上重现。
如何在包中包含示例文件,以便小插图的代码可重现?我是否以正确的方式解决这个问题?
(对不起,这个问题本身并不能重现!)
【问题讨论】:
-
您是否尝试过使用
system.file来引用文件? -
显然
/example/是绝对路径;你可能是说example/