Lenn
Lenn

Reputation: 1469

How to create dynamic r markdown reports from r package

I want to create a function in my r-package that kind of looks like this:

mypackage::render_github_overview(username, repo, output = "/tmp/testDir/test.html")

This should then generate a directory containing the Rmd and the output (either html or pdf). The pseudo-content would look something like:

## Overview for Repository <repository>

User <user> has <n_repos> repos with <n_stars> stars

Now my questions are:

  1. How to pass arguments to the R markdown.
  2. Where to store the template R markdown

Regarding 1.: I think the solution might be a parameterized R Markdown document and then calling rmarkdown::render(param1...) with the parameters.

Regarding 2 my doubts are where to store the R markdown template where the gaps will be filled in. From this answer here (How to include RMarkdown file in r package?) I assume it could be in R/inst/rmd/template.Rmd or something like this. Or is there any better location?

Upvotes: 3

Views: 652

Answers (1)

Adele
Adele

Reputation: 93

You should put your rmd file in inst/rmd/template.Rmd. Then render it using a code like this:

rmarkdown::render(
   input = paste0(system.file(package = "mypackage"), "/rmd/template.Rmd"),
   output_file = template.pdf,
   params = list(x1 = firstParam, x2 = secondParam, data = yourData),
   encoding     = 'UTF-8'
   )

About your parameters: put them at the beginning of your rmd file, like this:

---
title: "your title"
author: name 
output: beamer_presentation
params:
  x1 : firstParam
  x2 : secondParam
  data : yourData
---

Upvotes: 3

Related Questions