The Big R-Book. Philippe J. S. De Brouwer
http://www.r-fiddle.org
RStudio
For the user, who is not familiar with the command line, it is highly recommendable to use an IDE, such as RStudio (see https://www.rstudio.com
). Later on – for example in Chapter 32 “R Markdown” on page 879 – we will see that RStudio has some unique advantages over the R-console in store, that will convince even the most traditional command-line-users.
IDE
RStudio
Whether you use standard R or MRAN, using RStudio will enhance your performance and help you to be more productive.
MRAN
Rstudio is an integrated development environment (IDE) for R and provides a console, editor with syntax-highlighting, a window to show plots and some workspace management.
IDE
integrated development environment
Rstudio can be downloaded from https://www.rstudio.com
for free.
RStudio is also in the repositories of most Linux distributions. That means that there is no need to go to the website or RStudio, but it is possible to download R, and install it with just one line in the CLI of your OS. For example, in Debian and its derivatives, this would be:
# Note that the first 2 lines are just to make sure that # you are using the latest version of the repository. sudo apt-get update sudo apt-get upgrade # Install RStudio: sudo apt-get install rstudio
This process is very simple: provide your admin password and the script will take care of everything. Then use your preferred way to start the new software. R can be started by typing R
in the command prompt and RStudio will launch with the command rstudio
.
An important note is that RStudio will work with the latest version of R that is installed. Also when you install MRAN, RStudio will automatically load this new version.
Basic arithmetic
The basic operators work as one would expect. Simply type in the R terminal 2+3
followed by ENTER
and R will immediately display the result.
addition product power
#addition 2 + 3 #product 2 * 3 #power 2**3 2∧3 #logic 2 < 3 x <- c(1,3,4,3) x.mean <- mean(x) x.mean y <- c(2,3,5,1) x+y
Usually, white space is not important for R.
For example, mean ( c (c + pi, 2 ) )
will do exactly the same as mean(c(1+pi,2))
. However, it is a good habit to add a white space before and after operators and after the comma. This will improve readability, which makes the debugging process easier and is helpful for other people who want to read your code.
Editing variables interactively
To create a variable x
, type:
scan()
x <- scan()
will start an interface that invites you to type all values of the vector one by one.
In order to get back to the command prompt: type enter without typing a number (ie. leave one empty to end).
This is only one of the many ways to get data into R. Most probably you will use a mix of defining variables in the code and reading in data from files. See for example Chapter 15 “Connecting R to an SQL Database” on page 327.
To modify an existing variable, one can use the edit()
function edit()
edit(x)
The edit function will open the editor that is defined in the options. While in RStudio, this is a specially designed pop-up window with buttons to save and cancel, in the command line interface (CLI) this might be vi
. The heydays of this fantastic editor are over and you might never have seen it before. It is not really possible to use vi without reading the manual (e.g. via the man vi
command on the OS CLI or an online tutorial). To get out of vi
, type: [ESC]:q![ENTER]
. Note that we show the name of a key within square brackets and that all the other strings are just one keystroke each.
Batch mode
R is an interpreted language and while the usual interaction is typing commands and getting the reply appear on the screen, it is also possible to use R in batch mode.
batch mode
functions
1 create a file test.R
2 add the content print(“Hello World”)
3 run the command line Rscript test.R
4 now, open R and run the command source(“test.R”)
source()
1 add in the file
my_function <- function(a,b) { a + b }
1 now repeat step 4 and run my_function(4,5)
In this section we will present you with a practical introduction to R, it is not a formal introduction. If you would like to learn more about the foundations, then we recommend the documentation provided by the R-core team here: https://cran.r-project.org/doc/manuals/r-release/R-lang.pdf
.
4.2. Variables
As any computer language, R allows to use variables to store information. The variable is referred to by its name. Valid names in R consist of numbers and characters. Evenmost special characters can be used.
variables
In R, variables
can contain letters as well as “_” (underscore) and “.” (dot), and
variables must start with a letter (that can be preceded with a dot).
For example, my_var.1
and my.Cvar
are valid variables, but _myVar
, my%var