1

I am using R / Rmarkdown / knitr to generate multiple reports (pdfs) via render(), but the content / length of the reports will vary depending on certain characteristics of the underlying data.

As an example, let's say I have 10 different datasets of 50 variables each and I'm examining a correlation matrix of all 50 variables in the data. I want to produce a report for each dataset that has a new page for each variable pair that has a correlation that is greater than 0.5 and each variable pair that has a correlation that is less than -0.5. The number of correlations that will meet these thresholds will vary by dataset, and thus the report length / number of pages will vary by dataset.

I've learned to use {asis, echo = somecondition, eval = somecondition} to evaluate whether an entire section needs to be included (e.g., when there are no negative correlations less than -0.5). I have also seen solutions utilizing 'for' loops when there might be variable-length arguments across reports, but those solutions don't include printing each result on a new page. I'd also like to include section headers on each of the pages reporting the correlations as well.

The difficulty for me is that any solution I can think of requires nesting chunks of text and r code within one another. For some sample Rmd code of how I am approaching the problem, I've tried to print a new histogram for each small dataset on a new page, using "```" to denote where three ticks would usually be as to not mess up the sample code formatting:

"```"{r, echo = FALSE}
datlist <- list(df1 = rnorm(100), df2 = rnorm(100), df3 = rnorm(100)) # fake data
"```"

Some Text Introducing the Report


"```"{'asis', eval = length(datlist) > 0} # evaluating if the section is to be included
"```"{r, echo = FALSE, eval = length(datlist) > 0}

for(i in 1:length(datlist)){ # starting the variable-length scope 

"```"{'asis', eval = length(datlist) > 0} # the information to be included on each new page

\newpage
\section{`r (names(datlist[i]))`}
Here is a histogram of the data found in `r (names(datlist[i]))`.
`r hist(unlist(datlist[i]))`

"```"
} # closing the for loop above
"```" 
"```"

Any help, including a solution using a completely different approach, is most welcome.

CRunyon
  • 83
  • 1
  • 2
  • 10

1 Answers1

1

A correlation is always between two variables so I am unsure wether this is what you want, but the following code will display the correlation of all pairs of variables that are greater than 0.5 in absolute value.

---
title: "Untitled"
author: "Author"
date: "18 November 2019"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r, echo = FALSE}
datlist <- data.frame(var1 = rnorm(100),
                      var2 = rnorm(100),
                      var3 = rnorm(100)) # fake data
# add some correlations
datlist$var4 <- datlist$var1*(rnorm(100,0,0.05)+1)
datlist$var5 <- datlist$var3*(rnorm(100,0,0.05)-1)
# get all correlations, there is probably an easier way of doing this...
corlist <- as.data.frame(t(combn(colnames(datlist),2)))
corlist$cor <- apply(corlist,1,function(x) {
  cor(datlist[,x[1]],datlist[,x[2]])
})
```

Some Text Introducing the Report

```{r, results='asis', echo=F}
apply(corlist[abs(corlist$cor)>0.5,],1, function(x) {
   cat('\n')  
   cat("# Correlation between ", x[1], " and ",x[2],"\n")
   cat("The correlation between both variables was ", x[3], ".\n")
})
```

result

Of course you can extend the content of the loop to do whatever you want with the variables.

Original solution from here

jkd
  • 1,327
  • 14
  • 29
  • Thank you for the help! I have clarified my question a bit based on your feedback. In your solution is there any way to include \newpage or \section in the apply loop? In using your example, I'd "Correlation between var3 and var5" to start on a new page. – CRunyon Nov 18 '19 at 18:19
  • Yes just add `cat("\newpage")` inside the loop. Using `# Your title` should already result in a section since this is markdown syntax. – jkd Nov 18 '19 at 18:33
  • Thank you very much! Unfortunately the details of my report (not really on correlations) made it so that the content didn't align well with the page breaks, but with your answer I was able to come up with a reasonable solution by making a HTML document. – CRunyon Nov 21 '19 at 17:03