In 20120718Wed143201, an Sweave example we used Sweave, xtable, and print options to display a dataframe in LaTeX.

In 20120718Wed183538, a knitr-to-LaTeX example we performed the same task replacing Sweave by knitr.

Now we will produce an html document to display the same table.

Whereas in a LaTeX document it was important to control the width of the columns so that a dataframe with long text passages in one of its columns (variables) would be legible, in this lesson we just depend on our ability to squish and squeeze the browser window using the mouse. The table responds accordingly---at least in today's browsers.

opts_chunk$set(fig.width = 5, fig.height = 5, fig.path = "ex-out-")

Suppose you have a table (or matrix or dataframe) with a lot of text in a cell, such as the following.

library(knitr)
library(xtable)
library(tools)
set.seed(5)
bigmess<- 
data.frame(
freeText=c("I have done the state some service and they know it. No more of that. I pray you, in your letters, When you shall these unlucky deeds relate, Speak of me as I am. Nothing extenuate, Nor set down aught in malice."
, "ignotumque sibi mentula discat opus" 
, "denique cum membris conlatis flore fruuntur aetatis, iam cum praesagit gaudia corpus"
, "cum dicis propero fac si facis Hedyle languet protinus et cessat debilitata Venus. exspectare jube. velocius ibo retentus.")
, x=sample(1:199, size=4, replace=FALSE))
bigmess$group<- sample(LETTERS, replace=TRUE, size= nrow(bigmess))
## Note that bigmess has three variables (columns):
names(bigmess)
## [1] "freeText" "x"        "group"   

The header option "results='markup'" displays something like what the R "console" would show us:

bigmess
##                                                                                                                                                                                                              freeText
## 1 I have done the state some service and they know it. No more of that. I pray you, in your letters, When you shall these unlucky deeds relate, Speak of me as I am. Nothing extenuate, Nor set down aught in malice.
## 2                                                                                                                                                                                 ignotumque sibi mentula discat opus
## 3                                                                                                                                denique cum membris conlatis flore fruuntur aetatis, iam cum praesagit gaudia corpus
## 4                                                                                          cum dicis propero fac si facis Hedyle languet protinus et cessat debilitata Venus. exspectare jube. velocius ibo retentus.
##     x group
## 1  40     C
## 2 136     S
## 3 181     N
## 4  56     V

whereas specifying "results='asis'" gives something illegible without further work:

bigmess
freeText 1 I have done the state some service and they know it. No more of that. I pray you, in your letters, When you shall these unlucky deeds relate, Speak of me as I am. Nothing extenuate, Nor set down aught in malice. 2 ignotumque sibi mentula discat opus 3 denique cum membris conlatis flore fruuntur aetatis, iam cum praesagit gaudia corpus 4 cum dicis propero fac si facis Hedyle languet protinus et cessat debilitata Venus. exspectare jube. velocius ibo retentus. x group 1 40 C 2 136 S 3 181 N 4 56 V

This is where xtable, along with "a judicious choice of print options," comes in handy.

In this situation, "results='markup'" is not what we want:

tmess <- xtable(as.matrix(bigmess))
print(tmess, include.rownames = FALSE, type = "html")
## 
## 
## 
## 
##   
##   
##   
##   
##    
freeText x group
I have done the state some service and they know it. No more of that. I pray you, in your letters, When you shall these unlucky deeds relate, Speak of me as I am. Nothing extenuate, Nor set down aught in malice. 40 C
ignotumque sibi mentula discat opus 136 S
denique cum membris conlatis flore fruuntur aetatis, iam cum praesagit gaudia corpus 181 N
cum dicis propero fac si facis Hedyle languet protinus et cessat debilitata Venus. exspectare jube. velocius ibo retentus. 56 V

In addition to the visible double pound signs (number signs), the displayed table does not reshape itself when I use the mouse to reshape the browser window (Firefox 8.0.1 or Safari 5.1.7).

But with "results='asis'", things are nicer:

tmess <- xtable(as.matrix(bigmess))
print(tmess, include.rownames = FALSE, type = "html")
freeText x group
I have done the state some service and they know it. No more of that. I pray you, in your letters, When you shall these unlucky deeds relate, Speak of me as I am. Nothing extenuate, Nor set down aught in malice. 40 C
ignotumque sibi mentula discat opus 136 S
denique cum membris conlatis flore fruuntur aetatis, iam cum praesagit gaudia corpus 181 N
cum dicis propero fac si facis Hedyle languet protinus et cessat debilitata Venus. exspectare jube. velocius ibo retentus. 56 V

Unlike with "results='markup'" this displayed table does reshape itself with my browser window.

Now let's display only the results, not the code, by setting "echo=FALSE".

freeText x group
I have done the state some service and they know it. No more of that. I pray you, in your letters, When you shall these unlucky deeds relate, Speak of me as I am. Nothing extenuate, Nor set down aught in malice. 40 C
ignotumque sibi mentula discat opus 136 S
denique cum membris conlatis flore fruuntur aetatis, iam cum praesagit gaudia corpus 181 N
cum dicis propero fac si facis Hedyle languet protinus et cessat debilitata Venus. exspectare jube. velocius ibo retentus. 56 V

Fine print (we use "results='markup'" so that this will display legibly):

## knit('knitfoo.Rhtml')
sessionInfo()
## R version 2.15.1 (2012-06-22)
## Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] tools     stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
## [1] xtable_1.7-0  knitr_0.7     ggplot2_0.9.1
## 
## loaded via a namespace (and not attached):
##  [1] colorspace_1.1-1   dichromat_1.2-4    digest_0.5.2      
##  [4] evaluate_0.4.2     formatR_0.4        grid_2.15.1       
##  [7] labeling_0.1       MASS_7.3-18        memoise_0.1       
## [10] munsell_0.3        parser_0.0-14      plyr_1.7.1        
## [13] proto_0.3-9.2      psych_1.2.1        RColorBrewer_1.0-5
## [16] Rcpp_0.9.10        reshape2_1.2.1     scales_0.2.1      
## [19] stringr_0.6       
Jacob Wegelin, 19 July 2012