Reproducible Longitudinal report writing
I'm trying to write a report which looks at longitudinal data, and analyzes those data in the same way each time. Because of the nature of my data (they include network objects generated within igraph
), I can't/don't combine them into a single data structure with year as a column. I'm trying to generate a report, and I've been marginally successful. This is my method so far
First, I create the analysis template
Here is a MWE which I have called templateSection.Rnw
:
% !Rnw weave = knitr
\section{Year \Sexpr{y} results}
Here is our ``data'' from year \Sexpr{y}.
<<results='hide'>>=
set.seed(y)
yearData = rnorm(1000, mean = y)
knitr::opts_chunk$set(fig.path = paste0('year',y,'/'))
@
Now let's make a plot:
<<histogram>>=
hist(yearData)
@
And let's view the summary:
<<>>=
summary(yearData)
@
Next, I make a loop to go through all of the years
I've created a script that I've entitled generateSections.R
for that purpose.
library(knitr)
years = 1:5
for(y in years){
outFile = paste0('year',y,'.tex')
knit(input = 'templateSection.Rnw', output = outFile)
}
Then I put them into my master report document
I've entitled this masterReport.Rnw
% !Rnw weave = knitr
\documentclass{article}
\title{Fantastic Yearly Report}
\author{me}
\date{\today}
\begin{document}
\section{Introduction}
Blah blah, we have this cool longitudinal data we want to study, let's do it!
%% Script generates/knits all of the appropriate sections individually
<<>>=
source('generateSections.R')
@
%% Now drop them here:
\input{year1}
\input{year2}
\input{year3}
\input{year4}
\input{year5}
\end{document}
The problem:
When I knit Ctrl-Shift-K
the master document in RStudio, it fails. The error I get is:
LaTeX Error: File `year2.tex' not found.
Indeed, only year1.tex
has been generated, and it works if I only include that file.
My workaround so far has been to source the generateSections.R
script before I knit the document. Then knitting works. But I'd really prefer to run one thing. Part of my plan is to study trends in the yearly data, so I plan on further editing the master report document and add some chunks at the end.
Thing I've tried and other system info:
- I've added this line
opts_knit$set(self.contained=FALSE)
in a number of places, but I'm not sure where it's supposed to go. I found that in the R Studio Docs. (Should it be in the master doc? The child doc? Both docs? Does the position within the code matter (does it have to be the first line of the first chunk)?
My system info is:
- R version 3.6.3 (2020-02-29)
- packageVersion("knitr") ‘1.28’
- RStudio Version 1.3.1073
- Ubuntu 18.04.5 LTS
Additional answers that I would find helpful:
- Why are you doing this with
Rnw
files?Rmd
files have a much more elegant solution, and you can do it like this... - You don't need to manually enter the files you want to input. You can do it like this...