poptemp
is a simple little R library that can be used to generate individual files by populating an arbitrary template with the values found in the rows of a data frame. This can be useful when you would like to generate many individualized reports, letters, analyses or other documents from a template.
If you don't already have the devtools
library installed, install it!
To install poptemp
directly from github:
devtools::install_github("ram6ler/poptemp")
You are the Company Designator of Inane Tasks (every company has one), just bursting to use your RStudio skills to help you designate inane tasks more efficiently. In particular, you need to assign an inane task to each of the employees Jack, Ivan, Johab and Jan. Although the task is very similar in each case, the specifics are different. You have the specific details stored, say, in a csv file called inane-tasks.csv
:
person,model_mean,model_sd
Jack,85,10
Ivan,90,5
Johab,100,8
Jan,120,15
You load this into your session:
inane_task_details <- read.csv("inane-tasks.csv")
You set up a template file, say template.Rmd
, that has the approprate variable (column) names in the appropriate places:
---
output: html_document
---
Dear [[person]]
The following script produces a random sample of size 10 drawn from a population
with a Gaussian distribution, mean $\mu=[[model_mean]]$ and standard deviation
$\sigma=[[model_sd]]$.
```{r}
set.seed(1)
random_sample <- rnorm(10, mean = [[model_mean]], sd = [[model_sd]])
```
```
`r paste(sprintf("%.2f", random_sample), collapse = "\n")`
```
Ignoring the population parameters, please calculate the 95% confidence interval
for the mean based on the sample and let me know whether it captures the population
mean. Urgently please.
Regards,
The Company Designator of Inane Tasks
In the R session, you load up the poptemp
library:
library(poptemp)
You then execute the function pop_temp
, specifying the data, template and names for the files to be produced. For example:
pop_temp(
data = inane_task_details,
template_file = "template.Rmd",
output_name_template = "problem-[[i]]-for-[[person]]",
output_extension = ".Rmd"
)
(For other settings, such as the directory for the output files or the format for the substitution markers in the template, see ? pop_temp
.)
This writes several new files to your working directory, one for each row in the data frame:
problem-1-for-Jack.Rmd
problem-2-for-Ivan.Rmd
problem-3-for-Johab.Rmd
problem-4-for-Jan.Rmd
You finally knit these files into an appropriate form and send them out, content with a productive morning!
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.