Setup and configuration
Adapted from org-mode official site.
-
Install R with binary or source code Comprehensive R Archive Network for Arch Linux, following documents here.
12345sudo pacman -S r# and rstudio IDEpacaur -y rstudio-desktop-bin -
Install ESS (Emacs Speak Statistics) Mode
Follow instruction here.
1234(add-to-list 'load-path "~/.emacs.d/ESS/lisp")(require 'ess-site) -
Finally, you’ll need to ensure that org-babel-load-languages includes an entry for R.
12345(org-babel-do-load-languages'org-babel-load-languages'((R . t))) -
Org Mode supports graphical output for LaTeX documents using the PGF and TikZ graphics system for TeX. In order to use this facility your R installation will need the tikzDevice package. One way to ensure that this device is loaded is to put the following lines in .Rprofile:
1234old <- getOption("defaultPackages")options(defaultPackages = c(old, "tikzDevice"))
Use ESS to step through evaluation line-by-line
Use C-c C-c to execute the block
Use C-c ' to visit the edit buffer for your code block
Use ess-eval-line-and-step to evaluate each line in turn
Header arguments for output
-
does not produce a file -as you see it does here?
123456#+begin_src R :file 1.png :results value graphicslibrary(lattice)xyplot(1:10 ~ 1:10)#+end_src -
does produce a file, by printing object
123456#+begin_src R :file 2.png :results value graphicslibrary(lattice)print(xyplot(1:10 ~ 1:10))#+end_src -
does produce a file, by using :results output
123456#+begin_src R :file 3.png :results output graphicslibrary(lattice)xyplot(1:10 ~ 1:10)#+end_src -
does produce a file, by evaluating in :session
123456#+begin_src R :file 4.png :session :results graphicslibrary(lattice)xyplot(1:10 ~ 1:10)#+end_src
share var across blocks
1 2 3 4 5 |
#+BEGIN_SRC R :session test <- head(mtcars) #+END_SRC |
1 2 3 4 5 6 7 8 9 |
#+RESULTS: | 21 | 6 | 160 | 110 | 3.9 | 2.62 | 16.46 | 0 | 1 | 4 | 4 | | 21 | 6 | 160 | 110 | 3.9 | 2.875 | 17.02 | 0 | 1 | 4 | 4 | | 22.8 | 4 | 108 | 93 | 3.85 | 2.32 | 18.61 | 1 | 1 | 4 | 1 | | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 | | 18.7 | 8 | 360 | 175 | 3.15 | 3.44 | 17.02 | 0 | 0 | 3 | 2 | | 18.1 | 6 | 225 | 105 | 2.76 | 3.46 | 20.22 | 1 | 0 | 3 | 1 | |
1 2 3 4 5 6 |
#+BEGIN_SRC R :session :file 4.png :results graphics test plot(pressure) #+END_SRC |
Org Mode Output
from R with Ascii package
1 2 3 4 5 6 7 8 9 10 |
> library(ascii) > options(asciiType = "org") > 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()) > ascii(glm.D93) |
1 2 3 4 5 6 7 8 9 |
| | Estimate | Std. Error | z value | Pr(> \vert z \vert ) | |-------------+----------+------------+---------+----------------------| | (Intercept) | 3.04 | 0.17 | 17.81 | 0.00 | | outcome2 | -0.45 | 0.20 | -2.25 | 0.02 | | outcome3 | -0.29 | 0.19 | -1.52 | 0.13 | | treatment2 | 0.00 | 0.20 | 0.00 | 1.00 | | treatment3 | 0.00 | 0.20 | 0.00 | 1.00 | |
from org-mode R block
1 2 3 4 5 6 7 8 9 10 |
#+BEGIN_SRC R :results output org library(ascii) a <- runif(100) c <- "Quantiles of 100 random numbers" b <- ascii(quantile(a),header=T,include.colnames=T,caption=c) print(b,type="org") rm(a,b,c) #+END_SRC |
1 2 3 4 5 6 7 8 9 10 11 |
#+CAPTION: Quantiles of 100 random numbers | 0% | 25% | 50% | 75% | 100% | |------+------+------+------+------| | 0.01 | 0.28 | 0.56 | 0.76 | 1.00 | Warning messages: 1: In rep(rownames, length = nrow(x)) : 'x' is NULL so the result will be NULL 2: In rep(colnames, length = ncol(x)) : 'x' is NULL so the result will be NULL |
latex code from R
1 2 3 4 5 6 7 8 |
#+begin_src R :results output latex library(xtable) x <- rnorm(100) y <- x + rnorm(100) xtable(summary(lm(y ~ x))) #+end_src |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#+RESULTS: #+BEGIN_EXPORT latex % latex table generated in R 3.4.0 by xtable 1.8-2 package % Thu May 25 16:07:05 2017 \begin{table}[ht] \centering \begin{tabular}{rrrrr} \hline & Estimate & Std. Error & t value & Pr($>$$|$t$|$) \\ \hline (Intercept) & -0.0308 & 0.1013 & -0.30 & 0.7617 \\ x & 0.9268 & 0.0930 & 9.96 & 0.0000 \\ \hline \end{tabular} \end{table} #+END_EXPORT |
MAY
About the Author:
Beyond 8 hours - Computer, Sports, Family...