Skip to main content

Introduction to Data Science Version 3

Section 11.3 Working with twitteR

To begin working with twitteR, launch your copy of R-studio. The first order of business is to create a new R-studio “project”. A project in R-studio helps to keep all of the different pieces and parts of an activity together including the datasets and variables that you establish as well as the functions that you write. For professional uses of R and R-studio, it is important to have one project for each major activity: this keeps different data sets and variable names from interfering with each other. Click on the “Project” menu in R-studio and then click on “New Project.” You will usually have a choice of three kinds of new projects, a brand new “clean” project, an existing directory of files that will get turned into a project folder, or a project that comes out of a version control system. (Later in the book we will look at version control, which is great for projects involving more than one person.) Choose “New Directory” to start a brand new project. You can call your project whatever you want, but because this project uses the twitteR package, you might want to just call the project “twitter”. You also have a choice in the dialog box about where on your computer R-studio will create the new directory.
R-studio will respond by showing a clean console screen and most importantly an R “workspace” that does not contain any of the old variables and data that we created in previous chapters. In order to use twitteR, we need to load several packages that it depends upon. These are called, in order “bitops”, “RCurl”, “RJSONIO”, and once these are all in place “twitteR” itself. Rather than doing all of this by hand with the menus, lets create some functions that will assist us and make the activity more repeatable. First, here is a function that takes as input the name of a package. It tests whether the package has been downloaded - “installed” - from the R code repository. If it has not yet been downloaded/installed, the function takes care of this. Then we use a new function, called require(), to prepare the package for further use. Let’s call our function EnsurePackage() because it ensures that a package is ready for us to use. If you don’t recall this step from the previous chapter, you should click the “File” menu and then click “New” to create a new file of R script. Then, type or copy/paste the following code:
EnsurePackage<-function(x)
{
x <- as.character(x)
if (!require(x,character.only=TRUE))
{
install.packages(pkgs=x,+
         repos="http://cran.r-project.org")
require(x,character.only=TRUE)
}
}
On Windows machines, the folder where new R packages are stored has to be configured to allow R to put new files there (“write” permissions). In Windows Explorer, you can right click on the folder and choose “Properties->Security” then choose your username and user group, click Edit, enable all permissions, and click OK. If you run into trouble, check out the Windows FAQ at CRAN by searching or using this web address: http://cran.r-project.org/bin/windows/base/rw-FAQ.html .
The require() function on the fourth line above does the same thing as library(), which we learned in the previous chapter, but it also returns the value FALSE if the package you requested in the argument "x" has not yet been downloaded. That same line of code also contains another new feature, the "if" statement. This is what computer scientists call a conditional. It tests the stuff inside the parentheses to see if it evaluates to TRUE or FALSE. If TRUE, the program continues to run the script in between the curly braces (lines 4 and 8). If FALSE, all the stuff in the curly braces is skipped. Also in the third line, in case you are curious, the arguments to the require() function include x, which is the name of the package that was passed into the function, and character.only=TRUE which tells the require() function to expect x to be a character string. Last thing to notice about this third line: there is a "!" character that reverses the results of the logical test. Technically, it is the Boolean function NOT. It requires a bit of mental gyration that when require() returns FALSE, the ! inverts it to TRUE, and that is when the code in the curly braces runs.
Once you have this code in a script window, make sure to select the whole function and click Run in the toolbar to make R aware of the function. There is also a checkbox on that same toolbar called, "Source on Save," that will keep us from having to click on the Run button all the time. If you click the checkmark, then every time you save the source code file, Rstudio will rerun the code. If you get in the habit of saving after every code change you will always be running the latest version of your function.
Now we are ready to put EnsurePackage() to work on the packages we need for twitteR. We’ll make a new function, "PrepareTwitter," that will load up all of our packages for us. Here’s the code:
PrepareTwitter<-function()
{
EnsurePackage("bitops")
EnsurePackage("RCurl")
EnsurePackage("RJSONIO")
EnsurePackage("twitteR")
}
This code is quite straightforward: it calls the EnsurePackage() function we created before five times, once to load each of the packages we need. You may get some warning messages and these generally won’t cause any harm. If you are on Windows and you get errors about being able to write to your library remember to check the Windows FAQ as noted above.
Make sure to save your script file once you have typed this new function in. You can give it any file name that make sense to you, such as "twitterSupport." Now is also a good time to start the habit of commenting: Comments are human readable messages that software developers leave for themselves and for others, so that everyone can remember what a piece of code is supposed to do. All computer languages have at least one "comment character" that sets off the human readable stuff from the rest of the code. In R, the comment character is #. For now, just put one comment line above each function you created, briefly describing it, like this:
# EnsurePackage(x) - Installs and loads a package if necessary
and this:
# PrepareTwitter() - Load packages for working with twitteR
Later on we will do a better job a commenting, but this gives us the bare minimum we need to keep going with this project. Before we move on, you should run the PrepareTwitter() function on the console command line to actually load the packages we need:
PrepareTwitter()
Note the parentheses after the function name, even though there is no argument to this function. What would happen if you left out the parentheses? Try it later to remind yourself of some basic R syntax rules.
You may get a lot of output from running PrepareTwitter(), because your computer may need to download some or all of these packages. You may notice the warning message above, for example,, about objects being "masked." Generally speaking, this message refers to a variable or function that has become invisible because another variable or function with the same name has been loaded. Usually this is fine: the newer thing works the same as the older thing with the same name.
Take a look at the four panes in R-Studio, each of which contains something of interest. The upper left pane is the code/ script window, where we should have the code for our two new functions. The lower left pane shows our R console with the results of the most recently run commands. The upper right pane contains our workspace and history of prior commands, with the tab currently set to workspace. As a reminder, in R parlance, workspace represents all of the currently available data objects include functions. Our two new functions which we have defined should be listed there, indicating that they have each run at least once and R is now aware of them. In the lower right pane, we have files, plots, packages, and help, with the tab currently set to packages. This window is scrolled to the bottom to show that RCurl, RJSONIO, and twitteR are all loaded and “libraryed” meaning that they are ready to use from the command line or from functions.
described in detail following the image
A screenshot of RStudio with four panes. The upper left shows the twitterSupport script with EnsurePackage and PrepareTwitter functions. The lower left shows the R console output from PrepareTwitter(). The upper right shows the workspace with both functions listed. The lower right shows the packages pane with RCurl, RJSONIO, and twitteR checked as loaded.
Figure 11.3.1. RStudio showing the twitterSupport script with both functions defined, the R console output from running PrepareTwitter(), the workspace listing both functions, and the packages pane confirming that RCurl, RJSONIO, and twitteR are loaded.