# -*- eval: (load-library "ob-R") -*-


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

This is an example of an ~org-mode~  html-style file.  Run the
~irsOrgHtml.org~ file through ~org-mode~ in ~emacs~ to create and display file
~irisOrgHtml.html~.  The distributed version of the saved ~html~ file
is called ~irisOrgHtml-distributed.html~.

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  R 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 ho~ (on my machine) or ~C-c C-e b~ (maybe on yours).

# #+NAME: as.orgfile
# #+BEGIN_SRC R :session :exports none :results output
# as.orgfile <- function(object, height = "1em", width = NULL, wd = getwd(),
#     raise = NULL)
# {
#     result <- paste0("[[file:", file.path(wd, object), "]]")
# }

# as.orgtable <- function(x) {
#   tableheader <- paste(c("", names(x), ""), collapse=" | ")
#   tmp <- names(x)
#   tmp[] <- "-"
#   tablerule <-  paste0("|", paste(tmp, collapse="+"), "|")
#   tablebody <- apply(cbind("", x, ""), 1, paste, collapse=" | ")
#   as.matrix(c(tableheader, tablerule, tablebody))
# }
# #+END_SRC

# #+RESULTS: as.orgfile

#+NAME: Iris
#+BEGIN_SRC R :session :exports results :results output raw
  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)
         )

  png("irisBW%03d.png", height=.4, width=1.6, units="in", res=72)
  print(irisBW.update)
  suppress <- dev.off()

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

  graphicsnames <- t(matrix(as.orgfile(graphnames),
                            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)]
  BW5numTemp <- BW5num
  BW5numTable <- as.orgtable(BW5num)

  cat(BW5numTable, sep="\n")
#+END_SRC

#+RESULTS: Iris
 | Measurement  | min |  Q1 |  med |  Q3 | max | Box Plots                         |
 |--------------+-----+-----+------+-----+-----+-----------------------------------|
 | Sepal.Length | 4.3 | 4.8 | 5.00 | 5.2 | 5.8 | [[file:/Users/rmh/Rwd/irisBW001.png]] |
 | Sepal.Width  | 2.3 | 3.2 | 3.40 | 3.7 | 4.4 | [[file:/Users/rmh/Rwd/irisBW004.png]] |
 | Petal.Length | 1.0 | 1.4 | 1.50 | 1.6 | 1.9 | [[file:/Users/rmh/Rwd/irisBW007.png]] |
 | Petal.Width  | 0.1 | 0.2 | 0.20 | 0.3 | 0.6 | [[file:/Users/rmh/Rwd/irisBW010.png]] |
 | Sepal.Length | 4.9 | 5.6 | 5.90 | 6.3 | 7.0 | [[file:/Users/rmh/Rwd/irisBW002.png]] |
 | Sepal.Width  | 2.0 | 2.5 | 2.80 | 3.0 | 3.4 | [[file:/Users/rmh/Rwd/irisBW005.png]] |
 | Petal.Length | 3.0 | 4.0 | 4.35 | 4.6 | 5.1 | [[file:/Users/rmh/Rwd/irisBW008.png]] |
 | Petal.Width  | 1.0 | 1.2 | 1.30 | 1.5 | 1.8 | [[file:/Users/rmh/Rwd/irisBW011.png]] |
 | Sepal.Length | 4.9 | 6.2 | 6.50 | 6.9 | 7.9 | [[file:/Users/rmh/Rwd/irisBW003.png]] |
 | Sepal.Width  | 2.2 | 2.8 | 3.00 | 3.2 | 3.8 | [[file:/Users/rmh/Rwd/irisBW006.png]] |
 | Petal.Length | 4.5 | 5.1 | 5.55 | 5.9 | 6.9 | [[file:/Users/rmh/Rwd/irisBW009.png]] |
 | Petal.Width  | 1.4 | 1.8 | 2.00 | 2.3 | 2.5 | [[file:/Users/rmh/Rwd/irisBW012.png]] |

