Closed jtrac1 closed 7 months ago
Hi,
pdf_document use LaTeX to create a pdf document
What we do in pagedreport
is to compute an html report. After that we print the html into pdf using pagedown::chrome_print
(as you can see in the YAML).
You should render to html. And after that load the html to chrome_print
. It will output a pdf.
If I run with this output format, I get an error. If I remove output format, it outputs to an html. I'm running this in an R script file.
reports <- tibble( input = "MonthlyReports.Rmd", output_format = "pagedown::chrome_print", output_file = stringr::str_c("reports/", survey, "-Monthly-Reports-", month, year, ".html"), params = map(survey, ~list(survey = .)) )
reports %>% pwalk(rmarkdown::render)
Error:
Error in file.exists(input) : argument "input" is missing, with no default
Here is the YAML of my Rmd file:
--- title: "Monthly
r params$surveySurvey Report" subtitle: "
r lubridate::month(lubridate::mdy(params$month_start_date), label=TRUE, abbr=FALSE)`r lubridate::year(lubridate::mdy(params$month_end_date))
"
author: "Author"
r Sys.Date()
"params: survey: "Phone" year_start_date: "08/01/2020" month_start_date: "04/01/2021" month_end_date: "04/30/2021" output: pagedreport::paged_grid: logo: "logo.jpg" knit: pagedown::chrome_print main-color: "#800000" secondary-color: "#183d68" google-font: TRUE main-font: "Roboto" header-font: "Roboto" ---`
In fact pagedown::chrome_print
comes after the render and takes the html file which was rendered as an input. It's not an output format.
I would do something like
my_function <- function(x, output_dir_html, output_dir_pdf) {
rmarkdown::render(
"my_file.Rmd",
output_format = "pagedreport::windmill",
output_dir = output_dir_html,
output_file = paste0(x, ".html"),
params = list(param1= x)
)
# to PDF
pagedown::chrome_print(paste0(output_dir_html, x, ".html"),
output = paste0(output_dir_pdf, x, ".pdf"))
}
walk(list_of_params, my_function, "dir_html", "dir_pdf")
@tvroylandt thanks so much for this reply. I had a similar issue to what @jtrac1 described and your reply helped me troubleshoot. I adapted your solution to make it work for my case. My code is shown below. Maybe my code will help someone else parameterize pdf pagedreports. In the reprex below, I'm producing parameterized pagedreports for each mpg$class in the mpg dataset.
reprex: save "test.Rmd" and "run_test.R" as separate files in the same folder run "run_test.R" to produce parameterized versions of "test.Rmd" in .pdf format
##################################################################### test.Rmd #####################################################################
---
title: "`r params$param1`"
output:
pagedreport::paged_hazelnuts:
front_img: "https://images.pexels.com/photos/717988/pexels-photo-717988.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260"
back_img: "https://images.pexels.com/photos/3303615/pexels-photo-3303615.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"
img_to_dark: TRUE
knit: pagedown::chrome_print
params:
date: !r format(Sys.Date(), "%B %d, %Y")
param1: x
---
## Fuel economy for `r params$param1`
```{r, echo=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
class <- mpg %>% filter(class == params$param1)
ggplot(class, aes(displ, hwy)) +
geom_point() +
geom_smooth(se = FALSE) # remove # from the following line; required for the code chunk to format for github
#```
## Updated: `r params$date`
##################################################################### run_test.R #####################################################################
library(tidyverse)
library(rstudioapi)
# establish which parameter we need to iterate the report over
reports <- tibble(
class = unique(mpg$class),
filename = stringr::str_c(tolower(class)),
params = purrr::map(class, ~list(x=.))
)
reports
# grab filepath that holds our 'parent' .Rmd file
dir_script <- dirname(rstudioapi::getSourceEditorContext()$path)
dir_script
# parameters for reports
x <- reports$filename
my_function <- function(x, output_dir_html, output_dir_pdf) {
rmarkdown::render(
paste0(dir_script, "//","test.Rmd"),
output_format = "pagedreport::paged_hazelnuts",
output_dir = output_dir_html,
output_file = paste0(x, ".html"),
params = list(param1= x)
)
# to PDF
pagedown::chrome_print(paste0(output_dir_html, "//", x, ".html"),
output = paste0(output_dir_pdf, "//", x, ".pdf"))
}
# make pdf parameterized pagedreports
walk(reports$params, my_function, dir_script, dir_script)
I'm trying to use knit parameterized reports to the paged_grid format, but I'm getting a pandoc error. When I tried using "pdf_document", it wasn't knitting to the pagedreport format, so I'm wondering what to put as the correct format. I also tried changing the output_file to .html, which ended up creating a file with the paged_grid template, but it didn't save as a pdf.
Create output data frame Must specify an output format if using pdf, otherwise you'll get a pandoc error https://github.com/rstudio/rmarkdown/issues/1569 "pdf_document" doesn't use the pagedown formatting, and this attempt below returns an error
reports <- tibble( input = "Report.Rmd", output_format = "pagedreport::paged_grid", output_file = stringr::str_c("reports/", survey, "-Monthly-Reports-", month, year, ".pdf"), params = map(survey, ~list(survey = .)) )
Codebased way to knit (render) iteratively pwalk walk across each row, do something, then go to the next row
reports %>% pwalk(rmarkdown::render)
Error message:
Error: pandoc document conversion failed with error 4