0

I have been trying to produce multiple reports from a data set about individual students' diagnostic measures, following the suggestions given here. I have to admit that I am using this procedure for the first time; therefore, it is possible that there are very basic mistakes. Anyway, I created two files (one .Rmd and one .Rnw) and I used a for loop in the .Rnw file to input different data for each report.

Here is my R Markdown file.

library(dplyr)
library(tidyr)
library(magrittr)
library(plyr)
library(knitr)
library(openxlsx)
library(xtable)
library(kableExtra)
library(brew)

xx <- "Student Category Sensitivity Specificity Accuracy
1      A        D       0.000       1.000    0.966
2      A       DB       0.925       0.892    0.907
3      A        E       0.784       0.636    0.729
4      B        D       0.500       0.991    0.975
5      B       DB       0.528       0.969    0.771
6      B        E       0.932       0.636    0.822"

dz <- read.table(text = xx, header = TRUE)

for(cd in unique(dz$Student)){

  knit2pdf("Multiple_reports.Rnw", output = paste0('report_', cd, '.tex'))

  }

And here is my .Rnw file.

\documentclass{article}
\usepackage{fullpage}
\usepackage{booktabs}
\usepackage{caption}
\usepackage{longtable}
\usepackage{array}
\usepackage{multirow}
\usepackage{wrapfig}
\usepackage{float}
\usepackage{colortbl}
\usepackage{pdflscape}
\usepackage{tabu}
\usepackage{threeparttable}
\usepackage{titlesec}
\usepackage{xcolor}
\usepackage{framed}

\begin{document}
\captionsetup{justification = raggedright, singlelinecheck = false}

\title{Individual Report\vspace{-5ex}
}
%\author{}
\date{}
\maketitle

\noindent
This report contains a brief explanation of some diagnostic accuracy measures.

<<loaddata, echo=FALSE,cache = TRUE, autodep = TRUE, cache.comments = FALSE,message = FALSE, warning = FALSE>>=

for(cd in unique(dz$Student)){
    subgroup <- dz %>% dplyr::filter(Student == cd)
    subgroup %>% 
      dplyr::select(-Student) %>% 
      mutate(Sensitivity = cell_spec(Sensitivity, "latex", color = ifelse(Sensitivity > .90, "black", "red"), align = "r"),
         Specificity = cell_spec(Specificity, "latex", color = ifelse(Specificity > .90, "black", "red"), align = "r"),
         Accuracy = cell_spec(Accuracy, "latex", color = ifelse(Accuracy > .90, "black", "red"), align = "r")) %>%
      knitr::kable(., caption = "Results", "latex", booktabs = T, escape = F) %>%
      kable_styling(full_width = T) %>%
      footnote(., "D = Disrespectful; DB = Disruptive Behavior; E = Engagement; P = Prompts") %>% 
      column_spec(1, width = "8cm")
}
@

\end{document}

Unfortunately, when I run the R MArkdown file, multiple reports are made but the table with individual data is not displayed. I'm not able to understand if my mistake is in the for loop or in the procedure that uses Markdown and Sweave together.

Thanks for any help!

Michael Matta
  • 394
  • 2
  • 16
  • 2
    Cannot help with sweave, but I would use parametrised reports https://bookdown.org/yihui/rmarkdown/parameterized-reports.html – Richard Telford Oct 15 '18 at 16:45
  • 1
    The other answer you linked to was 5 years old so lots of the stuff is out of date. This is a really good opportunity for a parameterized report as stated by @RichardTelford – Michael Harper Oct 15 '18 at 16:50

0 Answers0