Funkeh-Monkeh
Funkeh-Monkeh

Reputation: 661

How do I create separate summary like PDF reports from a rmd file through a loop?

I am very new to RMarkdown and I am trying to generate separate pdf reports with a map from a data frame but the outputs are in a form of assessment questions and recommendations based on specific answers given. I have tried looking for some examples to help to no avail.

Here is a subset of the data frame I am using is as below

      mill_name latitude longitude       theme section                          question               remark
      <chr>    <dbl>     <dbl>       <chr>   <dbl>                             <chr>                <chr>
1    Mill A 3.955042  102.7211 environment     1.1 Do you have issue 1 in your area? Refer to Guideline 1
2    Mill A 3.955042  102.7211 environment     1.2 Do you have issue 2 in your area? Refer to Guideline 2
3    Mill A 3.955042  102.7211      social     1.3 Do you have issue 3 in your area? Refer to Guideline 3
4    Mill A 3.955042  102.7211      social     1.4 Do you have issue 4 in your area? Refer to Guideline 4
5    Mill B 1.961030  103.4140 environment     1.1 Do you have issue 1 in your area? Refer to Guideline 1
6    Mill B 1.961030  103.4140 environment     1.2 Do you have issue 2 in your area? Refer to Guideline 2
7    Mill B 1.961030  103.4140      social     1.3 Do you have issue 3 in your area? Refer to Guideline 3
8    Mill B 1.961030  103.4140      social     1.4 Do you have issue 4 in your area? Refer to Guideline 4

My rmarkdown code I managed to produce so far from my own research is shown below

---
title: "Summary Report"
author: "Authors Name"
date: "September 19, 2015"
output: pdf_document

---

# Summary

This is a summary report for your mill

```{r, comment=NA, echo=FALSE}
# A table with data received from R script
# create reports on students from an Excel spreadsheet. 
library(readxl)
library(knitr)

setwd("C:/Users/Jason/Mill Summary Report")

# read in the excel file

data <- read_excel("dummy_pat_results.xlsx")

for (i in 1:nrow(data)) {
cat("Section:", data$section[i], "\n")
cat("Question: ",data$question[i], "\n")
cat("Remark: ",data$remark[i], "\n")
cat("\n")
}


```

and the R script I am using is shown below

library("rmarkdown")

for (i in unique(data$mill_name)){
  subgroup <- data[data$mill_name == i,]
  rmarkdown::render("Test_Markdown_v2.Rmd", 
  output_file=paste0(i, ".pdf"))    
}

This is what I am getting right now for the 2 pdf's I created which for a start isn't splitting them down to each Mill's report (Mill A and Mill B)

Current output

Ideally, what I would like to get is something like this

enter image description here

I have seen a number of examples of creating separate reports for plots with different sets of data but nothing quite like a report like the one I require. Any help with this is much appreciated. Thanks in advance.

Upvotes: 1

Views: 868

Answers (1)

TheRimalaya
TheRimalaya

Reputation: 4592

I think you need Parameterized Rmarkdown document. Following links takes you to a very simple tutorial. I hope this will solve your problem.

http://rmarkdown.rstudio.com/developer_parameterized_reports.html

Upvotes: 1

Related Questions