In R Studio, my R Markdown document begins like this:
---
title: 'ST 412: Homework 3'
author: "Camden White"
documentclass: amsart
geometry: margin=1in
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## 1. Read Data Set and Define Variables
```{r, message=FALSE, warning=FALSE}
library(tidyverse)
library(magrittr)
```
When I do not include documentclass: amsart, everything works fine, but when I do include it, the library section is placed before the ## 1. Read Data Set and Define Variables header.
When there is text between the two components, the order is the same order in the code, but without it, the library section comes first. This does not happen with the default article document class, and I do not know why this occurs. How can I use the amsart document class and fix this ordering issue?
The problem seems to be that a subsection in amsart is basically what a paragraph is in normal classes: an unnumbered bold piece of text without an new paragraph after it. The text will just continue in the same line. As the grey box with the source code spans a whole line, it can't be placed there and shows up above. You can reproduce this with normal article class like this:
\documentclass{article}
\usepackage{framed}
\begin{document}
\paragraph{1. Read Data Set and Define}
\begin{framed}
test
\end{framed}
\end{document}
You can avoid the problem by making sure the source code isn't the first text in the subsection, e.g. by adding something invisible:
---
title: 'ST 412: Homework 3'
author: "Camden White"
documentclass: amsart
geometry: margin=1in
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## 1. Read Data Set and Define Variables
\mbox{}
```{r, message=FALSE, warning=FALSE}
library(tidyverse)
library(magrittr)
```
Related
I'm need to generate a "bunch" of tables in R that must be included in a Latex document.
I can create very nice tables using flextable... but I am not able to generate ANY format that can then be inserted in (multiple places) in a latex document.
The problem is that any method that I try will add extra "margins" (like in pdf... prints a table to a full page) or has some limitation (like png).
Does any ne has a solution, either using directly an RScript or trough knitting R-Markdown?
(Note: The solution of producing the whole latex doc in RMarkdown is Not feasible.)
Just to present an example. I would like this would produce a Full-Page (with dimensions of the table... not A4) of the table.
Thank you very much for your Help :).
---
title: "Untitled"
output:
pdf_document:
latex_engine: xelatex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(flextable)
```
```{r}
ft <- flextable(head(airquality))
ft <- autofit(ft)
theme_vader(ft)
```
You could use the standalone class (a bit of extra space is still there, but much less than a full page):
---
documentclass: standalone
classoption: varwidth
output:
pdf_document:
latex_engine: xelatex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(flextable)
```
```{r}
ft <- flextable(head(airquality))
ft <- autofit(ft)
theme_vader(ft)
```
How can I add an empty code chunk in R markdown? I have found several options to manipulate the html to give more white space. But I would like to present some empty lines in the well known gray code box in order to indicate space for assigments.
---
title: "Untitled"
author: "Author"
output: html_document
---
## R Markdown
```{r cars}
summary(cars)
```
## Homework
Please calculate the mean of the `speed` variable in `cars`.
```{r}
```
A hacky way... almost there:
```{r, code="'\n\n\n\n'", results=F}
```
A possible solution using results = 'asis' and relying on chunck HTML class:
```{r, results='asis', echo=F}
cat(
'<pre class="r">
<code class = "hlsj"> <span class="hljs-string"> <br> <br> </span> </code>
</pre>
')
```
Just add <br> to increase the number of lines.
There does not seem to be a way to get knitr to recognise a completely empty chunk as a chunk. It will always omit it, regardless of the chunk options.
You have to insert something to get it to render, for example a comment. So you can put the empty lines between two comments:
---
output: html_document
---
## Homework
Please calculate the mean of the `speed` variable in `cars`.
```{r}
# Insert code here
# End
```
Or with the strip.white=FALSE chunk option we can use a single comment line, but strangely this only works for leading, not trailing, whitespace:
---
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(strip.white = FALSE)
```
## Homework
Please calculate the mean of the `speed` variable in `cars`.
```{r}
# Insert code above
```
Consider the following R Markdown document:
---
title: "Stack Overflow Question"
author: "duckmayr"
date: "6/21/2019"
output: pdf_document
header-includes:
- \usepackage{setspace}
- \doublespacing
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is some example text.
I want all the body text to be double-spaced,
but I want all echoed code from code chunks to be single spaced.
In other words, not this:
```{r}
## This code is double-spaced.
## I want it to be single spaced.
## How can I do that?
```
Is there a canned or relatively painless way to have all normal text double-spaced, but have all code echoed from code chunks single spaced? I tried consulting the guide to chunk options here, but couldn't quite find what I was looking for.
If you are outputting to pdf the most painless way might be adding some LaTeX commands to your Rmd document:
---
title: "Stack Overflow Question"
author: "duckmayr"
date: "6/21/2019"
output: pdf_document
header-includes:
- \usepackage{setspace}
- \doublespacing
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is some example text. I want all the body text to be double-spaced, but I
want all echoed code from code chunks to be single spaced. In other words, not
this:
\singlespacing
```{r}
## This code is double-spaced.
## I want it to be single spaced.
## How can I do that?
```
\doublespacing
Some additional body text. Nor hence hoped her after other known defer his.
For county now sister engage had season better had waited. Occasional mrs
interested far expression acceptance. Day either mrs talent pulled men
rather regret admire but. Life ye sake it shed. Five lady he cold in meet up.
Alternatively, you could define a new chunk option using knitr chunk hooks. For instance, you could include in the setup chunk:
```{r setup, include=FALSE}
hook_chunk = knitr::knit_hooks$get('chunk')
knitr::knit_hooks$set(chunk = function(x, options) {
regular_output = hook_chunk(x, options)
# add latex commands if chunk option singlespacing is TRUE
if (isTRUE(options$singlespacing))
sprintf("\\singlespacing\n %s \n\\doublespacing", regular_output)
else
regular_output
})
knitr::opts_chunk$set(echo = TRUE, singlespacing = TRUE)
```
Some useful references:
Hooks - Customizable functions to run before/after a code chunk, tweak the output, and manipulate chunk options
How to Create New Chunk Options in R Markdown
I'm having a lot of trouble getting basic references to work in R Markdown. To reduce complexity from my original project, I've decided to use the bookdown example code, but I'm experiencing the same problem. Here's a link to the intro exmample code: https://github.com/rstudio/bookdown-demo/blob/master/01-intro.Rmd
When I use Knitr to HTML or PDF the file is being generated fine but the references are not working, instead the file will just containt "#ref(example)". Here is an image to show better the output (my emphasis added in red):
Direct link to image: https://i.imgur.com/2yxB5h3.png
Here is a minimal example:
---
title: "Minimal"
output:
pdf_document:
fig_caption: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is a reference to the plot below \#ref(fig:minGraph)
```{r minGraph, echo=FALSE, fig.cap="\\label{fig:minGraph}test"}
plot(x=1)
```
With the output appearing as such:
https://i.imgur.com/J3UECqn.png
If you want make use of the bookdown extensions in a normal rmarkdown document you can use bookdown::html_document2 and bookdown::pdf_document2 instead of rmarkdown::html_document and rmarkdown::pdf_document. Example:
---
title: "Minimal"
output:
bookdown::html_document2:
fig_caption: yes
bookdown::pdf_document2:
fig_caption: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is a reference to the plot below \#ref(fig:minGraph)
```{r minGraph, echo=FALSE, fig.cap="test"}
plot(x=1)
```
Looks like I was getting my syntax confused by reading the bookdown guide while using just R markdown. Thanks to Ralf for pointing me in the this direction. The correct minimal code would be like so:
---
title: "Minimal"
output:
pdf_document:
fig_caption: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Here is a reference to the plot below \ref{fig:minGraph}
```{r minGraph, echo=FALSE, fig.cap="\\label{fig:minGraph}test"}
plot(x=1)
```
Often when I include r code in a rmarkdown pdf presentation, I want to use the space of the whole slide and therefore want to present the code and the output side-by-side.
In pure LaTex I would go for \begin{columns}...\end{columns} and then include the code/output manually using lstlistings or some other code-highlighting library. But that proves tedious if I exceed a couple of code examples.
Now I want to use RMarkdown for the presentations and would like to achieve a similar result.
However, the following code throws an error:
## This is slide 1
\begin{columns}[t]
\begin{column}{0.5\textwidth}
```{r, eval=F}
plot(1:10)
```
\end{column}
\begin{column}{0.5\textwidth}
```{r, echo=F}
plot(1:10)
```
\end{column}
\end{columns}
Leaving out the knitr code-chunks and replacing them with text works.
I am aware that it has something to do with the pandoc engine (see here), but wanted to ask if anybody has found a way around this issue.
Well, I may should have looked with a wider focus.
Here is a solution that works for Python, but can easily be adapted to Rmarkdown: https://stackoverflow.com/a/26069751/3048453
I ended up with this code:
in header.tex
\newcommand{\columnsbegin}{\begin{columns}}
\newcommand{\columnsend}{\end{columns}}
in presentation.Rmd
---
title: "Two-column codes in RMarkdown"
author: "My Name"
date: "February 4, 2017"
output:
beamer_presentation:
includes:
in_header: header.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Testslide with Columns
\columnsbegin
\column{.5\textwidth}
```{r, eval=F}
plot(mtcars[, 1:3])
```
\column{.5\textwidth}
```{r, echo=F}
plot(mtcars[, 1:3])
```
\columnsend
Which results in this