testing.Rorg 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- mode: org -*-
  2. #+OPTIONS: LaTeX:t
  3. * Sweave and org-mode
  4. If you're reading a PDF version of this document, you should also
  5. look at [[file:testing.Rorg][testing.Rorg]] (the source file) and [[file:testing.org][testing.org]] (the output
  6. of the Sweave process).
  7. Keep in mind that one of the advantages of a block-based approach is
  8. using \texttt{C-'} to edit code in its native mode.
  9. ** Use the Sweave package for latex formatting
  10. Org allows us to issue commands to be included in \{LaTeX} export.
  11. #+LATEX_HEADER \usepackage{Sweave}
  12. ** R blocks
  13. The first argument to an R block when using Sweave is the label for
  14. that block.
  15. Not all R blocks are printed. Sweave options allow the printing of
  16. the evaluated code, the output of the code, both, or neither.
  17. *** R code that is not printed
  18. #+BEGIN_R hidden_block, echo=FALSE, results=HIDE
  19. a <- 3
  20. b <- 6
  21. #+END_R
  22. *** R code that is printed
  23. #+BEGIN_R visible_block
  24. c <- 4
  25. #+END_R
  26. We can use block labels to embed blocks by reference (even if they
  27. weren't printed before).
  28. *** R code that references other blocks
  29. #+BEGIN_R combined_block
  30. #+R_CODEREF: hidden_block
  31. #+R_CODEREF: visible_block
  32. a + b +c
  33. #+END_R
  34. ** Inline references to R data
  35. We can evaluate R code inline.
  36. *** Used in text
  37. The value of =a= is 3.
  38. *** Used in a table
  39. | a | b | c | TOTAL |
  40. |-------+-------+-------+---------------|
  41. | \R{a} | \R{b} | \R{c} | \R{a + b + c} |
  42. ** Single-line R commands
  43. If we want a line of R code to be evaluated but not printed,
  44. there's a convenient shorthand. This only works for single lines
  45. of R code, but you can have more than one in a row.
  46. #+R library(lattice)
  47. #+R data(cars)
  48. ** Graphics
  49. We use values defined elsewhere in the buffer to produce this
  50. graph. The new CAPTION and LABEL arguments work just fine.
  51. #+CAPTION: speed by distance
  52. #+LABEL: fig:speed_by_distance
  53. #+BEGIN_R figure.eps
  54. print(xyplot(speed ~ dist, cars,
  55. panel = function (x, y, ...) {
  56. panel.xyplot(x, y, ...)
  57. panel.abline(h=a)
  58. panel.abline(v=b)
  59. }))
  60. #+END_R