【问题标题】:Writing an R package vignette that reads in an example file?编写读取示例文件的 R 包小插图?
【发布时间】:2012-07-11 09:43:02
【问题描述】:

我正在尝试为 R 中的一个包编写一个小插图。我一直在关注 a tutorial from Vanderbilt Universitythe 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/

标签: r sweave


【解决方案1】:

您可以使用 system.file('doc', 'example', package = 'mypackage') 来引用该目录,因为 R 将在构建小插图之前安装该软件包,正如您在运行 R CMD build mypackage 时所看到的那样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-19
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    • 2018-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多