R and RStudio

Author
Affiliation

Md Rasel Biswas

IASDS, University of Dhaka

1 Introduction to R

  • R is an extremely powerful programming language for statistical computing, data analysis, and graphics.
  • R was developed in the early 1990s at the University of Auckland, New Zealand, by two statisticians:
    • Ross Ihaka, University of Auckland, New Zealand
    • Robert Gentleman, University of Auckland at that time
  • R is a dialect of the S programming language.

  • R is flexible and free to download under the GNU General Public License. It has been widely used in academic and research environments for several decades.
  • R is open source and is supported by an extensive user community.
  • R is currently maintained by the R Core Team.
  • CRAN (the Comprehensive R Archive Network) distributes R and additional packages contributed by the R user community.

2 Why should we learn R?

  • R is open source and freely available.
  • R is available for Windows, macOS, and Linux operating systems.
  • R has extensive and highly flexible graphical facilities capable of producing publication-quality figures.
  • R has an expanding collection of freely available packages that extend its capabilities.
  • R is widely used for data manipulation, statistical analysis, visualization, and machine learning.

2.1 Scripts

  • R is a command-driven program. All the steps used in an analysis, from reading the data to producing the final results, can be saved in a script and rerun without much effort.
    • For example, we only need to run the script again; we do not need to remember the sequence of clicks used in the analysis.
  • Working with scripts makes the steps used in an analysis clear, and the code can be inspected by others. This helps improve the code and identify mistakes.
  • Working with scripts also helps us understand the associated statistical methods more clearly.

2.2 Reproducibility

  • The term reproducibility is used when someone else, including your future self, can obtain the same results from the same dataset by using the same analysis coded in scripts.
  • Nowadays, funding agencies and peer-reviewed journals often expect analyses to be reproducible. Journals may also ask for the data and code before publishing an accepted manuscript.
  • R is an integral part of reproducible research. A small change in the data or analysis can be incorporated by running the scripts again.

3 Download R

  • Download the R installer from the Comprehensive R Archive Network (CRAN): https://cran.r-project.org.

  • To download the R installer for Windows:

    • click Download R for Windows;
    • click base; and
    • click Download R for Windows.

4 R interface

5 Download RStudio

  • Go to https://posit.co/download/rstudio-desktop/ to download RStudio Desktop.
  • Install R first, and then install RStudio.
  • RStudio is an integrated development environment (IDE) for R. It provides a graphical interface where we can write code, see the results, view plots, and inspect the objects created during programming.

R is the programming language.
RStudio is software designed to make working with R easier.

6 RStudio interface

The RStudio user interface has four primary panes:

  • Source pane: used to write and edit R scripts and other files.
  • Console pane: the workhorse of R. This is where R evaluates the code we write.
  • Environment pane: contains the Environment, History, Connections, and Tutorial tabs. The Environment tab displays the objects created during the current R session.
  • Output pane: contains the Files, Plots, Packages, Help, and Viewer tabs.

7 Working directory in RStudio

  • R is always pointed to a directory on the computer. This is called the working directory.
  • We can find the current working directory by running the getwd() function in the Console.
getwd()
#> [1] "/Users/isrtdu12/Documents/ast230-2026/r"
  • We can set the working directory manually in two ways.

7.1 Using the Console

Use the setwd() function and place the path of the directory inside quotation marks.

setwd("directory/path")
File paths

Use quotation marks around a file or directory path. On Windows, forward slashes are usually easier to use in R, for example, "D:/R-course/data".

7.2 Using the RStudio interface

Click the three-dot button in the Files tab to open the file browser and choose the directory.

After choosing the directory, click on the setting button and select Set As Working Directory.

Recommended practice

Keep the data, scripts, and other files for one analysis in the same main folder. This makes the analysis easier to organize and reproduce.

8 Practice

  1. Install R and RStudio on your computer.
  2. Open RStudio and identify its four main panes.
  3. Run getwd() in the Console.
  4. Create a folder for this course and set it as the working directory.