Playing with R and org-mode
Table of Contents
Sitemap
Playing with babel
This test has been inspired by http://orgmode.org/worg/org-contrib/babel/examples/foo.html
2+2
4
Let's generate some data
n <- 50 x <- seq(1, n) a.true <- 3 b.true <- 1.5 y.true <- a.true + b.true * x s.true <- 17.3 y <- y.true + s.true * rnorm(n) out1 <- lm(y ~ x)
Let's look at the summary of the linear regression
summary(out1)
Call: lm(formula = y ~ x) Residuals: Min 1Q Median 3Q Max -32.131 -9.722 -2.701 10.029 40.094 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 1.1789 4.2621 0.277 0.783 x 1.5420 0.1455 10.601 3.62e-14 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 14.84 on 48 degrees of freedom Multiple R-squared: 0.7007, Adjusted R-squared: 0.6945 F-statistic: 112.4 on 1 and 48 DF, p-value: 3.621e-14
And now, let's plot all this:
plot(x, y) abline(out1)
Sweet! Let's try a more complicated one, just for the fun:
library(ggplot2) data(diamonds) dsmall <-diamonds[sample(nrow(diamonds), 100), ] ggplot(data = dsmall, aes(x=carat, y=price, color=color, shape=cut, size=carat)) + geom_point()
Issues
- This org file was originally in my journal where one of the
previous entry is configured to use beamer. Whatever I tried,
the LaTeX export keeps using beamer. Isn't it possible to have in
the same org files some part that are exported in beamer and
others that are exported with article? It seems I can
override
TITLE
andAUTHOR
but notLaTeX_CLASS
. The html export works as a charm though so whatever…. The file/figure export mechanism presented in the previous link is completely broken. But the one explained in https://github.com/erikriverson/org-mode-R-tutorial/blob/master/org-mode-R-tutorial.org works like a charm. The correct syntax for simple blocks is thus:
#+BEGIN_SRC R :results output :exports both :session n <- 50 x <- seq(1, n) a.true <- 3 #+END_SRC
and for images:
#+BEGIN_SRC R :results output graphics :file fig1.png :session :exports both plot(x, y) abline(out1) #+END_SRC
Then all you have to do is
C-c C-c
in the block to evaluate it. For the moment, I just finally got it working and I have to say I still haven't completely undertood how these:results
and:exports
really work.Objects are thrown away from one block to another. I had to add a
:session
at the end of every block, which is a pain. Actually, I later realized that in already had set up a shortcu C-S-F4 that inserts an org-skeleton with default behavior for babel with R, including a global session flag.#+BABEL: :session *R* :cache yes :results output graphics :exports both :tangle yes
Unfortunately, it does not seem to work (maybe syntax has changed) and I still need to add these
:session
flags everywhere…This webpage also indicates a trick for inlining images that is just amazing! So when I open the org file in emacs it's almost wysiwyg! ;)
(add-hook 'org-babel-after-execute-hook 'org-display-inline-images) (add-hook 'org-mode-hook 'org-display-inline-images)
Entered on