Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include RMarkdown file in r package? [duplicate]

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.

The goal is to regularly run a function (via a cronjob) that renders the RMarkdown file in order to produce weekly reports.

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.

like image 880
Alex Franz Avatar asked May 21 '15 14:05

Alex Franz


People also ask

How do I import RMD into RStudio?

To open a new file, click File > New File > R Markdown in the RStudio menu bar. A window will pop up that helps you build the YAML frontmatter for the . Rmd file. Use the radio buttons to select the specific type of output that you wish to build.

How do I make a copy of a markdown in R?

To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.

How do I import data into R Markdown?

You can click in the upper left menu File > Import Dataset > From Excel and select the file to import it. Then you can copy the code that appears in the R console with the code required for import the data in xlsx and then copy it in a R Markdown code chunk.

What are .RMD files?

An RMD file is a media file used by RealPlayer, a multimedia playback and streaming program. It may include audio or video data, or a reference to a streaming media location that is played back while it is downloaded. RMD files are often used for standalone audio and video, or Internet radio and video streams.


1 Answers

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.)

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" 

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.

like image 116
r2evans Avatar answered Sep 21 '22 07:09

r2evans