We're hiring a quantitative ecologist, find out more here

seascape models

A fast start to R scripts

I recently heard about the Atom text editor. It has been designed for coders by coders. The idea is to have a text editor that works on any OS and is hackable. That means you can customise any of its features, or write your own features. It acheives OS independence by using web-technology, i.e. Javascript and CSS. If you know these it is easy start hacking Atom, if not, they are not that hard to learn (certainly easier than R!). Regardless, you don't need to know any programming language to use Atom. I am now trying it out for R programming.

In the past I have primarily used the R inbuilt scripter for writing code and RStudio for teaching. My hope is that Atom will make life easier and will also be flexibile, but easy enough to use for teaching.

To help convince you about why switching to Atom is a good idea, here is an example of one of its cool features: Snippets

Atom Snippets allow you to type a shortcut and hit tab, and it will fill in the rest for you. One way to use Snippets would be to set up Snippets for common program structures, to save you time in re-writing them every time you start a new project

One of the most common things to do in R is to import data and manipulate it into a usable form. So here is a step by step guide for how to set up such a snippet in Atom

Step 1: Install Atom

Atom is available here

Step 2: Open Your Snippets

Got to the 'Atom' menu and select 'Open your snippets'

Step 3: Write your snippet

Snippet code uses the .cson (CoffeeScript Object Notation) structure to delineate commands and code. Here is an example to create a common R script:

      'R data import':
        'prefix': 'R_data_import'
        'body': """
        rm(list = ls())
        dat <- read.csv('.csv', header = TRUE)

The first line is the file type, the second line is the name, the third line is the shortcut name and the fourth line 'body' is the snippet. You can enclose multiline snippets in '"""' as I have done here.

Now, create a new file and save it with the '.r' (lower case is important) extension. When you start writing your file type 'R_data_' and a drop down box should appear. Scroll down and select 'R_data_import' and hit tab. It should auto-fill your script and you are ready to begin with some data wrangling.

If you want to evalute code you write in Atom, check out Atoms 'eval-r' package (OSX only).

Contact: Chris Brown

Email Tweets Code on Github


Designed by Chris Brown. Source on Github