Site icon R-bloggers

Programming for the rest of us: Introducing a new R package to help solve problems

[This article was first published on R Programming – Thomas Bryce Kelly, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

As a self trained, informal, fly by the seat of my pants programming, I am hardly the “best” person to give advice about teaching other how to program. I have no formal background in computer science and have never had anyone teach me to program, it’s just been something that I’ve played around in by myself. I taught myself some Java when I was 12, python during undergrad–at one point I knew some C, PHP, and others–but it was all a means to an end. I learned it so I could solve a problem I had, or an idea of project that I wanted to work on. A Gedankenexperiment. So with that in mind I’d like to share a bit about a tool I’ve built up over the last couple of years.

For my PhD work I decided to learn R for a number of reasons, and so I taught myself R (and continue to learn). After a while I started making my own cheat-sheet of useful functions that I would simply import whenever I started a new script: I had a function for adding error bars to datapoints, a function for converting between excel’s numeric datetime values and true datetime’s. These functions were often just snippets of code put together based on stackexchange answers or from bits of code that I found myself typing out constantly. Basically I was lazy and these functions helped me focus on my work.

Flash forward 4 years and my catalogue of useful functions has grown considerably: some functions have disappeared, most have been rewritten or generalized. And honestly they have become much more useful over the years based on feedback I’ve received while teaching people R in my department and elsewhere. Since I am a jack-of-all-trades for oceanography, lots of people come to me because they have a particular problem they would like some advice, help, moral support with. And so my cheat sheet of functions has grown and improved because of this.

Recently I took the next step to formalizing the code I’ve written by wrapping them all up into a new R library called TheSource. basically it now easier than ever to share this tool with everyone:

library(devtools)
devtools::install_github('tbrycekelly/TheSource')

## Now that the package is installed, just load it when you want:
library(TheSource)

While my code is not very novel, I think it is incredibly useful since it combines some of the best R libraries out there (e.g. OCE for maps) under a simplified, common grammer. For example, if you have a plot and want to add some error bars, you would use the add.error.bars() function. If you had some data and wanted to convert between pressure and depth you would use conv.p.to.d(). If you want to make a map of some location you would type out make.map(). The whole idea is that this is a package that does not require extensive study to utilize, rather it keeps everything as simple as possible with useful function names and straightforward R code and data structures.

To see the code used in any function just type out the function name. Most functions are about a dozen lines of code; and when there was a choice, I’ve opted for a simple and easy to read implementation of the function so that they would be easy to customize and to learn from. If you want a tool to help you solve problems (and not create more for you), then please check out TheSource. It will hold your beer.

See more at the github page.                        

To leave a comment for the author, please follow the link and comment on their blog: R Programming – Thomas Bryce Kelly.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.