#+TITLE:
#+OPTIONS: author:nil toc:nil
#+DATE:

#+LATEX_Header: \usepackage{graphicx}
#+LATEX_Header: \oddsidemargin  0in
#+LATEX_Header: \evensidemargin 0in
#+LATEX_Header: \textwidth  6.5in


This is an example of an ~org-mode~  LaTeX-style file.  Run the
~irsOrg.org~ file through ~org-mode~  to create file
~irisOrg.pdf~.

Table \ref{irisBW5num} was generated by the ~#+BEGIN_SRC R~ chunk and included in
this ~org-mode~ file.

The file was run by

1. Entering ~ESS~ in Iris code block (the chunk bounded by ~#+BEGIN_SRC R~
   and ~#+END_SRC~), and running it in the ~*R*~ buffer.

   Place the cursor on ~#+BEGIN_SRC R~ and enter ~C-c C-c~.

2. ~C-c C-e lo~ (on my machine, perhaps ~C-c C-e d~ on yours).

#+NAME: Iris
#+BEGIN_SRC R :exports none :results none
  library(microplot)
  library(lattice)

  iris.melt <- reshape2::melt(iris, id="Species")
  irisBW <- bwplot( ~ value | Species * variable, data=iris.melt)

  ## twelve individual boxplots without axes
  irisBW.update <-
  update(irisBW,
         xlab=NULL,
         par.settings=list(
           layout.heights=layoutHeightsCollapse(),
           layout.widths=layoutWidthsCollapse(),
           axis.line=list(col="transparent")),
         layout=c(1,1)
         )

  pdf("irisBW%03d.pdf", onefile=FALSE, height=.4, width=1.6)  ## inch ## BB = 0 0 216 28
  print(irisBW.update)
  suppress <- dev.off()

  graphnames <- paste0("irisBW", sprintf("%03i", 1:12), ".pdf")

  graphicsnames <- t(matrix(as.includegraphics(graphnames, height="2em", raise="-1.3ex"),
                            nrow=3, ncol=4,
                            dimnames=dimnames(irisBW)))


  ## Each of the twelve Species*Measurement with its five number summary
  iris2 <- array(iris.melt$value, ## $
                 dim=c(50, 3, 4),
                 dimnames=list(NULL,
                   levels(iris.melt$Species),
                   levels(iris.melt$variable)))
  iris2.fivenum <- apply(iris2, 2:3, fivenum)
  dimnames(iris2.fivenum)[[1]] <- c("min", "Q1", "med", "Q3", "max")

  ## Species and Measurement in separate columns
  BW5num <-
  rbind(
  data.frame(t(iris2.fivenum[,1,]), "Box Plots"=graphicsnames[,1], check.names=FALSE),
  data.frame(t(iris2.fivenum[,2,]), "Box Plots"=graphicsnames[,2], check.names=FALSE),
  data.frame(t(iris2.fivenum[,3,]), "Box Plots"=graphicsnames[,3], check.names=FALSE))
  BW5num$Measurement=levels(iris.melt$variable)
  BW5num <- BW5num[, c(7,1:6)]

  BW5num.latex <- Hmisc::latex(BW5num,
                               rowname=" ",
                               rowlabel="Species",
                               rgroup=levels(iris.melt$Species),
                               n.rgroup=c(4,4,4),
                               cgroup=c("", "Five Number Summary", ""),
                               n.cgroup=c(1, 5, 1),
                               caption="Five Number Summary and Box Plots for each Species and Measurement.",
                               label="irisBW5num")
  BW5num.latex$style <- "graphicx"
  ## BW5num.latex  ## this line requires latex in the path
  ## the BW5num.latex line is not needed when the file is used in a \verb:\input: statement.
#+END_SRC

#+RESULTS: Iris
: graphicx

\input{BW5num.tex}
