R Programming - Part 0 - Installation

19 Sep 2020 - Benjamin Loi

Introduction

R is a programming language widely used in the field of statistics as well as Earth System Science. This article will explain how to install R and run a R script.

Installing R and RStudio

Note: R and RStudio can also be installed through Anaconda, which is explained later.

R can be obtained from the official website. Choose the download page corresponding to your operating system and find the latest version. For Windows user, select base and download the newest version. For Mac user, simply download the package file of the newest version.

R-00-00 For Windows User, R-00-01
R-00-02 For Mac User, R-00-03

We also want to download RStudio for an enhanced interactive editor and interface. It can be acquired here. For our purposes, picking the free RStudio Desktop is enough.

After successful installation of R and RStudio, launching RStudio will look like this.

R-00-04

Installing R and RStudio with Anaconda

Anaconda is originally a package management tool for Python. However, the ability to install R and related packages is added in later versions of Anaconda. This is very convenient as we can create a virtual environment for R with a particular set-up. Anaconda can be downloaded from the official website, by scrolling down to the bottom part of the page.

R-00-a00

After successful installation of Anaconda, for Windows user, search for Anaconda prompt and open it. While for Mac user, simply start the terminal. Next, we install R and other frequently used packages in a new environment by typing conda create -n r_env r-essentials r-base rstudio where we can replace r_env with any other name.

R-00-a01
R-00-a02

Subsequently, we can activate the environment by typing conda activate r_env, with r_env replaced with the name of the environment. Finally, we open RStudio by simply typing Rstudio in the command prompt.

R-00-a03
R-00-a04

Running your first command and script

To run any command, we can directly type in the console prompt indicated by the blue > sign. For demonstration purpose, we run the simplest command print() where inside the parenthesis we provide the string to be printed.

R-00-05

We can also pack all the codes into a script to be executed at once. To do so, we create a new script from the tool bar as follow.

R-00-06

Now we try again to do a print statement. Save and run the script by clicking on the respective buttons. As expected, it should give the same outcome as when we directly enter in the console prompt.

R-00-07

Next: Part 1 - Basic Operations

Last Update: 2020-10-12