Teach Yourself How to Create Functions in R

As you can tell from my previous posts, I am diving in head first into learning how to program (and simplify) my analytical life using R.  I have always learned by example and have never really prospered from the “learn from scratch” school of thought.  As I follow along with some other fellow R programmers, I find that they often use functions.  Intuitively I understand what they are and why the are awesome, yet I rarely find myself every thinking to employ them.

One reason is that I simply don’t yet fully grasp how to catch errors and debug them efficiently.  In addition, I am a very sloppy coder and don’t want to create anything “too complicated” since I am just starting out.  However, I recently was introduced to a neat little trick and I wanted to share.

Say there is a function that you often use but finding yourself wanting to make a personal change to.  Simply type the function name into the command line without any arguments!

For example:

[sourcecode language="r"]
library(gmodels)
CrossTable
[/sourcecode]

For example, I currently am trying to find a quick way to automate professional- looking crosstabs that I would feel comfortable using in the appendix of a survey research project on campus.  I really like the CrossTable function inside the gmodels package, but when Sweaving the output, the size is rediculous!  Even when it print’s to the console, you can see how large it prints to other things.  Not that I am capable of figuring out how to change this quite yet, it is still good as a newbie to R to see how the function was programmed, and presumably, how to improve my R coding skills.

As an aside, I recently added my blogs post to the R-bloggers feed.  If you stumble across my posts and are too trying to learn R, you would be well off to subscribe to the RSS feed.  The link to the feed is http://www.r-bloggers.com/.

Get up and running with R, Sweave, and LaTex

There are a lot of great references on the web on how to levarage LaTeX for reporting and presentations (beamer), but as someone who is completely new to R and reproducible research, I was having a pretty hard time figuring out simply what to do (what tools, where do you edit, etc.). Eclipse as a programming environment has been growing on me, but I simply could not get it to render me a PDF.

In this post, I am going to link to a few resources and attempt to explain it to you in a way that hopefully will get you up and running.  It really is pretty straight forward.  Learning how to modify commands, however, thats another post for another day.

Assumptions:
You have R up and running (I am using 2.10.1)
You are just trying to create a document – I tried to create a presentation (Beamer) and got an error when trying to open the PDF….I will try to figure this out later

First, save the following text below as a .rnw file

@found at http://www.math.ucla.edu/~getreuer/latex.html
\documentclass[12pt]{article}
\begin{document}

The \#1 story is that this article was compiled today (\today) with
\LaTeX.  To try some commands, it includes \textit{a few fancy}
\textbf{fonts}.

\end{document}

Here is the code:

### Getting started with Sweave in R
### helpful links
# My favorite
# http://biostat.mc.vanderbilt.edu/wiki/pub/Main/TheresaScott/ReproducibleResearch.TAScott.handout.pdf
# Others
# http://www.wekaleamstudios.co.uk/posts/creating-a-basic-presentation-using-latex-beamer
# http://jeromyanglim.blogspot.com/2010/02/getting-started-with-sweave-r-latex.html

### Simply, LaTeX is just formatting embedded inside of your text
### You can take templates online and save/edit in Notepad, just save as .nw
### The process is simple, text editor, save as .nw, run Sweave to generate .tex
### , compile in R

### There is also a stand alone program MikTex that will compile for you as well

### My basic code:

#### set the working diretory and check it
setwd("C:/Users/You/Desktop/Test Sweave/")
getwd()

#### Sweave is a function that will work on the .nw file
#### ?Sweave
Sweave("Test.nw")

####  You should see some code saying
####  Writing to file Test.tex
####  You can now run LaTeX on 'test.tex'
####  Now we simply have to compile it
tools::texi2dvi("Test.tex", pdf=TRUE)

####  You can also load the tools library first - shorter way above
####  library(tools)
####  texi2dvi("Test.tex", pdf=TRUE)

####  Voila, its that easy. No need to bother with eclipse plugins
####  The PDF will be saved to the working directory

One Note:  You may get prompted to install some additional packages…I said Yes.