Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R markdown to PDF - Printing console output

Tags:

r

pdf

r-markdown

I am working in RStudio for a course, and I want to write up my reports with R markdown. I would like to display certain console output in the pdf that will be the report, specifically the output of summary(model) where model is e.g. a linear model obtained by using the command lm. The default seems however to be that the console output, after conversion to pdf with knitr, is displayed as-is and with '#'-symbols in front of it, which is both ugly to me and also quite cumbersome to deal with when tweaking the final layout of the report.

Is there any nicer way to display console output, specifically when converting my notebooks to pdf? Ideally, I would like something like a box surrounding the output (HTML conversion seems to give you just that) and optimally be able to add a caption to state what the output is representing. And most of all, no annoying '#'-signs on every row.

I have tried searching around here and googling for a solution, but I haven't found anything that solves my problem.

like image 689
MonadBoy Avatar asked Apr 08 '18 15:04

MonadBoy


People also ask

How do I display Markdown output?

If you prefer to use the console by default for all your R Markdown documents (restoring the behavior in previous versions of RStudio), you can make Chunk Output in Console the default: Tools -> Options -> R Markdown -> Show output inline for all R Markdown documents .

How do I publish R Markdown as PDF?

To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.

How do I get rid of console output in R?

Ctrl+L — Clear the Console.

How do I create a PDF Markdown?

Having saved your R Markdown file, it's time to process it and generate a PDF file. You can do this by clicking the Knit PDF button above the text. Your PDF file will be generated and opened as shown below.

How do I create a PDF document from R Markdown?

3.3. PDF document. To create a PDF document from R Markdown, you specify the pdf_document output format in the YAML metadata: --- title: "Habits" author: John Doe date: March 22, 2005 output: pdf_document ---. Within R Markdown documents that generate PDF output, you can use raw LaTeX, and even define LaTeX macros.

What is Markdown in R?

Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see . When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.

How do I render a markdown file with RStudio IDE?

The header of 1-example.Rmd shows that it renders to an HTML file by default. The RStudio IDE knit button renders a file to the first format listed in its output field. You can render to additional formats by clicking the dropdown menu beside the knit button: The following output formats are available to use with R Markdown.

Can I use latex in R Markdown?

Within R Markdown documents that generate PDF output, you can use raw LaTeX, and even define LaTeX macros. See Pandoc’s documentation on the raw_tex extension for details. Note that PDF output (including Beamer slides) requires an installation of LaTeX (see Chapter 1). 3.3.1 Table of contents


2 Answers

Here is a workaround. The idea is to transform console output in text that you can plot and custom as you like.

---
title: "Untitled"
output:
  pdf_document: default
---

```{r, echo = F}
print_output <- function(output, cex = 0.7) {
  tmp <- capture.output(output)
  plot.new()
  text(0, 1, paste(tmp, collapse='\n'), adj = c(0,1), family = 'mono', cex = cex)
  box()
}
```

```{r, warning = F}
lm <- lm(mpg ~ hp, data = mtcars)

print_output(summary(lm))
```

which gives: enter image description here

like image 113
DJack Avatar answered Oct 18 '22 07:10

DJack


Here is another approach using knitr hooks. We simply wrap additional LaTeX commands around the chunk output.

TOC:

  1. Basic solution using the latex package framed.
  2. Flexible solution using the latex package fancyvrb

1. Basic solution using the latex package framed.

---
title: "Output Hook"
output: pdf_document
---

```{r setup, include = F}
library(knitr)
opts_chunk$set(comment=NA)
def_hook <- knit_hooks$get("output")
knit_hooks$set(output = function(x, options) {
  out <- def_hook(x, options)
  return(paste("\\begin{framed}\\begin{verbatim}", x, "\\end{verbatim}\\end{framed}", collapse = "\n"))
})
```

```{r}
lm(mpg ~ hp, data = mtcars)
```

enter image description here

2. Flexible solution using the latex package fancyvrb

You could even modify the outcome in a very flexible way using the latex package fancyvrb (see this documentation for options):

---
title: "Output Hook"
output: pdf_document
header-includes:
  - \DefineVerbatimEnvironment{myVerb}{Verbatim}{numbers=left,numbersep=1mm,frame=lines,framerule=0.4mm,rulecolor=\color{blue}}
---

```{r setup, include = F}
library(knitr)
opts_chunk$set(comment=NA)
def_hook <- knit_hooks$get("output")
knit_hooks$set(output = function(x, options) {
  out <- def_hook(x, options)
  return(paste("\\begin{myVerb}\n", x, "\\end{myVerb}", collapse = "\n"))
})
```

```{r}
lm(mpg ~ hp, data = mtcars)
```

enter image description here

like image 20
Martin Schmelzer Avatar answered Oct 18 '22 08:10

Martin Schmelzer