Wrap text around plots in Markdown - r

I am reasking this question:
How to wrap text around plots in R Markdown?:
'Currently the default for R Markdown in R is to have one line of text in line with the plot but this looks very awkward and I would like to save space by having the text wrap around the plot (plot aligned left, with text wrapping on the right).'

It appears there is an easier way now by adding two arguments to the r chunk:
out.width= "65%", out.extra='style="float:right; padding:10px"'
This scales the plot to 65% of the page width and lets text float on the left side with a distance of 10px.

You can use CSS styling to position the elements however you want. However there can be some difficulties and it does require some tweaking. Here is a simple example:
---
title: "Untitled"
author: "Ian Wesley"
date: "April 21, 2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
<div style= "float:right;position: relative; top: -80px;">
```{r pressure, echo=FALSE}
plot(pressure)
```
</div>
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
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. You can embed an R code chunk like this:
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Yields:

Related

Is there a way to add line breaks ONLY when exporting to PDF in R Markdown?

I think the question is quite self-explanatory but for avoidance of doubt I'll explain with more detail below:
I have an R Markdown document that works well if converted to HTML or uploaded to GitHub. When converting to PDF (using Latex), the results are not so pretty. I find that the biggest problem in a Latex PDF document are line breaks. I can fix the line breaks issue on the PDF document by adding "\ " characters, but that throws my HTML document out of whack too.
Is there a way to manually add line breaks (or "space before/after paragraphs") for the PDF output only?
Thank you!
You can redefine the relevant spacings in the YAML header. \parskip controls the paragraph spacing. Code blocks are shaded using a snugshade environment from the framed package. We can also redefine the shaded environment for code blocks to have some vertical space at the start. Here's a reproducible example. Note: I also added the keep_tex parameter so you can see exactly what the generated tex file looks like, in case this is useful:
title: "test"
author: "A.N. Other"
header-includes:
- \setlength{\parskip}{\baselineskip}
- \renewenvironment{Shaded}{\vspace{\parskip}\begin{snugshade}}{\end{snugshade}}
output:
pdf_document:
keep_tex: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
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. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
Once you output to HTML, you can just print the HTML webpage as PDF. that might be an easy way keep the original format

How to align table left in HTML output using R Markdown and Knitr

I am experimenting with Knitr/KableExtra in RStudio but cannot make my tables use the full width of the web browser or control the table alignment on the screen.
Below is an example of the code where as per the kable_styling documentation, I have tried force the table to align to the left of the screen but in the html output, the table is always centered. It sees that there is an invisible margin to the left that I can't use. The problem arises when I have a table with more fields....the large margin on the left remains, forcing the table to extend to the right of the screen and generate a horizontal scrollbar - very annoying and ugly.
Is there some way I can use the space on the left margin or force the table to align truly to the left?
Here is an example of the issue:
---
title: "Untitled"
author: "ME"
date: "2/4/2020"
output: html_document
---
```{r setup, include=FALSE}
library(kableExtra)
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
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. You can embed an R code chunk like this:
```{r cars}
x_html <- knitr::kable(head(mtcars), "html")
kable_styling(x_html, "striped", position = "left", font_size = 7)
```
You need to tweak the default CSS theme. For example, to make the content display on 100% of the available width:
```{css}
.main-container {
max-width: 100%;
}
```
There are other solutions but this one is probably the most easy:
---
title: "Untitled"
author: "ME"
date: "2/4/2020"
output: html_document
---
```{r setup, include=FALSE}
library(kableExtra)
knitr::opts_chunk$set(echo = TRUE)
```
```{css}
.main-container {
max-width: 100%;
}
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
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. You can embed an R code chunk like this:
```{r cars}
x_html <- knitr::kable(head(mtcars), "html")
kable_styling(x_html, "striped", position = "left", font_size = 7)
```
I think another way to fill in the left-hand side blank is to add Floating TOC option (right under html_document), which will illustrate the table content of Rmarkdown (given that you used headers).
output:
html_document:
toc: true
toc_float: true
---
Kate

R Markdown excessive white space PDF output

Does anybody know why R markdown generates excessive white space above each plot and how can I fix that ? Is there a knitr option to be included ? Or any chunk option maybe ?
I've provided 3 images at the bottom of this post so that you can see what I mean.
Haven't used any other chunk option than echo, warning and message and about the plot it is a basic ggplot.
Let me know in the comments if I need to provide any code example of my chunks for a better view.
Edit: here's a simple rmarkdown file generating the same excessive white space above ggplots.
---
title: "Untitled"
author: "Razvan Cretu"
date: "January 9, 2019"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r import, echo=FALSE}
library('ggplot2')
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
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. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
ggplot(cars, aes(speed, dist))+
geom_line()
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

RMarkdown - Change Inline Code Color

I am using inline code in RMarkdown and I would like all the text that is a result of inline code to be a different color in the document. In this example, I would like heat.colors to be red all over the document. Is there a way to do this?
Or you can use text_spec in kableExtra. It literarily does the same thing but just a tiny bit more literal. See more here
---
title: ''
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(kableExtra)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
This is inline code: `r text_spec(colnames(mtcars)[1], color = "red")`.
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. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
### This is more inline code `r text_spec(colnames(mtcars)[2], color = "red")`.
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
You can do something like:
---
title: ''
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{css echo=FALSE}
.custom-inline {
color: red;
font-weight: 700
}
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
This is inline code: `r sprintf("<span class='custom-inline'>%s</span>", colnames(mtcars)[1])`.
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. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
### This is more inline code `r sprintf("<span class='custom-inline'>%s</span>", colnames(mtcars)[2])`.
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
to get:
The default templates do not wrap inline chunks in a classed <span> tag so you have to do it manually. You can make a function to do it, too.

Inserting logo into beamer presentation using R Markdown

I am trying to insert logo into beamer presenation using Rmarkdown, and it looks like size controls in \logo{\includegraphics[height=1cm,width=3cm]{logo.png}} do not work, no matter what values I put there, image is always of the same size. Any suggestions besides modifying image manually?
---
title: "Presentation"
author: "Author"
output:
beamer_presentation:
includes:
in_header: mystyle.tex
---
## R Markdown
This is an R Markdown presentation. Markdown is a simple formatting
syntax for authoring HTML, PDF, and MS Word documents. For more
details on using R Markdown see <http://rmarkdown.rstudio.com>.
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.
## Slide with Bullets
- Bullet 1
- Bullet 2
- Bullet 3
## Slide with R Code and Output
```{r}
summary(cars)
```
## Slide with Plot
```{r, echo=FALSE}
plot(cars)
```
This is mystyle.tex
\logo{\includegraphics[height=1cm,width=3cm]{logo.png}}
\usetheme{Madrid}
\usefonttheme{serif}
\institute{Institute}
\setbeamertemplate{navigation symbols}{}
UPDATE: Quick work around - simply modifying image will not work - image is ugly and pixelated. Simply converting to pdf also didn't work well, so I used following R code to create pdf and use it in \logo{\includegraphics{logo.pdf}}
library(png)
library(grid)
img <- readPNG('logo.png')
img <- rasterGrob(img, interpolate=TRUE)
pdf(file = 'logo.pdf', width = 1, height = 0.25)
grid.newpage()
grid.raster(img)
dev.off()
I found solution; in beamer manual there is another way of using logo function and it works fine.
\pgfdeclareimage[height=0.2787cm, width=2.5cm]{logo}{logo.png}
\logo{\pgfuseimage{logo}}
I found this beamer tutorial quite useful. Just add the following to the file mystyle.tex passed to the YAML option in_header (as shown in the question):
\usepackage{tikz}
\titlegraphic {
\begin{tikzpicture}[overlay,remember picture]
\node[left=0.2cm] at (current page.30){
\includegraphics[width=3cm]{Beamer-Logo}
};
\end{tikzpicture}
}
and then you can play around with the node parameters to adjust the placement of your logo (Beamer-Logo).

Resources