Adding a caption to a Kable Table in R Markdown - r

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")
```

Related

How to Center Figure Caption in RMarkdown Word Output

I cannot figure out how to center a figure caption in RMarkdown with MS Word output. I'm pointing the Markdown document to a reference Word template, and the figure captions in the template are correct. The problem is that RMarkdown doesn't know that the captions are supposed to be captions. It's treating them as just normal text.
---
title: "Title"
author: "Author"
date: "June 5, 2020"
output:
bookdown::word_document2:
fig_caption: yes
reference_docx: Template.docx
subsubtitle: Test
subtitle: Subtitle
---
# My Plots
Sample Plot
{r pressure,fig.cap="Sample Caption", echo=FALSE}
plot(pressure)
How do I tell RMarkdown that "Sample Caption" is actually figure text and not just normal text?
Try specify fig.id = "pressure" and it should generate automatically numbered captions.

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)

How to remove compact title from R markdown to latex conversion?

I wrote my own titlepage and it is loaded via an include in the R-markdown file. However, this conflicts with the pandoc title. I am trying to find settings in the R markdown yaml header such that pandoc does not insert the following code snipped into the tex-file.
% Create subtitle command for use in maketitle
\newcommand{\subtitle}[1]{
\posttitle{
\begin{center}\large#1\end{center}
}
}
\setlength{\droptitle}{-2em}
\title{}
\pretitle{\vspace{\droptitle}}
\posttitle{}
\author{}
\preauthor{}\postauthor{}
\date{}
\predate{}\postdate{}
There is no clear indication in the pandoc documents or the r markdown guidelines how to disable the title generation. Any help would be appreciated.
Update: In particular, I am looking for solutions that allow me to keep creating my title page with the \maketitle command. That is why I focussed on this particular code snipped that I want to get rid of.
I also use my own title page with rmarkdown documents for latex/pdf outputs. To remove the title, you can add the following command in a text file called with in_header :
\AtBeginDocument{\let\maketitle\relax}
A reproductible example with the header.tex file built directly within the Rmd document:
---
title: "RMarkdown No title Test"
author: "StatnMap"
date: "July 30, 2017"
output:
pdf_document:
includes:
in_header: header.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r rm_title_page, echo=FALSE}
head <- cat('
\\AtBeginDocument{\\let\\maketitle\\relax}
', file = "header.tex")
```
# Title 1
**Some text**
# Title 2
**Some text**
Using compact-title: false in the YAML works.
---
title: "This title is not compact"
author: "Test"
date: "2019 May 10"
output: pdf_document
compact-title: false
---
I had the same problem today. Here's what I did. (Maybe I'll update the solution when I come up with something better.)
The solution is dumb but useful. I can't set an arbitrary space between the lines now, because I used \newline.
---
title: "\\huge My Smart Title"
author: "\\newline \\Large My Smart Author"
date: "\\newline \\Large 2018-12-25"
output:
pdf_document:
includes:
in_header: preamble.tex
latex_engine: xelatex
---
Below are the outputs before and after the solution.
BEFORE:
AFTER:
Note:
You may be confused about the different sizes of the "author" and the "date" in the two pictures above, if you don't know that the fontsize of the "author" and the "date" is \large instead of \Large by default.
END

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.

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

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")
```

Resources