I'm trying to render a pander table in RMarkdown that includes superscript. Here is my current RMarkdown code:
```
---
title: "My Table"
output: html_document
---
```{r packages, include = FALSE}
library(tidyverse)
library(pander)
```
```{r my_table}
my_table <-
data.frame(
c("Sector 1",
"Sector 2",
"Sector 3"),
c("100%",
"29%",
"NA"))
pander(my_table,
col.names = c("Sector", "% Coverage"),
split.cells = 40,
keep.line.breaks = TRUE)
```
And here is the knit result:
I would like to add a superscripted letter "c" to the "NA" cell of the table, yielding an output like this:
You are looking to use the notation $$ NA^c $$, but this only works outside of the R chunk at the moment.
Related
I want to change the numbering of the tables in the RMarkdown document so that all tables in the appendix have an "A-" in front of the number, thus: "Table A-2".
Only in the appendix. Otherwise with normal numbering ("Table 1").
However, I am not really getting anywhere.
Here is my reproducible example:\
---
title: "This is my title"
date: "`r Sys.setlocale(locale = 'English') ; format(Sys.time(), '%B %d, %Y')`"
output: pdf_document
---
```{r echo = F, message = F, warning = F}
library(tidyverse)
library(knitr)
``` #The hash mark must be removed!
# Results
```{r echo = F, message = F, warning = F}
tribble(~column1, ~column2,
"value1", 2,
"value2", 5
)%>%
kable(booktabs=T, caption = "This is the caption of the first table")
```
# Appendix
```{r echo = F, message = F, warning = F}
tribble(~column1, ~column2,
"value1", 6,
"value2", 8
)%>%
kable(booktabs=T, caption = "This is the caption of the second table")
```
This is really a LaTeX question, and I found the answer here.
You add these LaTeX lines after your Appendix title:
\setcounter{table}{0}
\renewcommand{\thetable}{A\arabic{table}}
I'm super new to R and I'm struggling to create a header above the column names.
colnames(system_table) <- c("System 1", "System 2", "System 3","System 4")
I want to add a title above these that says "All Systems" but I can't seem to find the function to do so.
You can use something like this in Rmarkdown:
---
title: "Test"
output: html_document
---
```{r, echo=FALSE, warning=FALSE, message=FALSE}
library(magrittr)
library(knitr)
library(formattable)
system_table <- data.frame(v1 = c(1,2,3),
v2 = c(3,2,5),
v3 = c(5,3,2),
v4 = c(3,2,1))
colnames(system_table) <- c("System 1", "System 2", "System 3","System 4")
formattable(system_table) %>%
kable("html", escape = F, caption = "All Systems")
```
Output:
The section of the bookdown manual on generating figures demonstrates a case where include_graphics() can be given a vector of paths of length > 1, producing a number of subplots with a single caption:
However, when I try this in my fork of thesisdown, in the PDF output I get the figure caption (and, judging by the spacing, the entire figure environment) repeated for each subplot. Here is a reproducible example:
---
output: bookdown::pdf_document2
toc: false
---
```{r, echo = FALSE}
for(i in 1:3){
jpeg(filename = paste0("temp_", i, ".jpg"), width = 600, height = 250)
plot(cars)
title(main = i)
dev.off()
}
```
```{r fig.cap = "Caption", out.width="100%", fig.ncol = 1, echo = FALSE}
knitr::include_graphics(paste0("temp_", 1:3, ".jpg"))
```
I was hoping more for the five images stacked, with a single caption at the bottom. This also appears to be breaking the figure cross-referencing, as each plot has its own figure number and cross-references to the chunk render as ??.
Getting subfigures requires a few additional settings to be set in the chunk header.
fig.subcap is a list of the captions for subfigures
fig.ncol: the number of columns of subfigures
out.width: the output width of the figures. You will normally set this 100% divided by the number of sub columns.
Subfigures are built using the subfig package. You can either include this within your LaTeX bookdown template, or alternative you can added it to the YAML as follows:
Here is an example:
---
output: bookdown::pdf_document2
toc: false
header-includes:
- \usepackage{subfig}
---
```{r, echo = FALSE}
for(i in 1:3){
jpeg(filename = paste0("temp_", i, ".jpg"), width = 600, height = 250)
plot(cars)
title(main = i)
dev.off()
}
```
```{r fig.cap = "Caption", out.width="100%", fig.ncol = 1, echo = FALSE, fig.subcap= c("First", "Second", "Third")}
knitr::include_graphics(paste0("temp_", 1:3, ".jpg"))
```
I successfully generate tables using either ztable, xtable or htmlTable packages with no problems. But, I'd like to know whether these tables may be output as pdf documents. When I try to knit a pdf_output, the table is not displayed. Instead the information is displayed as several strings. I've tried changing the latex engine (using ztable) and other methods with no luck.
I've looked here:
http://www.mzan.com/article/29773068-rmarkdown-latex-table-output-difficulties.shtml
I've also looked in the vignettes, etc.
Example (RMarkdown):
---
output: pdf_document
---
```{r, message = F, results = 'asis'}
# will throw out Error: pandoc document conversion failed with error 43
library(ztable)
data(iris)
options(ztable.type="latex")
zt <- ztable(iris[1:5,], caption = "ztable")
zt <- addcgroup(zt,
cgroup = c("group 1", "group 2"),
n.cgroup = c(2,3))
print(zt)
```
```{r, message = F }
# since it's html, will produce text only
library(htmlTable)
data(mtcars)
colnames(mtcars) <- NULL
htmlTable(mtcars[1:5,], caption = "htmlTable",
cgroup = c("group 1", "group 2"),
n.cgroup = c(5,6))
```
Adding header-includes: \usepackage{colortbl} resolves the error for ztable. I don't think htmlTable has a latex engine.
full code:
---
output: pdf_document
header-includes: \usepackage{colortbl}
---
```{r, message = F, results = 'asis'}
library(ztable)
data(iris)
options(ztable.type="latex")
zt <- ztable(iris[1:5,], caption = "ztable")
zt <- addcgroup(zt,
cgroup = c("group 1", "group 2"),
n.cgroup = c(2,3))
print(zt)
```
```{r, message = F }
# since it's html, will produce text only
library(htmlTable)
data(mtcars)
colnames(mtcars) <- NULL
htmlTable(mtcars[1:5,], caption = "htmlTable",
cgroup = c("group 1", "group 2"),
n.cgroup = c(5,6))
```
knitr defines the argument fig.cap as
fig.cap: (NULL; character) figure caption to be used in a figure
environment in LaTeX (in \caption{}); if NULL or NA, it will be
ignored, otherwise a figure environment will be used for the plots in
the chunk (output in \begin{figure} and \end{figure})
However, for HTML output the following works:
---
title: "Caption Test"
author: "Some Author"
date: "February 18, 2016"
output: html_document
---
```{r}
library(ggplot2)
```
```{r, fig.cap = c("This is caption 1", "This is caption 2")}
## Plot 1
qplot(carat, price, data = diamonds)
## Plot 2
qplot(carat, depth, data = diamonds)
```
meaning, each figures gets its correct caption defined in the code chunk argument fig.cap = c("Caption 1", "Caption 2")
However, it is challenging to keep track of captions -especially if long- when they are placed inside the chunk options. Besides creating two separate chunks for each figure with captions inserted outside of the chunk, are there other options?
You can set eval.after="fig.cap" so that figure captions are evaluated after the chunk is run. That way, you can define your captions inside the chunk.
---
title: "Caption Test"
author: "Some Author"
date: "February 18, 2016"
output: html_document
---
```{r}
library(ggplot2)
library(knitr)
opts_knit$set(eval.after = 'fig.cap')
```
```{r, fig.cap = cap}
## Plot 1
qplot(carat, price, data = diamonds)
cap <- "This is caption 1"
## Plot 2
qplot(carat, depth, data = diamonds)
cap <- c(cap, "This is caption 2")
```