<

Go Premium for a chance to win a PS4. Enter to Win

x

Mastering R Programming: The Basics of R Sweave

Published on
4,448 Points
1,148 Views
3 Endorsements
Last Modified:
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This article is a continuation of my series "Mastering R Programming.  I encourage you to read the other articles in my series (Article 1 and Article 2) to gain basic foundations and to become comfortable navigating and using of R and R Studio.
 
This article will focus on something a bit different than my previous articles have.  I would like to highlight one of my favorite components of R: Sweave.
 
R Sweave combines the power of R with the functionality of LaTeX (a document markup language) to produce a PDF file that will be useful for dynamic reports and narratives that include graphics, code, or anything you need from your R output!  If you are not already familiar with using LaTeX I highly recommend you look into the powerful reports and clean information it can produce.  The language is very straightforward and will take very little time to pick up because it is so natural to utilize.
 
First let's start with the basics.  The first thing you need is to have LaTeX installed on your computer.  You can find the correct file for your system here
 
NOTE: These links will take you to webpages that allow you to install 1-2GB files with full GUI interfaces for LaTeX.  You only need to download the basic TeX (a typesetting system) software because we will be only using R Studio as the GUI for now.  If you feel so inclined you can download the entire thing but this article only requires the basic package.
 
NOTE: I will be using LaTeX code in this article which I will not be explaining.  That is a whole different series of articles, I would recommend if you have no experience, start here.
 
Once you have installed TeX, go ahead and start up R Studio.
 
 
Creating a new Sweave file
 
First thing we need to do is make sure our working directory is setup properly!  If you don’t remember how to do this, it is highlighted in my first article.  When creating the actual PDF from the Sweave file, there will be about 6 different documents (1 being the PDF itself), all which are needed to compile the document.  Trust me you do not want to lose track of these files if you ever need to edit the document.
 
Next step is to create a new Sweave file.  Navigate to the “New” icon and select “R Sweave”
Screen-Shot-2015-09-15-at-3.51.28-PM.png 
You will see that R Studio automatically populates some text for you.  This is the basic structure every document needs to eventually create you a PDF.
 
 
Basic Functionalities
 
I will structure this article like I needed to type up…well…an article :P.  This would be the format for scientific articles or published articles as an example. 
 
So first we will put in some documentation for the file.  This includes an author and a title.  This type of information, along with packages (more information to come on those later), needs to come after the “\documentclass” section but before the “\begin{document}” section.  Think if it like setting parameters of any new document.  You wouldn’t want the same title on every article and they might not always have the same author, so you need to define that:
 
\title {Using \LaTeX with R}
\author {Matthew Richardson\\
Written for Experts Exchange
}

Open in new window


Below this would go any packages you are using, here we are only going to use a simple one to include pictures:
 
\usepackage{graphicx}

Open in new window


That is all we need for now.  Next we are going to jump down below the \SweaveOpts{concordance=TRUE}.  This is the section where the “body” of your information goes.  See below:
Screen-Shot-2015-09-16-at-10.03.23-A.png 
Even though you defined what the title and author should say, you need to remember to call that function, so the most important thing:
 
\maketitle

Open in new window


You now have enough to compile the document and produce a PDF!  On the top menu box, click “Complie PDF” and R will do the rest for you (see screenshot below).  It creates a couple files for processing, which you will see in your working directory. 
Screen-Shot-2015-09-16-at-10.14.58-A.pngThis will trigger a popup that will show you what your PDF document will look like!  It should look similar to mine below:
Screen-Shot-2015-09-16-at-10.19.32-A.png 
You’ll notice a few other functional buttons next to the compile button.  The “Format” button is of particular interest for the type of paper we are constructing.  I encourage you to click that dropdown and play with some of the options but I will go over some of the most important to get you on your way to organizing an article!
 
This button is the shortcut for including LaTeX code for simple functions like section headers, lists, and font structures.  When you choose an option you will see that R Studio does the TeX formatting for you and all you have to do is add your information.  Here is an example:
 
\section*{Introduction}
\subsection*{Topics that will be covered in this article}
\begin{enumerate}
  \item What is \LaTeX?
  \item Why is it important?
  \item What can I do with it?
\end{enumerate}

Open in new window


Which will compile to produce something like this:
Screen-Shot-2015-09-16-at-10.26.34-A.png 
NOTE: The asterisk (*) that R Studio automatically puts next to the “section” and “subsection” functions will suppress the numbering of the sections.  I encourage you to remove that and see the difference for yourself if you want that preference in your paper.
 
Well that gives you a basic understanding of how to use R Sweave to write LaTeX code.  I will continue with the next article and explaining how to integrate other functions from packages and more importantly, how to use your R code in your document!
 
As always, comment if you would like something particular included in the next article in the series!
3
Comment
0 Comments

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Join & Write a Comment

The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month8 days, 2 hours left to enroll

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month