简体   繁体   中英

How do I convert my RMarkdown report from HTML to a PDF?

I want to change the format of my R Markdown report from HTML to a PDF. I have looked thoroughly online and tried multiple scenarios but none have worked for me. If someone could describe the process in the most simplest way possible that would be greatly appreciated (disclaimer I have seen every question already related to this topic on this site but nothing helped) .

Is there a way that I can just input the relevant code to save it to my local documents as a PDF?

Below is the code I am using to format my current R Markdown report -

---
title: Transparency return 2019
author: 
date: November 21, 2019
output:
  prettydoc::html_pretty: 
    theme: leonids
    highlight: github
---
{r knitr_init, echo=FALSE, cache=FALSE, warning=FALSE}

library(knitr)
library(rmdformats)

## Global options

options(max.print="75")
opts_chunk$set(echo=FALSE,
                 cache=TRUE,
               prompt=FALSE,
               tidy=TRUE,
               comment=NA,
               message=FALSE,
               warning=FALSE)
opts_knit$set(width=75)
{r}
library(knitr)
library(rmdformats)
library(ggplot2)
library(dplyr)
library(magick)
library(tidyverse)
library(hrbrthemes)
library(kableExtra)
options(knitr.table.format = "html")
chris <- read.csv("Table set 1.csv")

kable(chris[1:21, 1:6]) %>%
  kable_styling("striped", "condensed", font_size = 8) %>%
  pack_rows("Full Time", 1, 7) %>%
  pack_rows("Part Time", 8, 15) %>%
  pack_rows("Apprenticeship", 16, 21)

Simply choose a PDF output format, and don't tell knitr to use html format tables (as you do in options(knitr.table.format = "html") ).

The report will be formatted differently, but there are different PDF formats to choose from, so you may be able to find one that suits you:

  • pdf_format is the standard simple format, but it allows you to choose any LaTeX styling you like.
  • tufte::tufte_book styles it like Edward Tufte's books
  • various presentation styles

and there are probably others I don't know about.

You can also use Pandoc to convert your HTML output to PDF (via an intermediate format like LaTeX), but this doesn't look as good as the formats above.

Finally, your browser may have a "Print to PDF" output option; that might be acceptable, but I find it usually isn't.

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