forked from HanOostdijk/rmd_pdf_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiris_data_set_vm6.rmd
91 lines (80 loc) · 3.31 KB
/
iris_data_set_vm6.rmd
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
---
params:
sortorder: ascending
title: "iris_data_set_vm6"
author: "Han Oostdijk (www.hanoostdijk.nl)"
date: "`r format(Sys.time(), '%d %B, %Y')`" # current date
graphics: yes
linkcolor: blue
output:
pdf_document:
fig_caption: yes
includes:
in_header:
- styles.tex
keep_tex: no
highlight: tango
geometry:
- a4paper
- portrait
- margin=0.5in
toc: false
toc_depth: 2
knit: (function(inputFile, encoding) {rmarkdown::render(inputFile,encoding=encoding,clean=T,output_file = 'Iris_data_set_vm6.pdf') })
#bibliography: mybib.bib
---
```{r child='setup.rmd'}
```
# Introduction
See the Introduction pdf for the various examples and the relation of this example to the others.
This example shows how to print two tables side by side. It makes use of the
function \mytextit{print\_table\_sbs} that generates the \LaTeX \ commands that are necessary for this. If this is usable depends on the size of the tables and your eyesight.
# Utility functions and constants
```{r echo=T,warning=FALSE,message=FALSE}
numlist = 10 # number of observations in iris data set that is listed
print_table_sbs <- function (files,label,cap,caps,scalebox=0.85) {
# print xtables saved in files side-by-side
# derived from Marcin Kosiński
# http://stackoverflow.com/questions/23926671/side-by-side-xtables-in-rmarkdown
cat('\\begin{table}[ht]\n')
cat('\\centering\n')
for (i in 1:length(files)) {
tc = caps[[i]]
tci = paste0(label,letters[i])
f = files[[i]]
c = '\\subfloat[%s]{\\label{table:%s}\\scalebox{%.3f}{\\input{./%s}}}\\quad\n'
cat(sprintf(c,tc,tci,scalebox,f))
}
cat(sprintf('\\caption{%s}\n',cap))
cat(sprintf('\\label{table:%s}\n',label))
cat('\\end{table}')
}
```
# Explanation
We will create two tables in R and use package \mytextbf{xtable} to write the \LaTeX \ representation to a tex-file. We use the iris data set as an example and will sort it in ascending and descending order (by the variable \mytextit{Sepal.Length}). Because that is twice (nearly) the same code we have created the function \mytextit{create\_xtable} to do this. In the function we select only the first/last `r numlist` observations.
However the only important line is \mytextit{print(xtable(. . . floating=FALSE)} .
The function \mytextit{print\_table\_sbs} will take the two files and print them side by side if that fits. Otherwise the second table will be positioned below the first one.
`r ref_tab('r1a',prefix='')` we will show the `r numlist` observations with the smallest and `r ref_tab('r1b',prefix='',add_page=F)` the ones with the greatest \mytextit{Sepal.Length}.
```{r,warning=F,message=F}
library(xtable)
create_xtable<- function (decreasing,numlist,filename) {
data(iris)
tab = iris[order(iris$Sepal.Length,decreasing = decreasing),]
print(xtable(tab[1:numlist,],row.names=F),
file=paste0(filename,'.tex'),floating=FALSE)
}
create_xtable(F,numlist,filename='asc')
create_xtable(T,numlist,filename='desc')
```
```{r r1,results= 'asis'}
files = c('asc','desc') # filenames (without suffix tex)
label = 'r1' # label (sublabels have suffix 1,2, ...)
cap = 'first/last observations of iris data set sorted by \\mytextit{Sepal.Length}'
caps = c('first', 'last')
print_table_sbs(files,label,cap,caps,scalebox=0.5)
cat(paste(' #produced',ref_tab('r1',prefix='')))
```
# Session Info
```{r}
sessionInfo()
```