Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Mastering R Programming: The Basics of R Sweave

Published on
4,355 Points
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”
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:

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:
Even though you defined what the title and author should say, you need to remember to call that function, so the most important thing:

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:
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:
\subsection*{Topics that will be covered in this article}
  \item What is \LaTeX?
  \item Why is it important?
  \item What can I do with it?

Open in new window

Which will compile to produce something like this:
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!
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Featured Post

Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

Join & Write a Comment

The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month