尝试使用参数呈现html_paged rmarkdown文档时无法生成分页的pdf文档

问题描述

我正在尝试创建一个参数化html_paged文档。 当我使用渲染功能渲染.Rmd文件时。它创建分页的html文档,但不创建分页的pdf文档。 但是当我使用编织按钮时。然后,它可以创建分页的html doc和pdf doc。

因为我要使其成为参数化报告,所以才选择渲染功能。有没有一种使用渲染功能生成分页pdf文档的方法。下面是可复制的示例:

---
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,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('##',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')
```

现在,如果我使用渲染功能对其进行渲染。

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

它将创建分页的html输出,但不会创建分页的pdf文档。
我正在使用最新的rmarkdown(2.3版)和pagedown(0.11版)软件包。

解决方法

我找到了解决方法here

根据建议,当前chrome_print不支持参数传递。 here已请求此功能。 现在,我必须按照如下所示的两步过程来创建参数化的分页pdf报告,看来工作正常。

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