################################################### ### chunk number 1: eval=FALSE ################################################### ## #line 15 "xtableGallery.snw" ## makeme <- function() { ## # I am a convenience function for debugging and can be ignored ## setwd("C:/JonathanSwinton/PathwayModeling/src/R/SourcePackages/xtable/inst/doc") ## Sweave("xtableGallery.RnW",stylepath=FALSE) ## } ## makeme() ################################################### ### chunk number 2: ################################################### #line 44 "xtableGallery.snw" library(xtable) ################################################### ### chunk number 3: ################################################### #line 51 "xtableGallery.snw" data(tli) ## Demonstrate data.frame tli.table <- xtable(tli[1:10,]) digits(tli.table)[c(2,6)] <- 0 ################################################### ### chunk number 4: ################################################### #line 58 "xtableGallery.snw" print(tli.table,floating=FALSE) ################################################### ### chunk number 5: ################################################### #line 63 "xtableGallery.snw" design.matrix <- model.matrix(~ sex*grade, data=tli[1:10,]) design.table <- xtable(design.matrix) ################################################### ### chunk number 6: ################################################### #line 67 "xtableGallery.snw" print(design.table,floating=FALSE) ################################################### ### chunk number 7: ################################################### #line 72 "xtableGallery.snw" fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data=tli) fm1.table <- xtable(fm1) ################################################### ### chunk number 8: ################################################### #line 76 "xtableGallery.snw" print(fm1.table,floating=FALSE) ################################################### ### chunk number 9: ################################################### #line 80 "xtableGallery.snw" fm2 <- lm(tlimth ~ sex*ethnicty, data=tli) fm2.table <- xtable(fm2) ################################################### ### chunk number 10: ################################################### #line 84 "xtableGallery.snw" print(fm2.table,floating=FALSE) ################################################### ### chunk number 11: ################################################### #line 89 "xtableGallery.snw" print(xtable(anova(fm2)),floating=FALSE) ################################################### ### chunk number 12: ################################################### #line 93 "xtableGallery.snw" fm2b <- lm(tlimth ~ ethnicty, data=tli) ################################################### ### chunk number 13: ################################################### #line 96 "xtableGallery.snw" print(xtable(anova(fm2b,fm2)),floating=FALSE) ################################################### ### chunk number 14: ################################################### #line 103 "xtableGallery.snw" ## Demonstrate glm fm3 <- glm(disadvg ~ ethnicty*grade, data=tli, family=binomial()) fm3.table <- xtable(fm3) ################################################### ### chunk number 15: ################################################### #line 109 "xtableGallery.snw" print(fm3.table,floating=FALSE) ################################################### ### chunk number 16: ################################################### #line 115 "xtableGallery.snw" print(xtable(anova(fm3)),floating=FALSE) ################################################### ### chunk number 17: ################################################### #line 121 "xtableGallery.snw" ## Demonstrate aov ## Taken from help(aov) in R 1.1.1 ## From Venables and Ripley (1997) p.210. N <- c(0,1,0,1,1,1,0,0,0,1,1,0,1,1,0,0,1,0,1,0,1,1,0,0) P <- c(1,1,0,0,0,1,0,1,1,1,0,0,0,1,0,1,1,0,0,1,0,1,1,0) K <- c(1,0,0,1,0,1,1,0,0,1,0,1,0,1,1,0,0,0,1,1,1,0,1,0) yield <- c(49.5,62.8,46.8,57.0,59.8,58.5,55.5,56.0,62.8,55.8,69.5,55.0, 62.0,48.8,45.5,44.2,52.0,51.5,49.8,48.8,57.2,59.0,53.2,56.0) npk <- data.frame(block=gl(6,4), N=factor(N), P=factor(P), K=factor(K), yield=yield) npk.aov <- aov(yield ~ block + N*P*K, npk) op <- options(contrasts=c("contr.helmert", "contr.treatment")) npk.aovE <- aov(yield ~ N*P*K + Error(block), npk) options(op) #summary(npk.aov) ################################################### ### chunk number 18: ################################################### #line 138 "xtableGallery.snw" print(xtable(npk.aov),floating=FALSE) ################################################### ### chunk number 19: ################################################### #line 143 "xtableGallery.snw" print(xtable(anova(npk.aov)),floating=FALSE) ################################################### ### chunk number 20: ################################################### #line 148 "xtableGallery.snw" print(xtable(summary(npk.aov)),floating=FALSE) ################################################### ### chunk number 21: ################################################### #line 152 "xtableGallery.snw" #summary(npk.aovE) ################################################### ### chunk number 22: ################################################### #line 155 "xtableGallery.snw" print(xtable(npk.aovE),floating=FALSE) ################################################### ### chunk number 23: ################################################### #line 160 "xtableGallery.snw" print(xtable(summary(npk.aovE)),floating=FALSE) ################################################### ### chunk number 24: ################################################### #line 165 "xtableGallery.snw" ## Demonstrate lm ## Taken from help(lm) in R 1.1.1 ## Annette Dobson (1990) "An Introduction to Generalized Linear Models". ## Page 9: Plant Weight Data. ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) group <- gl(2,10,20, labels=c("Ctl","Trt")) weight <- c(ctl, trt) lm.D9 <- lm(weight ~ group) ################################################### ### chunk number 25: ################################################### #line 177 "xtableGallery.snw" print(xtable(lm.D9),floating=FALSE) ################################################### ### chunk number 26: ################################################### #line 182 "xtableGallery.snw" print(xtable(anova(lm.D9)),floating=FALSE) ################################################### ### chunk number 27: ################################################### #line 187 "xtableGallery.snw" ## Demonstrate glm ## Taken from help(glm) in R 1.1.1 ## Annette Dobson (1990) "An Introduction to Generalized Linear Models". ## Page 93: Randomized Controlled Trial : counts <- c(18,17,15,20,10,20,25,13,12) outcome <- gl(3,1,9) treatment <- gl(3,3) d.AD <- data.frame(treatment, outcome, counts) glm.D93 <- glm(counts ~ outcome + treatment, family=poisson()) ################################################### ### chunk number 28: ################################################### #line 199 "xtableGallery.snw" print(xtable(glm.D93,align="r|llrc"),floating=FALSE) ################################################### ### chunk number 29: prcomp ################################################### #line 204 "xtableGallery.snw" if(require(stats,quietly=TRUE)) { ## Demonstrate prcomp ## Taken from help(prcomp) in mva package of R 1.1.1 data(USArrests) pr1 <- prcomp(USArrests) } ################################################### ### chunk number 30: ################################################### #line 212 "xtableGallery.snw" if(require(stats,quietly=TRUE)) { print(xtable(pr1),floating=FALSE) } ################################################### ### chunk number 31: ################################################### #line 220 "xtableGallery.snw" print(xtable(summary(pr1)),floating=FALSE) ################################################### ### chunk number 32: ################################################### #line 226 "xtableGallery.snw" # ## Demonstrate princomp # ## Taken from help(princomp) in mva package of R 1.1.1 # pr2 <- princomp(USArrests) # print(xtable(pr2)) ################################################### ### chunk number 33: ################################################### #line 234 "xtableGallery.snw" temp.ts <- ts(cumsum(1+round(rnorm(100), 0)), start = c(1954, 7), frequency=12) temp.table <- xtable(temp.ts,digits=0) caption(temp.table) <- "Time series example" ################################################### ### chunk number 34: ################################################### #line 239 "xtableGallery.snw" print(temp.table,floating=FALSE) ################################################### ### chunk number 35: savetofile ################################################### #line 242 "xtableGallery.snw" if (FALSE) { for(i in c("latex","html")) { outFileName <- paste("xtable.",ifelse(i=="latex","tex",i),sep="") print(xtable(lm.D9),type=i,file=outFileName,append=TRUE,latex.environment=NULL) print(xtable(lm.D9),type=i,file=outFileName,append=TRUE,latex.environment="") print(xtable(lm.D9),type=i,file=outFileName,append=TRUE,latex.environment="center") print(xtable(anova(glm.D93,test="Chisq")),type=i,file=outFileName,append=TRUE) print(xtable(anova(glm.D93)),hline.after=c(1),size="small",type=i,file=outFileName,append=TRUE) # print(xtable(pr2),type=i,file=outFileName,append=TRUE) } } ################################################### ### chunk number 36: ################################################### #line 257 "xtableGallery.snw" insane <- data.frame(Name=c("Ampersand","Greater than","Less than","Underscore","Per cent","Dollar","Backslash","Hash", "Caret", "Tilde","Left brace","Right brace"), Character = I(c("&",">", "<", "_", "%", "$", "\\", "#", "^", "~","{","}"))) colnames(insane)[2] <- paste(insane[,2],collapse="") ################################################### ### chunk number 37: pxti ################################################### #line 263 "xtableGallery.snw" print( xtable(insane)) ################################################### ### chunk number 38: ################################################### #line 267 "xtableGallery.snw" wanttex <- xtable(data.frame( label=paste("Value_is $10^{-",1:3,"}$",sep=""))) ################################################### ### chunk number 39: ################################################### #line 270 "xtableGallery.snw" print(wanttex,sanitize.text.function=function(str)gsub("_","\\_",str,fixed=TRUE)) ################################################### ### chunk number 40: ################################################### #line 278 "xtableGallery.snw" mat <- round(matrix(c(0.9, 0.89, 200, 0.045, 2.0), c(1, 5)), 4) rownames(mat) <- "$y_{t-1}$" colnames(mat) <- c("$R^2$", "$\\bar{R}^2$", "F-stat", "S.E.E", "DW") mat <- xtable(mat) ################################################### ### chunk number 41: ################################################### #line 284 "xtableGallery.snw" print(mat, sanitize.text.function = function(x){x}) ################################################### ### chunk number 42: ################################################### #line 290 "xtableGallery.snw" money <- matrix(c("$1,000","$900","$100"),ncol=3,dimnames=list("$\\alpha$",c("Income (US$)","Expenses (US$)","Profit (US$)"))) ################################################### ### chunk number 43: ################################################### #line 293 "xtableGallery.snw" print(xtable(money),sanitize.rownames.function=function(x) {x}) ################################################### ### chunk number 44: ################################################### #line 299 "xtableGallery.snw" print(xtable(lm.D9,caption="\\tt latex.environment=NULL"),latex.environment=NULL) print(xtable(lm.D9,caption="\\tt latex.environment=\"\""),latex.environment="") print(xtable(lm.D9,caption="\\tt latex.environment=\"center\""),latex.environment="center") ################################################### ### chunk number 45: ################################################### #line 306 "xtableGallery.snw" tli.table <- xtable(tli[1:10,]) ################################################### ### chunk number 46: ################################################### #line 309 "xtableGallery.snw" align(tli.table) <- rep("r",6) ################################################### ### chunk number 47: ################################################### #line 312 "xtableGallery.snw" print(tli.table,floating=FALSE) ################################################### ### chunk number 48: ################################################### #line 316 "xtableGallery.snw" align(tli.table) <- "|rrl|l|lr|" ################################################### ### chunk number 49: ################################################### #line 319 "xtableGallery.snw" print(tli.table,floating=FALSE) ################################################### ### chunk number 50: ################################################### #line 323 "xtableGallery.snw" align(tli.table) <- "|rr|lp{3cm}l|r|" ################################################### ### chunk number 51: ################################################### #line 326 "xtableGallery.snw" print(tli.table,floating=FALSE) ################################################### ### chunk number 52: ################################################### #line 334 "xtableGallery.snw" digits(tli.table) <- 3 ################################################### ### chunk number 53: ################################################### #line 337 "xtableGallery.snw" print(tli.table,floating=FALSE,) ################################################### ### chunk number 54: ################################################### #line 343 "xtableGallery.snw" digits(tli.table) <- 1:(ncol(tli)+1) ################################################### ### chunk number 55: ################################################### #line 346 "xtableGallery.snw" print(tli.table,floating=FALSE,) ################################################### ### chunk number 56: ################################################### #line 352 "xtableGallery.snw" digits(tli.table) <- matrix( 0:4, nrow = 10, ncol = ncol(tli)+1 ) ################################################### ### chunk number 57: ################################################### #line 355 "xtableGallery.snw" print(tli.table,floating=FALSE,) ################################################### ### chunk number 58: ################################################### #line 360 "xtableGallery.snw" print((tli.table),include.rownames=FALSE,floating=FALSE) ################################################### ### chunk number 59: ################################################### #line 365 "xtableGallery.snw" align(tli.table) <- "|r|r|lp{3cm}l|r|" ################################################### ### chunk number 60: ################################################### #line 368 "xtableGallery.snw" print((tli.table),include.rownames=FALSE,floating=FALSE) ################################################### ### chunk number 61: ################################################### #line 373 "xtableGallery.snw" align(tli.table) <- "|rr|lp{3cm}l|r|" ################################################### ### chunk number 62: ################################################### #line 378 "xtableGallery.snw" print((tli.table),include.colnames=FALSE,floating=FALSE) ################################################### ### chunk number 63: ################################################### #line 383 "xtableGallery.snw" print(tli.table,include.colnames=FALSE,floating=FALSE,hline.after=c(0,nrow(tli.table))) ################################################### ### chunk number 64: ################################################### #line 388 "xtableGallery.snw" print((tli.table),include.colnames=FALSE,include.rownames=FALSE,floating=FALSE) ################################################### ### chunk number 65: ################################################### #line 393 "xtableGallery.snw" print(xtable(anova(glm.D93)),hline.after=c(1),floating=FALSE) ################################################### ### chunk number 66: ################################################### #line 398 "xtableGallery.snw" print(xtable(anova(glm.D93)),size="small",floating=FALSE) ################################################### ### chunk number 67: longtable ################################################### #line 407 "xtableGallery.snw" ## Demonstration of longtable support. x <- matrix(rnorm(1000), ncol = 10) x.big <- xtable(x,label='tabbig', caption='Example of longtable spanning several pages') ################################################### ### chunk number 68: ################################################### #line 414 "xtableGallery.snw" print(x.big,tabular.environment='longtable',floating=FALSE) ################################################### ### chunk number 69: ################################################### #line 425 "xtableGallery.snw" x <- x[1:30,] x.small <- xtable(x,label='tabsmall',caption='A sideways table') ################################################### ### chunk number 70: ################################################### #line 430 "xtableGallery.snw" print(x.small,floating.environment='sidewaystable') ################################################### ### chunk number 71: ################################################### #line 438 "xtableGallery.snw" toLatex(sessionInfo())