Sunday, February 16, 2014

xtable in knitr

xtable in knitr
Here i am going to give an example of xtable package in knitr. We use cars data which comes default through the R
The summary data of cars:
summary(cars)
##      speed           dist    
##  Min.   : 4.0   Min.   :  2  
##  1st Qu.:12.0   1st Qu.: 26  
##  Median :15.0   Median : 36  
##  Mean   :15.4   Mean   : 43  
##  3rd Qu.:19.0   3rd Qu.: 56  
##  Max.   :25.0   Max.   :120
```{r table1, results='asis'} 
library(xtable)
data(cars)
print(xtable(summary(cars), caption = "Cars Data",
floating = FALSE, label = "tab:coef"), type = "html", include.rownames = F)
```
library(xtable)
data(cars)
print(xtable(summary(cars), caption = "Cars Data", floating = FALSE, label = "tab:coef"), 
    type = "html", include.rownames = F)
Cars Data
speed dist
Min. : 4.0 Min. : 2
1st Qu.:12.0 1st Qu.: 26
Median :15.0 Median : 36
Mean :15.4 Mean : 43
3rd Qu.:19.0 3rd Qu.: 56
Max. :25.0 Max. :120
The outcome in your html file is:
## <!-- html table generated in R 2.15.1 by xtable 1.7-1 package -->
## <!-- Sun Feb 16 23:55:02 2014 -->
## <TABLE border=1>
## <CAPTION ALIGN="bottom"> Cars Data </CAPTION>
## <TR> <TH>     speed </TH> <TH>      dist </TH>  </TR>
##   <TR> <TD> Min.   : 4.0   </TD> <TD> Min.   :  2   </TD> </TR>
##   <TR> <TD> 1st Qu.:12.0   </TD> <TD> 1st Qu.: 26   </TD> </TR>
##   <TR> <TD> Median :15.0   </TD> <TD> Median : 36   </TD> </TR>
##   <TR> <TD> Mean   :15.4   </TD> <TD> Mean   : 43   </TD> </TR>
##   <TR> <TD> 3rd Qu.:19.0   </TD> <TD> 3rd Qu.: 56   </TD> </TR>
##   <TR> <TD> Max.   :25.0   </TD> <TD> Max.   :120   </TD> </TR>
##    <A NAME=tab:coef></A>
## </TABLE>
The rest, you are able to add some style by using CSS…

No comments: