I have an .Rmd
report that I loop over a variable to get the report to export one report per variable in the dataset. As part of my report structure, I have an incremental function to label the tables I’m generating in the report. Each individual report should start with Table 1 and iterate from there for each table. This works correctly when I just knit the .Rmd itself. But when I use an .R
script to loop over the .Rmd
script, the iteration doesn’t reset back to one between loops.
So, the first report that kicks out starts the table captions at “Table 1” and then iterates through to “Table 4”. The second report that kicks out from the loop starts at “Table 5” and then iterates from there and so on. I need each report to start the captions at “Table 1”.
I’ll try to include a MWE below, but my report is heavily reliant on a package I developed to make these reports match the organizational style guide. The first section of the below code chunks is a relevant chunk of my developed package, modified for inclusion here.
Required Package Exported Functions
## Part of the org package, but included here for replication.
## These functions are exported for use in the package.
# Simple incremental function for labeling tables/plots/figures
increment <- function(init) function() { init <<- init + 1; init }
#Assigning new count functions
plotcount <- increment(0)
tablecount <- increment(0)
figurecount <- increment(0)
# Table/plot/figure caption function
caption <- function(caption, type) {
if (type == "plots") {
paste0("Plot ",plotcount(),". ",caption)
} else if (type == "tables") {
paste0("Table ",tablecount(),". ",caption)
} else if (type == "figures") {
paste0("Figure ",figurecount(),". ",caption)
} else {paste0("Please choose a type from 'plots', 'tables', and 'figures'.")}
}
.Rmd and MWE data
## .Rmd relevant bits and MWE data
---
title: "Title"
author: "Author"
date: 'r Sys.Date()'
output:
pdf_document:
latex_engine: lualatex
params:
primarygroup: i
---
``` {r setup, include=FALSE}
# knitr chunk options
knitr::opts_chunk$set(
echo = FALSE
)
# Reset increment
## This was a test, which failed.. Not sure what to do.
increment(0)
## Required Packages for Document
library(tidyverse)
library(kableExtra)
library(scales)
```
``` {r data-creation}
p1 <- tibble(
Itinerary = c(100, 101, 102, 103, 104),
PrimaryGroup = c("Contractor 1", "Contractor 1", "Contractor 2", "Contractor 2", "Contractor 3"),
Location = c("Loc 1", "Loc 2", "Loc 1", "Loc 2", "Loc 2"),
ProblemDays = c(3, 5, 4, 3, 5),
Fine = c(150, 250, 200, 150, 250)
)
p2 <- tibble(
Itinerary = c(200, 201, 202, 203, 204),
PrimaryGroup = c("Contractor 1", "Contractor 1", "Contractor 1", "Contractor 1", "Contractor 2"),
ProblemDays = c(3, 5, 4, 3, 5),
Fine = c(150, 250, 200, 150, 250)
)
```
# Problem 1
``` {r p1-table}
p1 %>%
filter(PrimaryGroup == params$primarygroup) %>%
mutate(Fine = dollar_format()(Fine)) %>%
kable(
booktabs = TRUE,
align = "cllcc",
linesep = ""
)
```
`r caption("Table showing Problem 1 events.","tables")`
# Problem 2
``` {r p2-table}
p2 %>%
filter(PrimaryGroup == params$primarygroup) %>%
mutate(Fine = dollar_format()(Fine)) %>%
kable(
booktabs = TRUE,
align = "clcc",
linesep = ""
)
```
`r caption("Table showing Problem 2 events.","tables")`
.R script to loop over PrimaryGroup values
## ReportLoop.R to run the .Rmd file
filedate <- format(Sys.Date(), "%Y%m%d")
# Run fines.Rmd for each service provider
for (i in unique(primarygroup$PrimaryGroup)) {
rmarkdown::render(input = "fines/fines.Rmd",
params = list(primarygroup = i),
output_file = paste0(filedate, "_", i, "_fines.pdf"))
}
If I manually knit the fines.Rmd
file and iterate on each primarygroup
value, the caption function correctly starts counting at “Table 1.” But if I run the ReportLoop.R script, the function iterates incorrectly as described above. I tried exporting the increment()
function [it wasn’t originally exported, but I wanted to test this] and then adding increment(0)
to the beginning of the .Rmd
document, but that didn’t fix the issue.
Is there some other way I can make this work? Either by modifying the package, the fines.Rmd
markdown file, or the reportloop.R
for loop script?