# Key features of org-babel with R

## Table of Contents

- 1 Introduction
  - 1.1 Prelim:
  - 1.2 Getting started
- 2 Process an org table in R
  - 2.1 Process the results
- 3 Directly calling R within a paragraph.
- 4 Comparing base vs lattice graphics output
  - 4.1 Example of base graphics output
  - 4.2 Example of lattice graphics output
- 5 Passing results from one language to another via org.
- 6 Defining simple functions and inline calls
- 7 Why :session?
- 8 Sources

## 1 Introduction

This is an example document to demonstrate some simple R blocks in an org document. Convert it either to pdf or html using the appropriate export tools.

### 1.1 Prelim:

    (setq org-src-fontify-natively t)

### 1.2 Getting started

Here we compare the output from `:results value` with `:results output`. Within each code block, press `C-c C-c` to see (or update) the results.

    1 + 2
    3 + 4

    7

    1 + 2
    3 + 4

    [1] 3
    [1] 7

## 2 Process an org table in R

We use org-mode to create a nice-looking table, which we want to process in R.

name | maths | physics
--- | --- | ---
joe | 75 | 90
fred | 80 | 50
harry | 90 | 75

### 2.1 Process the results

We would like to read in the table, and then compute the weighted sum of the scores for each student. Note how we refer to the table above through its tblname.

    ave <- (0.7 * scores$maths) + (0.3 * scores$physics)
    data.frame(name=scores$name, score=ave)

       name score
    1   joe  79.5
    2  fred  71.0
    3 harry  85.5

## 3 Directly calling R within a paragraph.

Let's say you want to find out what π/2 is. Just ask R to evaluate it for you inline like this: π/2 = `1.5707963` to 8 significant digits. Note how Org mode knows automatically what you mean when you type `\pi`, so you don't need to go into `math mode' like you would in latex. Obviously for more complicated math you can.

## 4 Comparing base vs lattice graphics output

Unfortunately you need to have slightly different expressions for generating base and lattice graphics output.

### 4.1 Example of base graphics output

With base graphics, e.g. to make a histogram, try the following snippet. Here we generate a png file, just hit `C-c C-c` within the code to eval it, and generate the resulting file.

#+begin#+BEGINSRC R hist( rnorm(1e4)) #+ENDSRC ults:

Emacs can view png's within a buffer, and even show them inline using the minor mode `iimage-mode`. Turn this on with `M-x turn-on-image-mode`. (Warning: this sometimes places the images in the wrong place.)

### 4.2 Example of lattice graphics output

When using lattice graphics, you will need to set `:results output graphics` rather than just `:results graphics`. For example, here we generate a contour plot of the function

\[ \log F - F + \log S - S = k \]

where F and S are varied to see what the constant k should be. Note that we export the code, rather than exporting both the code and the results (in this case the graphics). The results line is still output though (delete it to check). However, note that the caption line must come before the file: line.

    library(lattice)
    f <- seq(from=0.1, to=4, length=100)
    s <- f
    grid <- expand.grid(x=f, y=s)
    grid$z <- log(grid$x) - grid$x + log(grid$y) - grid$y
    levelplot(z ~ x + y, grid, more=TRUE, contour=TRUE,xlab='fish', ylab='shark')

Lattice contour plot showing populations of sharks and fish.

## 5 Passing results from one language to another via org.

We can have code snippets in different languages, and plug them together using the textual output from one code block. Here is a simple example where we take some output from a shell command and feed it into an R block. (Of course, this example can be done entirely in R …)

    ls -sS /etc | grep -v total | head -5

376 | services
16 | moduli
48 | php.ini.default
32 | amavisd.conf
64 | authorization

This table is named biggest-files, which can be passed to an R function to create a little pie chart:

    pie(f[,1], labels = f[,2])

## 6 Defining simple functions and inline calls

Here is an example taken from the release notes of Org 7.6. We first define a function, also showing that variables can take default values. Argument interpretation seems similar to that used by R. It is defined here in lisp, but the same principle applies to R functions.

    (- a b)

We then call the function directly as in the following ways

    4

    7

Or we can call the function inline, as follows, e.g. to check that `6` is the answer to 13-7. To call a function inline, simply prefix the function name by `call_`.

## 7 Why :session?

We have not used :session here in the calls, for simplicity. But you might find it useful, for several reasons.

- You can see what is happening, rather than running in batch.
- A bit quicker than starting a new session each time.
- State kept from one block to another in the process.

Example from http://orgmode.org/worg/org-contrib/babel/intro.html

## 8 Sources

http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-R.html

The main documentation page. Many examples adapted from here or other Org Babel (OB) pages.

Date: 2011-07-30 18:29:29 BST

Author: Stephen Eglen

Org version 7.7 with Emacs version 23

Validate XHTML 1.0