如何使用vignette向R包添加任意内容?
如果可以的话,如何使用 R 包小插图生成任意文件以包含在已安装的包中?
我今天一直在修改两个具体的用例和方法。
首先,我有一个 Excel 模板文件,我希望用 (Sweave) 小插图中派生的数据填充该文件,然后在包中提供此修改后的 Excel 文件(与模板一起)。按照约定,模板位于源码包的inst/extdata目录下;该插图包含一些代码,这些代码使用 system.file
读取此内容,然后将修改后的副本写回同一目录。然而,虽然 vignette(和包)使用 R CMD build
成功构建,但在 tarball 的 R CMD INSTALL
之后,已安装的 extdata
目录包不包含修改后的文件,仅包含模板。
第二个用例更通用,涉及 Makefile。我想将 .tex 文件中的小插图渲染为 PDF 和 HTML 格式。我想要使用 make4ht 创建的 HTML 版本,实际上是供另一个应用程序摄取的,而不是在包本身中使用;因此我不希望它们与 PDF 小插图一起放在 doc
目录中 - 让我们将它们放在 outputs
中。
目前我正在尝试使用 Makefile 来实现此目的。在源包的 vignettes
目录中,我有一个非常基本的 Makefile,如下所示:
all : foo.tex
make4ht foo.tex
mkdir ../outputs
mv foo.html ../outputs
texi2pdf foo.tex
rm foo.tex
R CMD build
上的预期行为是:
- 创建 vignette < 的 HTML 版本code>foo.html 和
make4ht
- 在 R 包根目录下直接创建一个名为
outputs
的目录 - 将 HTML 文件移到那里
- 创建 PDF 插图 正确
- 清理要避免的 .tex 文件关于重复的小插图条目的错误
然后,在生成的 tarball 的 R CMD INSTALL 上,我希望在已安装的包下找到一个目录 outputs
,其中包含文件 foo.txt。 html
.然而,事实并非如此——PDF 和源插图都在那里,但没有输出
的迹象。
据我所知,软件包被安装到临时目录中是为了构建小插图。我假设写入此临时位置的文件实际上都没有合并到最终包中,除了 vignettes/*.pdf
或 vignettes/*.html
之类的文件。因此,在这种情况下,让我重复我的问题:
是否可以使用小插图创建(任意)文件以包含在包中?
我知道所有这些都可以在包构建之前完成,将文件放在 inst
下的某个位置。但如果可能的话,我更愿意一步完成这一切;这些小插图记录了数据分析的各个方面,我想在最终的包中提供此分析的输出,因此这似乎是一个自然的工作流程。
How, if at all, can one use R package vignettes to generate arbitrary files for inclusion in the installed package?
I have two concrete use-cases and approaches I have been tinkering with today.
First, I have a template Excel file which I wish to populate with data derived in a (Sweave) vignette, and then provide this modified Excel file in the package (alongside the template). Following convention, the template is in the inst/extdata
directory of the source package; the vignette contains some code which reads this using system.file
and then writes the modified copy back to the same directory. However, while the vignette (and package) builds successfully with R CMD build
, after R CMD INSTALL
of the tarball the extdata
directory of the installed package does not contain the modified file, only the template.
The second use-case is more general, and involves Makefiles. I want to render the vignette(s) to both PDF and HTML format from the .tex files. The HTML versions I want to create using make4ht
, and are actually provided for ingestion by another application, rather than for use in the package per se; therefore I don't want them in the doc
directory with the PDF vignettes -- let's put them in outputs
.
Currently I am trying to use a Makefile for this purpose. In the vignettes
directory of the source package, I have a very rudimentary Makefile like the following:
all : foo.tex
make4ht foo.tex
mkdir ../outputs
mv foo.html ../outputs
texi2pdf foo.tex
rm foo.tex
The expected behaviour on R CMD build
is:
- Create the HTML version of the vignette
foo.html
withmake4ht
- Create a directory for it called
outputs
directly under the R package root - Move the HTML file there
- Create the PDF vignette proper
- Clean up the .tex file to avoid errors about duplicated vignette entries
Then on R CMD INSTALL
of the resulting tarball, I would expect to find a directory outputs
under the installed package, containing the file foo.html
. However, this is not the case -- the PDF and source vignettes are there, but there is no sign of outputs
.
I understand that packages are installed to a temporary directory for the purpose of building vignettes. I assume that none of the files written to this temporary location are actually incorporated into the final package, except files like vignettes/*.pdf
or vignettes/*.html
. So, with this context, let me repeat my question:
Is it possible to create (arbitrary) files for inclusion in a package using vignettes?
I am aware that all of this could be done prior to package-building, putting files somewhere under inst
. But I would prefer to have it all done in a single step if possible; the vignettes are documenting various aspects of data analysis, and I want to provide the outputs of this analysis in the final package, so it seems a natural workflow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我很确定你做不到,而且尝试也没有任何意义。用户应该可以运行小插图来复制您获得的结果,或者进行一些小的更改以查看它们的效果。用户不应期望这会更改包的内容。
如果您希望小插图为用户生成文件,则默认情况下它们应该位于临时目录中,因此它们不会对用户的文件系统进行永久更改。
我认为你最后一段中的方法是唯一可行的方法。您在构建包之前运行一些 Makefile,并且可能会运行 vignette 将文件安装到某个地方的
inst
中。实现此目的的一种方法是让您的小插图查看环境变量以找到放置文件的目的地。您的 Makefile 可以在处理小插图之前设置环境变量。但是,如果用户(或 R CMD 构建系统)在没有该环境变量的情况下运行您的 vignette,则文件将不会生成,或者将在运行结束时被删除。I am pretty sure you can't do that, and it doesn't really make sense to try. Vignettes are supposed to be runnable by users to duplicate the results you got, or to make small changes to see how they work out. Users shouldn't expect that to change the contents of the package.
If you want your vignette to produce files for the user, they should by default be in the temporary directory, so they won't make permanent changes to the user's file system.
I think the approach in your final paragraph is the only way that will work. You run some Makefile before building the package, and perhaps it runs the vignette to install files into
inst
somewhere. One way to do this would be for your vignette to look at environment variables to find the destination where it puts its file. Your Makefile can set the env var before it processes the vignette. But if a user (or theR CMD build
system) runs your vignette without that env var, the files won't be produced, or will be deleted at the end of the run.