如何在R包中包含RMarkdown文件? [英] How to include RMarkdown file in r package?
问题描述
我正在用R创建程序包,我也想包含一个R Markdown文件.该RMarkdown模板包含我程序包中的函数,并通过knitr呈现为html文档.
I'm in the process of creating a package in R and I also want to include an R Markdown file. This RMarkdown template contains functions from my package, and is rendered to an html document via knitr.
目标是定期运行一个函数(通过cronjob)以呈现RMarkdown文件,以生成每周报告.
The goal is to regularly run a function (via a cronjob) that renders the RMarkdown file in order to produce weekly reports.
在进行函数调用以渲染所述模板时,如何将此类文件添加到R包(如.Rmd)并引用.Rmd,尤其是因为使用use_data(myrmarkdown.Rmd)
不会获得所需的结果.
How is it possible to add such files to an R package (like a .Rmd) and reference the .Rmd when making a function call to render said template, particularly since using use_data(myrmarkdown.Rmd)
won't achieve the desired result.
推荐答案
在创建 R 包时,您将在目录的根目录中包含一个目录树,其中包含以下内容(以及其他内容)软件包:DESCRIPTION
,NAMESPACE
和R/
目录.如果您还有一个inst/
目录,则该目录中的所有内容都将被原样复制到包目录中,而inst/
除外.
When you are creating an R package, you will have a directory tree containing the following (among others) in the root directory of the package: DESCRIPTION
, NAMESPACE
, and the R/
directory. If you also have an inst/
directory, then everything within that directory is copied verbatim to within your package directory, excluding the inst/
.
例如,如果您的软件包目录如下:
For instance, if your package directory looks like this:
+- DESCRIPTION
+- NAMESPACE
+- inst/
| \- rmd/
| \- file.Rmd
\- R/
+- file1.R
+- file2.R
\- file3.R
然后,在构建软件包并安装它时,您会在软件包库中找到以下内容:
Then when you build the package and install it, you'll find in the following in your package library:
+- DESCRIPTION
+- INDEX
+- NAMESPACE
+- rmd/
| \- file.Rmd
\- R/
+- packagename
+- packagename.rdb
\- packagename.rdx
(其他文件/目录是在此过程中创建的,为简单起见,我将其忽略.)
(Other files/directories are created during the process, I'm ignoring them for simplicity.)
您需要了解的最后一条信息是安装后如何访问此文件?"由于某些系统将 R 库安装在不同的目录中,并且用户经常在个人 R 库中安装软件包,因此您不知道先验的输入位置system.file
:
The last piece of information you need to know is "how do I access this file once it is installed?" Since some systems install the R library in different directories, and on top of that users often install packages within a personal R library, you cannot know a priori where to look Enter system.file
:
system.file("rmd", "file.Rmd", package = "packagename")
## [1] "c:/R/R-3.1.3/library/packagename/rmd/file.Rmd"
这可以用于整个Rmd文件.我将其用于Rmd呈现的文档的公司特定模板.也就是说,我正在寻找包含"文件来个性化LaTeX,以便渲染的PDF具有页眉/页脚并按照我们想要的方式进行样式设置.此步骤需要编写一个函数来替换Rmd YAML标头中的pdf_document
(例如),但是在 rmarkdown中可以很好地覆盖它. rstudio.com .
This can be used for the whole Rmd file. I use it for company-specific templates for Rmd-rendered documents. That is, I look for "include" files to personalize the LaTeX so that the rendered PDF has headers/footers and is styled the way we want. This step requires writing a function that replaces the pdf_document
(for example) in the Rmd YAML header, but that's covered well at rmarkdown.rstudio.com.
这篇关于如何在R包中包含RMarkdown文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!