0

I'm trying to find a way to loop through rows of a data frame to produce a report (preferably in PDF) that includes strings the length of a paragraph or longer, plots, and data values for each day of the year. I've exported PDFs of just plots in a loop before, but I can't find a solution to include text of any considerable length.

Imagine I have a data frame like this:

Date <- c("01/01/2014", "01/02/2014", "01/03/2014")
TextString <- c("imagine this is a paragraph or longer 1", "imagine this is a paragraph or longer 2", "imagine this is a paragraph or longer 3")
TableData1 <- c(10000, 9000, 8000)
TableData2 <- c(6, 5, 4)
PlotData3 <- c(22, 11, 21)
PlotData4 <- c(6, 7, 8)
PlotData5 <- c(21, 17, 14)
Report <- data.frame(Date, TextString, TableData1, TableData2, PlotData3, PlotData4, PlotData5)

And I would like the report to be structured something like this in the PDF:

01/01/2014
imagine this is a paragraph or longer 1
"TableData1:" 10000 
"TableData2:" 6 
*plot* pie(c(Report$PlotData3[1], Report$PlotData4[1], Report$PlotData5[1]))

01/02/2014
imagine this is a paragraph or longer 2
"TableData1:" 9000 
"TableData2:" 5 
*plot* pie(c(Report$PlotData3[2], Report$PlotData4[2], Report$PlotData5[2]))  

01/03/2014
imagine this is a paragraph or longer 3
"TableData1:" 8000 
"TableData2:" 4 
*plot* pie(c(Report$PlotData3[3], Report$PlotData4[3], Report$PlotData5[3])) 

Can anyone point me in the right direction? I looked at some tutorials for knitr since it seems applicable, but I wasn't sure it would loop through data in this manner.

TRT08
  • 28
  • 4

1 Answers1

0

This is easily done using R Markdown. If you are using RStudio, this becomes extremely simple. Follow these steps:

  1. Open a new R Markdown file. (File -> New File -> R Markdown...
  2. Pick a title and output format. (These can also be modified in the resulting header)
  3. Paste the contents below into the R Markdown file.
  4. Click on the Knit button.
  5. RStudio automatically processes the R Markdown into the desired format.

The following .Rmd (R Markdown) file will generate what you want in HTML. Changing the output line in the header to pdf_document should result in a PDF file if you have LaTeX installed on your computer.

---
title: "Test PDF"
author: "Daddy The Runner"
date: "Monday, January 05, 2015"
output: html_document
---

```{r echo=FALSE, comment=NA}
Date <- c("01/01/2014", "01/02/2014", "01/03/2014")
TextString <- c("imagine this is a paragraph or longer 1", "imagine this is a paragraph or longer 2", "imagine this is a paragraph or longer 3")
TableData1 <- c(10000, 9000, 8000)
TableData2 <- c(6, 5, 4)
PlotData3 <- c(22, 11, 21)
PlotData4 <- c(6, 7, 8)
PlotData5 <- c(21, 17, 14)
Report <- data.frame(Date, TextString, TableData1, TableData2, PlotData3, PlotData4, PlotData5, stringsAsFactors = FALSE)

for (i in 1:nrow(Report)) {
  cat(Report$Date[i], "\n")
  cat(Report$TextString[i], "\n")
  cat("TableData1: ", Report$TableData1[i], "\n")
  cat("TableData2: ", Report$TableData2[i], "\n")
  pie(c(Report$PlotData3[i], Report$PlotData4[i], Report$PlotData5[i]))
  cat("\n")
}
```