Avoid spilling of text to margin when using R markdown and knitr - r

While generating a pdf file using rmarkdown and knitr, how to avoid spilling of text of R console output to the margins? For example,
---
title: "Illustration"
author: "Temp"
date: "Monday, April 06, 2015"
output: pdf_document
---
Spilling of R output.
```{r}
library("tm")
data("acq")
str(acq)
```

I have found the most consistent way for me to deal with this is to use the width and strict.width options to the str() function. The width option specifies the page width to be used and is supposed to inherit from the active options() setting for width. But, I haven't always found this consistent. The strict.width allows you to control how the excess text is handled (cut, wrapped).
---
title: "Illustration"
author: "Temp"
date: "Monday, April 06, 2015"
output: pdf_document
---
Spilling of R output.
```{r}
library("tm")
data("acq")
str(acq,width=80,strict.width="cut")
```

Related

`knitr::include_url` ignored in output

After updating R to version 3.6.0, and re-installing the required packages, knitting Rmd documents now seems to ignore the knitr::include_url function.
The following is a minimal example, in a file named test.Rmd -
---
title: "test"
author: "Michael Dorman"
date: "May 1, 2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
```{r}
knitr::include_url("https://www.wikipedia.org/")
```
```{r}
knitr::include_graphics("https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png")
```
And here is a screenshot of the output, after pressing Knit in Rstudio -
The knitr::include_url function is ignored. No <iframe> element was produced. The knitr::include_graphics function still works, so the image does appear in the output.
Thanks for reading, I appreciate any help!

Rmarkdown beamer: How to left justify the title page

How is it possible to align the title page of my Rmarkdown beamer presentation to the LEFT instead of the default center?
Example , default is center:
---
title: "Untitled"
author: "S SS"
date: "2/4/2018"
output: beamer_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## 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.
You can switch to a left aligned title page by using
\setbeamertemplate{title page}[default][left]
To use this in rmarkdown, place this line in a file called preamble.tex and use
---
title: "Untitled"
author: "S SS"
date: "2/4/2018"
output:
beamer_presentation:
includes:
in_header: preamble.tex
---
test
Edit:
with an up-to-date version of markdown, you can also directly do
---
title: "Untitled"
author: "S SS"
date: "2/4/2018"
output:
beamer_presentation
header-includes:
- \setbeamertemplate{title page}[default][left]
---
test
Here's a hack until someone comes along with a better answer: We'll dispense with a standard beamer title slide and just format the first slide as if it were a title slide:
---
output: beamer_presentation
header-includes:
- \usepackage{color}
---
\color{blue}
\LARGE{\textbf{This is the title}}
\color{black}
\vspace{0.2cm}
\large{Author Name}
\large{February 4, 2018}
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## 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.

R Markdown, numbering not showing for figure captions in HTML and Word

I am putting together an R Markdown document in HTML, and I have the following YAML:
---
title: "R Markdown Example"
author: "Me"
date: "October 30, 2017"
output:
html_document:
fig_width: 7
fig_height: 6
fig_caption: true
code_folding: "show"
---
I am trying to insert a figure with auto-numbered caption with the following code:
```{r, fig.cap="Figure caption \\label{fig_1}"}
plot(mtcars$hp, mtcars$mpg)
```
But the caption number won't show up. When I try to reference the image in the text
(Figure \ref{fig_1})
I just get:
(Figure )
I have the same problem. This only happens for Word and HTML outputs, so I think I may be using LaTeX documentation instead of the proper input for these types. I've tried a lot of the different recommendations for figure captions, but I can't seem to get any of it to work.
I needed to output to Word to adhere to a journal that only accepted Word... So I ended up using the captioner package. This should also work with html and pdf.
library(captioner)
fig_nums <- captioner()
In my r chunk I put my plot code and then the caption comes directly after the chunk in ticks, using the fig_nums function ``:
```{r}
plot(pressure)
```
`r fig_nums("pressure-plot", "pressure against temperature")`
It looks like this in the Word output:
Then I can refer to it in text like this:
As you can see from the nice visualisation in r fig_nums("pressure-plot", display = "cite") the pressure rises as
temperature rises.
This will show as:
As you can see from the nice visualisation in Figure 1 the pressure rises as
temperature rises
Be sure to run the first time you use, and check-out for more info:
install.packages("captioner")
vignette("using_captioner")
You can use the bookdown::html_document2 format.
---
title: "Untitled"
output: bookdown::html_document2
---
```{r pressure, echo=FALSE, fig.cap='test plot'}
plot(pressure)
```
\#ref(fig:pressure)

Why figure captions are not displayed in my rmarkdown documnet?

I am using rmarkdown and knitr
I have two questions:
Q1: I am not sure why the figure caption does not appear when I build my rmarkdown document.
Here is an example. I get the figure there but with not the figure caption!
---
title: "Untitled"
author: "XXXX"
date: "9 September 2016"
output: pdf_document
---
## R Markdown
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:
![figure1 caption](my photo.png)
Q2: I am developing an R-package using RStudio. I am able to create an html_vignette using the code below. However, I am not sure why I am not able to create a PDF vignette when I change the output to output: rmarkdown::pdf_vignette? The latter gives an error:
Error: 'pdf_vignette' is not an exported object from 'namespace:rmarkdown'
Execution halted
---
title: "TITLE XXX"
author: "AMT"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{package-intro}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
I am new to this area, so if somebody can explain that would be great.

Adding a caption to a Kable Table in R Markdown

I am using RMarkdown to write some university work
I have created a table using the code below and need to export it to Microsoft Word
Can anyone tell me how to add a label describing the data in the table or do I have to do this in MS Word afterwards
---
title: "Data"
author: "foo"
date: "2 July 2016"
output:
word_document:
bibliography: bibliography.bib
---
kable(table(Rawdata$Var1, Rawdata$Var2))
Use the caption argument in kable.
If you want more options for formatting tables in Word output, I would recommend downloading the Gmisc package and using the Gmisc::docx_document format. Just keep in mind that it will save the file as an HTML file that can be opened as a Word Document and preserve the formatting.
For example:
---
title: "Data"
author: "foo"
date: "2 July 2016"
output: Gmisc::docx_document
---
```{r}
knitr::kable(head(mtcars[, 1:3]),
caption = "This is the table caption")
```

Resources