Starting with R

You can download R from the Comprehensive R Archive Network (CRAN) site at http://cran.r-project.org. You can get the R engine for Windows, Linux, or macOS X. Microsoft also maintains its own R portal, the Microsoft R Application Network (MRAN) site at https://mran.revolutionanalytics.com/. You can use this site to download Microsoft R Open, the enhanced distribution of open source R from Microsoft. After installation, you start working in an interactive mode. You can use the R console client tool to write code, line by line. As you already know, there are many additional tools. Let's just repeat here that the most widely used free tool for writing and executing R code is RStudio. It is free and you can download it from https://www.rstudio.com/. This section assumes you use RStudio for the code examples.

R is a case-sensitive, functional, and interpreted language. The R engine interprets your commands one by one. You don't type commands but rather call functions to achieve results, even a function called q() to quit an R session. As in any programming language, it is good practice to comment the code well. You can start a comment with a hash mark (#) anywhere in the line; any text after the hash mark is not executed and is treated as a comment. You end a command with a semicolon (;) or a new line. Commands finished by a semicolon can be combined in a single line. Similarly, as in T-SQL, explicitly ending a command with a semicolon is a better practice than just entering a new command on a new line. Here is the first example of R code, showing a comment and using the contributors() function to list the authors and other contributors to the language:

# R Contributors 
contributors(); 

The abbreviated results are:

R is a project which is attempting to provide a modern piece of
statistical software for the GNU suite of software.
    
The current R is the result of a collaborative effort with
contributions from all over the world.
    
    
Authors of R.
    
R was initially written by Robert Gentleman and Ross Ihaka-also known as "R & R"
of the Statistics Department of the University of Auckland.
    
Since mid-1997 there has been a core group with write access to the R
source, currently consisting of
...

In R, help is always at your fingertips. With the help() function, you can get onto help first, and then search for the details you need. Using help.start() gives the links to the free documentation about R. With help("options"), you can examine the global options that are used to affect the way in which R computes and displays the results. You can get help for a specific function. For example, help("exp") displays the help for the exponential function. You can use a shorter version of the help function—just the question mark. For example, ?"exp" also displays help for the exponential function. With example("exp"), you can get examples of usage of the exponential function. You can also search in help using a command—either help.search("topic") or ??"topic". With RSiteSearch("exp"), you can perform an online search for documentation about exponential functions over multiple R sites. The following code summarizes these help options:

# Getting help on help 
help(); 
# General help 
help.start(); 
# Help about global options 
help("options"); 
# Help on the function exp() 
help("exp"); 
# Examples for the function exp() 
example("exp"); 
# Search 
help.search("constants"); 
??"constants"; 
# Online search  
RSiteSearch("exp"); 

Finally, there is also the demo() function. This function runs some more advanced demo scripts, showing you the capabilities of R code. For example, the following call to this function shows you some graphic capabilities:

demo("graphics");

In RStudio, you get the code to execute in the Console window (bottom-left window by default). You need to move the cursor to that window and hit the Enter key to execute the first part of the code. You get the first graph in the bottom-right window. Then you hit the Enter key a couple of times more to scroll through all demo graphs. One part of the code that creates a nice pie chart is shown here:

pie.sales <- c(0.12, 0.3, 0.26, 0.16, 0.04, 0.12); 
names(pie.sales) <- c("Blueberry", "Cherry", "Apple", 
                      "Boston Cream", "Other", "Vanilla Cream"); 
pie(pie.sales, 
    col = c("purple","violetred1","green3","cornsilk","cyan","white")); 
 
title(main = "January Pie Sales", cex.main = 1.8, font.main = 1); 
title(xlab = "(Don't try this at home kids)", cex.lab = 0.8, font.lab = 3); 

The graphical results are shown in the following figure:

Demo pie chart

All of the objects created exist in memory. Each session has its own workspace. When you finish a session, you can save the workspace or the objects from memory to disk in an .RData file and load them in the next session. The workspace is saved in the folder from which R reads the source code files, or in a default folder. You can check the objects in the current workspace with the objects() function or with the ls() function. You can check the working folder, or directory, with the getwd() call. You can change it interactively with the setwd(dir) function, where the dir parameter is a string representing the new working directory. You can remove single objects from the workspace and memory with the rm(objectname) function, or a full list of objects using the same function with the list of objects as a parameter (you will learn about lists and other data structures later in this chapter). There are many more possibilities, including saving images, getting the history of recent commands, saving the history, reloading the history, and even saving a specific object to a specific file. You will learn about some of them later in this chapter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.37.10