BRM | Lab #0 | Fall 2025

Introduction to R and RStudio

Before the Lab

  • Install R and RStudio (instructions below.)

Goals

  • Get familiar with the R development environment
  • Setting up a project
  • Working with R scripts




1. Installing R and RStudio

What is R?R is a language and environment for statistical computing and graphics.”1.

To install R:

What is R Studio? “R Studio is a free and open source integrated development environment (IDE) for R, a programming language for statistical computing and graphics.”2

To install RStudio:

  • Go to http://www.rstudio.com/
  • Click on the “Download R Studio” button.
  • Select the “Desktop Open Source” version.
  • Choose the correct link for download (Mac or Windows).
  • Install.
  • Open R Studio (not R): If you have a Windows PC and R Studio does not show on your desktop you have to search for it on your Start Menu.

During this course we will always work on R Studio as it is more user-friendly. But you need to install R to use R Studio.




2. The R Studio Environment

2.1 Basic layout

When you first open RStudio, you will be greeted by three panels:

1. The interactive R console (entire left)

  • Here you can type commands and see their output.

2. Environment / History (tabbed in upper right)

  • Environment: This tab stores the things you create during your R session.

  • History: This is a record of your activity, you can save it for later use.

3. Files / Plots / Packages / Help / Viewer (tabbed in lower right)

  • Files: This tab shows you the files and folders in your workspace (or work folder).

  • Plots: This tab shows you all the plots/graphics you produce.

  • Packages: This tab shows you a list of the add-ons available and indicate whether they are on/off.

  • Help: This tab gives you a search box where you can search for additional information.

4. The R script(s) and data view

  • The R script is where you keep a record of your work.


Your screen will initially look similar to this:



Once you open files, such as R scripts, an editor panel will also open in the top left.



to top



2.2 Creating a Project

Keeping all the files associated with a project organized together – input data, R scripts, analytics results, figures – is such a wise and common practice that RStudio has built-in support for this via its projects.

A good project layout will ultimately make your life easier:

  • It will help ensure the integrity of your data
  • It makes it simpler to share your code with someone else (a lab mate, collaborator, or supervisor)
  • It allows you to easily upload your code with your manuscript submission
  • It makes it easier to pick the project back up after a break


To create a new project in RStudio:

  1. Click the File menu button, then New Project
  2. Click New Directory
  3. Click New Project
  4. Type in the name of the directory to store your project, e.g. BRM-Labs
  5. Choose the project’s location (you may place it inside your BRM folder)
  6. Click the Create Project button

Now when we start R in this project directory, or open this project with RStudio, all of our work on this project will be entirely self-contained in this directory.

Switching between projects can be done from the File menu (Open Project).

The simplest way to open an RStudio project once it has been created is to click through your file system to get to the directory where it was saved and double click on the .Rproj file. This will open RStudio and start your R session in the same directory as the .Rproj file. All your data, plots and scripts will now be relative to the project directory. RStudio projects have the added benefit of allowing you to open multiple projects at the same time each open to its own project directory. This allows you to keep multiple projects open without them interfering with each other.


📓 Best practices for project organisation:

Although there is no single best way to lay out a project, there are general principles to adhere to that will make project management easier:

  • Treat data as read-only.

  • This is probably the most important goal of setting up a project. Data are typically time-consuming and / or expensive to collect. Working with them interactively where they can be modified means you are never sure of where the data came from, or how they have been modified. Separate raw and tidied data.

  • In many cases your data will be “messy”: it will need significant pre-processing to get into a format R (or any other programming language) will find useful. Storing these files in a separate folder, and creating a second “read-only” data folder to hold the “cleaned” data sets can prevent confusion between the two sets. Treat the generated output as disposable.

  • Anything generated by your scripts should be treated as disposable: it should all be possible to regenerate from your scripts.


📌 Some general recommendations for organizing a project:

  • Keep all project files under the directory that was automatically created when you named the project.
  • Put text documents associated with the project in a directory called doc.
  • Put raw data and metadata in the data directory, and files generated during cleanup and analysis in the results directory, and R scripts in code directory.
  • Name all files to reflect their content or function.


to top



2.3 R Packages

Though the base version of R is already quite powerful and includes several functionalities, we can expand R’s capabilities by installing additional packages. Think of R packages as smartphone applications – they let us do more with our phones. There are a large number of packages that serve different purposes, written by people around the world.

In order to install a package type install.packages("write.name.of.package.here") (this requires a working internet connection)3. Installation may take some time. Be patient and don’t worry about the messages in red showing on the console unless they explicitly say error.

We only need to install each package once. Once we install a package, we can delete or comment (#) that line of code so that we don’t run it again by accident. We can activate a package by typing library(write.name.of.package.here).

The Packages tab shows us which packages we have installed. If the box is checked that means the respective package is active. We can check the box next to each package in order to activate it (as an alternative to using the library() command). Whenever we start a new R session, we have to activate the necessary packages.

One important package for these labs is the tidyverse package4. If you haven’t installed this package yet, please install tidyverse and stargazer5 now.

# Install tidyverse and stargazer
install.packages("tidyverse")
install.packages("stargazer")

As of this writing, there are over 10,000 packages available on CRAN (the comprehensive R archive network).


to top



2.4 Finding Help

R and every package provide help files for functions. There are two commands for searching for help on any function that is in a package loaded into your R session:

?function_name
help(function_name)

This will load up a help page in RStudio. Each help page is broken down into sections:

  • Description: An extended description of what the function does.
  • Usage: The arguments of the function and their default values.
  • Arguments: An explanation of the data each argument is expecting.
  • Details: Any important details to be aware of.
  • Value: The data the function returns.
  • See Also: Any related functions you might find useful.
  • Examples: Some examples for how to use the function.

Different functions might have different sections, but these are the main ones you should be aware of.

One of the most daunting aspects of R is the large number of functions available. It would be prohibitive, if not impossible to remember the correct usage for every function you use. Luckily, the help files mean you don’t have to!

When looking for resources on debugging, it is crucial to know more details about your session (such as attached packages and their version numbers). You can achieve that with:

sessionInfo()


Another great resource when getting started are cheat sheets!

You can google them or go to Help menu -> Cheat Sheets


to top



3. Getting Started

3.1 Using the console as a calculator

You can use the Console just like a calculator. Try performing some calculations:

1+1
## [1] 2
2*(10-2)*4
## [1] 64
8^(1/3)
## [1] 2
# lines that start with # are not evaluated and are named comments


R has built-in mathematical functions. For example:

sqrt(25) # square root
## [1] 5
exp(2) # e^2
## [1] 7.389056
log(1) # natural log
## [1] 0


Some useful mathematical functions:


Function Description
abs(x) Takes the absolute value of x
log(x, base=y) Takes the logarithm of x with base y; if base is not specified, returns the natural logarithm
exp(x) Returns the exponential of x
sqrt(x) Returns the square root of x
factorial(x) Returns the factorial of x (x!)
choose(x,y) Returns the number of possible combinations when drawing y elements at a time from x possibilities


R does not have a command for declaring a variable. A variable is created the moment you first assign a value to it. To assign a value to a variable, use the <- sign. To output (or print) the variable value, just type the variable name:

name <- "John"
age <- 40

name   # output "John"
## [1] "John"
age    # output 40
## [1] 40

Once you store an object in your environment you can interact with it directly by using its name

age + 10 # interact with object
## [1] 50

Note that the previous command does not change the value of age because we did not store the result in a variable. The following command would change the value of age.

age <- age + 10
age
## [1] 50

Objects or variables can also contain text (aka strings). Everything in R is an object, and an object can contain anything.

my_class <- 'BRM is awesome'

R is case sensitive and spelling sensitive.

# try running 
# My_class


to top



3.2 R Scripts

When working with R, you should keep a record of your code so that you can keep track of what you have done and re-use it later. This will help in making your research reproducible. In this course we will always work on R scripts and not on the Console.

To create a new R script click on the blank sheet with a green plus sign on the top left corner of your screen and selecting the option R script.

You can save your script with by going to File --> Save as... and choosing a file name such as brm-lab1.R.


Working on an R script

  • In order to send a command line from the R script to the Console just put the cursor on the selected line and press the Run button. A faster way is using the keyboard shortcut: hit the keys ctrl+Enter (Windows users) or cmd+Enter (Mac users).

  • You can also select several command lines and execute them all in one go. - You can write comments on your R script (lines that the program will ignore) by using the symbol “#” on the beginning of a word or phrase. Text preceded by the “#” symbol will be ignored when executed.

  • You can comment out several code lines at once with the shortcut: Ctrl + Shift + C (Windows) or Cmd + Shift + C (Mac)

# This is a comment, it will be ignored by the program.
                                                               
1 + 1 # the sum is not commented and will be executed 
## [1] 2
  • Every time you want to re-run a command you have already typed in your script, just put the cursor on top of it and enter it, you do not need to retype it.

  • At the end, don’t forget to save your script before you close R Studio.

  • R is case sensitive and spelling sensitive.

🆘 Cancelling commands

  • Press Esc to cancel a command.

  • Canceling a command isn’t only useful for killing incomplete commands: you can also use it to tell R to stop running code (for example if it’s taking much longer than you expect), or to get rid of the code you’re currently writing.


🆘 Cleaning out the work space

  • Use the broom icon or the command rm(list = ls()).

  • It is a good idea to clean the work space, restart R (using from the Session menu), and re-run your analysis to check that the code you’re saving is complete and correct (or at least rule out obvious problems.)


to top



4. Coding Copilots & AI Helpers

As you continue learning R and working on research projects, you may benefit from tools that assist with writing and understanding code. These include coding copilots and AI helpers.


4.1 Coding Copilots

A coding copilot is a tool that provides real-time code suggestions and autocompletions based on what you’re typing.

We recommend GitHub Copilot, which integrates with editors like RStudio. As a student, you can register for a free GitHub Student Developer Pack, which includes access to GitHub Copilot at no cost.

⚠️ Always review Copilot’s suggestions critically — it may produce incorrect or inefficient code.


to top



4.2 AI Helpers

AI helpers such as ChatGPT, Gemini, Claude, and Copilot Chat can answer questions, explain code, or help troubleshoot errors in plain language.

While powerful, these tools should be used responsibly — they’re best for learning and idea generation, not for outsourcing critical thinking or research design.

⚠️ Use AI to enhance your understanding, not to bypass it.


to top




  1. http://www.r-project.org/about.html↩︎

  2. http://en.wikipedia.org/wiki/RStudio↩︎

  3. Alternatively, we can go to the packages tab and click Install. A message box will appear, and we simply need to write the name of the package. Make sure that there is a tick next to Install dependencies, and then click Install.↩︎

  4. “The tidyverse is an opinionated collection of R packages designed for data science. All packages share an underlying design philosophy, grammar, and data structures.” (from tidyverse.org)↩︎

  5. “The stargazer command produces LaTeX code, HTML code and ASCII text for well-formatted tables that hold regression analysis results from several models side-by-side. It can also output summary statistics and data frame content. stargazer supports a large number model objects from a variety of packages.” (from Marek Hlavac, Package-stargazer)↩︎