'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:
- How to pass arguments to the R markdown.
- 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?
Solution 1:[1]
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
---
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
