Download and Installation of R and R-Studio

This tutorial will teach you how to begin programming with R using RStudio. We’ll install R, and RStudio, a popular development environment for R. We’ll also learn how to install R-package and some key RStudio features to start programming in R on our own.

Download R

You can download the latest version of R from the Comprehensive R Archive Network (CRAN) website (https://cran.r-project.org/). Follow the instructions to install it on your computer. The version of R to download depends on our operating system. We can also install Microsoft R Open, the enhanced distribution of R from Microsoft. It includes additional capabilities for improved performance and reproducibility and support for Windows and Linux-based platforms.

  1. Click this link to download the latest stable version of R
  2. Select a CRAN location (a mirror site) close to you
  3. Click on the “Download R for Windows”
  4. Click on the “install R for the first time” link at the top corner of the page
  5. Click “Download R for Windows” and it will download to local download folder

Install R

Installation instruction of R in Windows and MAC could be found here. Detail Installation steps of Microsoft R Open in different operating systems can be found here.

Windows

  1. Open Windows Explorer
  2. Navigate to the directory where the downloaded R.4.2.#-win.exe (the latest version) file is exit.
  3. Double-click this exe file and follow the instruction as shown in the video below:

Mac OSX

Installing R on Mac OS is similar to Windows. The easiest way is to install it through CRAN by going to the CRAN downloads page and following description as as shown here. In brief, download the .pkg and open the .pkg file and follow the standard instructions for installing applications on MAC OS X.

Ubuntu

Step 1: Update and Upgrade the Ubuntu 22.04

To update and upgrade packages on Ubuntu 22.04, firstly, open up the terminal by pressing “CTRL+ALT+T” and execute the following command:

sudo apt update

sudo apt upgrade

Step 2: Add dependencies

Now, on your Ubuntu 22.04 system, download and install required dependencies. These dependencies are used to execute the R on Ubuntu 22.04. Run the following command for the specified purpose:

sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common

Step 3: Authenticate the packages

Authenticate the installed packages by writing out the following command:

sudo apt-key adv –keyserver keyserver.ubuntu.com –recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9

Step 4: Import GPG key

Next, import the GPG key in your system through below command:

wget -O- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | sudo gpg –dearmor | sudo tee /usr/share/keyrings/cran.gpg

Step 5: Add CRAN repository

After importing the GPG key, you need to add the CRAN repository that is highly recommended for R installation:

echo deb [signed-by=/usr/share/keyrings/cran.gpg] https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/ | sudo tee /etc/apt/sources.list.d/cran.list

sudo apt update

Step 6: Install R through CRAN repository Now, your Ubuntu 22.04 system is ready to install the R language from the CRAN repository. To do so, type the following command:

sudo apt install r-base

sudo apt-get install libcurl4-openssl-dev libssl-dev libxml2-dev liblapack-dev

Step 7: Check R -version

R –version

Open R-Base

After R installation in Windows, double click on the desktop icon or open the program from START to run R. R will be open as a Console window.

Figure 1: Interface of R-base

You can work in the console and the command line. However, the command line can be pretty daunting to a beginner. It is better to work in R Editor. First, you must create a New script from File menu. Any code you run in R-script output will be displayed in the console window. You can save all your R codes as an R script file and output in the console as an R-Data file.

RTools

Rtools is a collection of software tools that are used to build packages for the R programming language on Windows operating system. It includes various programs and libraries, such as make, gcc, g++, and others, that are needed to compile and build R packages that contain compiled code. Without Rtools, it is not possible to build such packages on Windows.

Rtools is not needed if you only use R for data analysis and use packages that only contain interpreted code. However, if you need to use a package that contains compiled code, you will need to install Rtools in order to use that package.

The version of may be installed from the Rtools installer. It is recommended to use the defaults, including the default installation location of C:.

When using R installed by the installer, no further setup is necessary after installing Rtools to build R packages from source. When using the default installation location, R and Rtools42 may be installed in any order and Rtools42 may be installed when R is already running.

Integrated Development Environment for R

R can be run in the command line and graphical user interfaces in integrated development environment (IDE). Below are the best programming IDE for R:

  1. RStudio
  2. R Tools for Visual Studio
  3. Rattle
  4. ESS
  5. Tinn-R
  6. R AnalyticalFlow
  7. Radiant
  8. RBox
  9. Code

Download and Installation RStudio

RStudio is one of the best integrated development environment (IDE) for R that includes a console, a terminal syntax-highlighting editor that supports direct code execution, as well as tools for plotting, history, debugging and workspace management. RStudio Desktop and RStudio Server for Windows, Mac, and Linux are open sources.

RStudio is now Posit. First, you have to download the latest version of RStudio according to your operating system from here.

For windows user, and just run the installation file and it normally detects your latest installed R version automatically. If you want to do some extra configuration, you need follow some steps which can be found here

For installation instruction of R and RStudio in Mac OS could be found here.

Installing R Studio on Ubuntu could be found here

We may open RStudio just double click Rstudio icon on your Desktop or on the task bar. The RStudio interface has four main panels:

  • Console: where you can type commands and see output.

  • Script editor: where you can type out commands and save to file.

  • Environment/History: environment shows all active objects and history keeps track of all commands run in console.

  • Files/Plots/Packages/Help etc.

Figure 2: RStudio interface

R Packages or Libraries

R Packages are collections of R functions, data, and compiled code in a well-defined format. The directory where packages are stored is called the library. We can install any R ‘package’ or multiple package directly from the console, using r-script and GUI (Tools > Install Packages) through internet.

Use install.packages() function in your console or in a script:

install.packages(“raster”, dependencies = TRUE)

For multiple packages:

install.packages(c(“raster”,“gstat”), dependencies = TRUE)

If we want see content of any library, just use help() function or package specific:

library(help=spatial)

R Markdown

R Markdown is an extension of the Markdown language that allows us to embed R code and its output within your documents. With R Markdown, we can create dynamic documents that integrate R code, results, and visualization with narrative text, all in a single document. R Markdown files have the extension “.Rmd” and can be rendered into various output formats such as HTML, PDF, Word, and more.

R Markdown documents contain three main components: 1. The YAML header specifies the document’s metadata, such as the title, author, and output format.

  1. The text body, contains the narrative text written in Markdown syntax.

  2. Code chunks are blocks of R code that can be executed and rendered within the document.

R Markdown is a powerful tool for creating reproducible research documents, reports, presentations, and websites. It allows the integration of code, results, and visualization into a single document that others can easily share and reproduce.

A brief overview could be found here:

For a brief tutorial, please visit here.

R-Markdown Project

To create a new R Project, open RStudio and click on

File -> New Project

R-Markdown Document

To create a new R Markdown document, open RStudio and click on

File -> New File -> R Markdown.

You will be prompted to choose a document format. Choose the format that suits your needs. In this tutorial, we will choose the “HTML” format.

Give your document a title and author name, then click “Create”. This will create a new R Markdown document with the extension “.Rmd”.

Your new R Markdown document will contain a template that you can use as a starting point. You can start adding content to your document by editing the template. The template contains a YAML header, which specifies the title, author, and output format of the document, and a Markdown body, which is where you can write your text.

You can add headings, lists, and links using Markdown syntax. You can also add inline R code and code chunks to your document. Once you have added content to your document, you can preview it by clicking on the “Knit” button at the top of the document editor. This will compile your R Markdown document into an output format (e.g. HTML, PDF, Word) based on the output format specified in the YAML header.

Here below cheat sheet rmarkdown syntax:

Figure 3: R-Markdown Cheat Sheet -1

R-Markdown Cheat Sheet -1

Figure 4: R-Markdown Cheat Sheet -2

Quarto

Like, R Markdown, Quarto is an open-source scientific and technical publishing system built on Pandoc, a universal document converter. It creates dynamic content with Python, R, Julia, and Observablefor publishing high-quality articles, reports, presentations, websites, blogs, and books in HTML, PDF, MS Word, ePub, and more.

Like R Markdown, Quarto uses Knitr to execute R code. So it can render most existing Rmd files without modification. The difference between R Markdown and Quarto is related to output formats. Quarto includes many more built-in output formats (and more options for customizing each format). Quarto also has native features for particular project types like Websites, Books, and Blogs (rather than relying on external packages).

To run Quarto on RStudio, you must install it in your system. You can download it from here according to your operating system. After installation, you need to install the quarto package to render documents from the R console.

install.packages(“quarto”)

quarto::quarto_render(“hello.qmd”)

Andy Field have series of YouTube video tutorials related to Quarto. Link of these are in below:

Quarto part 1

Quarto part 2

Quarto part 3

Quarto part 4

Quarto part 5