“Here comes a headache”
Kevin Flynn, Tron
At the end of this session you should be able to
For interactive scripting, you need to type the R commands directly into an R console. An R console opens automatically when you type “R” into a DOS or Linux shell provided that you have installed R on your system before. Try the mother of all examples - type the following in your R console and press Enter:
Alternatively, you can assign the string to a variable using “←” which is R's version of “=”. Afterwards, you can print the content of the variable (note, this time, no quoting inside the print function since you call a variable, not a string):
my_statement <- "Hello World" print(my_statement)
Of course, you can not only work with characters/strings but also with numbers. R has been invented for statistical computing, so there is a lot of stuff available. For now, we just introduce the basic arithmetic operators:
Beside these operators, R has many built-in functions like mean, median, square root, absolute value etc. An overview can be found at e. g. Quick R.
R offers a large functionality. This functionality is accessible through so called R packages which is the same as a library in any other programming language. To load an R package into your script so you can access the functions within it, just type the following in your R console or script:
library(<name of the library>)
If you want to use a library for the first time (on your system), it is very likely that you have to install it first. This is done by typing the following into an R console:
install.packages("<name of the library>")
Please note that if you want to load a library, you must not use “” inside the library() function. If you want to install a library, you have to put the name into “” inside the install.packages() function.
For more information on installing R packages, visit this tutorial from R-bloggers.
For more information and general help on R, visit the R project homepage where you e.g. also find manuals. If you need help for a specific package, have a look at the documentations of the standard library and the contributing packages.
?<package_name>in the R console. If you don't know which function you need, Google it (and look for results on the stackoverflow.com page)
Executing code in the R shell is good, using R Studio is superior. Along with Eclipse which is the standard choice for Java, Python or C programming, R Studio is one of the prime integrated development environments. Install it, run it, love it.
Basically, R Studio has four window frames. The upper left one is the standard frame for writing code. Below is the console which shows what happens, if the code is executed. Of course one can also directly type something into the console window (which is just the same R environment as the one which can be started from the operating system's bash). The upper right window displays all variables/data sets which have been used during code execution. Finally, the lower right window serves multiple purposes. Two nice features are the Help tab which displays the content of the R documentation of the respective package (just type
?<package_name in the console window) and the Plots tab which shows the results from any executed visualization command.
For more information see R Studio's documentation.