简体   繁体   中英

Not able to produce paged pdf document when trying to render html_paged rmarkdown document using parameters

I am trying to create a parametric html_paged document. When I use render function to render the .Rmd file. It creates the paged html document but it doesn't create paged pdf document. But when I use the knit button. Then it can create both paged html doc and pdf doc.

As I want to make it parametric report, that's why I prefer the render function. Is there a way to produce a paged pdf document using render function. Below is the reproducible example:

---
title: "A Multi-page HTML Document"
author: "Yihui Xie and Romain Lesur"
date: "`r Sys.Date()`"
output:
  pagedown::html_paged:
    toc: true
    # change to true for a self-contained document, but it'll be a litte slower for Pandoc to render
    self_contained: false
# uncomment this line to produce HTML and PDF in RStudio:
knit: pagedown::chrome_print

params:
  test: 'html' 
---

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

# Introduction

This is an example of a multi-page HTML document. See https://pagedown.rbind.io for the full documentation. The rest of this document is random text.

# Random text

```{r, results='asis', echo = FALSE}
random_markdown = function(len = 100) {
  uri = knitr::image_uri(file.path(R.home('doc'), params$test, 'logo.jpg'))
  text = function(len) {
    trimws(paste(sample(c(letters, rep(' ', 10)), len, TRUE), collapse = ''))
  }
  id = function() paste(sample(letters, 8, TRUE), collapse = '')
  figure = function() {
    sprintf('![(#fig:%s)The R logo.](%s){width=%d%%}', id(), uri, sample(20:60, 1))
  }
  tab = paste(knitr::kable(head(mtcars[, 1:5])), collapse = '\n')
  table = function() {
    c(sprintf('Table: (#tab:%s)A table example.', id()), tab)
  }
  unlist(lapply(seq_len(len), function(i) {
    if (i %% 20 == 0) return(paste('#', text(sample(10:30, 1))))
    if (i %% 10 == 0) return(paste('##', text(sample(10:30, 1))))
    type = sample(1:3, 1, prob = c(.9, .03, .07))
    switch(type, text(sample(50:300, 1)), figure(), table())
  }))
}
cat(random_markdown(), sep = '\n\n')
```

Now, if I render it using render function.

rmarkdown::render("reprex.Rmd", params=list(test="html"))

It would create paged html output but it wont't create paged pdf document.
I am using the latest rmarkdown (version 2.3) and pagedown (version 0.11) packages.

I found the solution here :

As per the suggestion, currently chrome_print doesn't support passing of parameters. This feature has been requested here . For now, I had to create parameterized paged pdf report in a two step process like below, which seems to work fine.

output <- rmarkdown::render("reprex.Rmd", params=list(test="html"))
pagedown::chrome_print(output)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM