-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTables_Extra.Rmd
More file actions
73 lines (60 loc) · 2.04 KB
/
Tables_Extra.Rmd
File metadata and controls
73 lines (60 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
---
title: "Tables"
author: "EE Holmes"
output:
html_document: default
pdf_document: default
word_document: default
always_allow_html: yes
---
This is a more complex example of making tables.
```{r setup, warning=FALSE}
library(knitr)
library(kableExtra)
library(xtable)
output.type <- knitr::opts_knit$get('rmarkdown.pandoc.to')
output.type
```
Here I'll use a data frame from from a R data set, but in practice your data frame would come from a function.
Note the first time you make a PDF, tinytex will load any needed packages and it can take a **long** time. You'll see a spinning wheel on the R Markdown tab.
```{r}
a <- mtcars[1:7, 1:6]
```
Default **kable** table.
```{r}
knitr::kable(a)
```
## Nicer version
This is the table with **xtable** for LaTeX and **kableExtra** for html.
```{r, results='asis', echo=FALSE}
caption="My table caption"
#Dynamically add stuff to caption
caption <- paste(caption, Sys.Date())
# set up latex alignment
aln <- paste0("rr|c|", paste(rep("c", ncol(a)-2), collapse=""))
if(output.type=="latex"){
# Google table placement latex to learn the placement options
print(xtable::xtable(a, align=aln, digits=0, caption=caption),
comment=FALSE, include.rownames=FALSE,
caption.placement="top", table.placement="h", type=output.type)
# Landscape for wide tables. You need table.placement="p" here because you need a new page.
cat("\\begin{landscape}")
print(xtable::xtable(a, align=aln, digits=0, caption=caption),
comment=FALSE, include.rownames=FALSE,
caption.placement="top", table.placement="p", type=output.type)
cat("\\end{landscape}")
# If you just want the tex file
print(xtable::xtable(a, align=aln, digits=0, caption=caption),
comment=FALSE, include.rownames=FALSE,
caption.placement="top", table.placement="p", type=output.type, file="table.tex")
}
if(output.type=="html"){
knitr::kable(a, caption = caption) %>%
kableExtra::kable_styling(full_width = FALSE)
}
if(output.type=="word"){
cat("\n## The table heading\n\n")
cat(caption)
knitr::kable(a)
}
```