CTCaliberTek
Course: R Programming Path: From Beginner to Reproducible Reports · Lesson 1 of 8Log in to track your progress and earn a certificate when you finish the course.
Programming

R for Beginners: Install R and RStudio and Run Your First Script

Published November 9, 2026·Updated November 9, 2026·9 min read

Edited by , Senior Data Analyst. How we make our content

Like SaveLog in to like, save, and track progress.

Introduction

R is a language built for statistics, data analysis, and visualization. It is used in research, finance, public health, and analytics teams, and it has one of the richest collections of free packages for working with data. This lesson gets a working setup on your computer and teaches the habits that save the most time later: using scripts instead of typing everything into the console, keeping each analysis in its own project, and installing packages the right way.

Prerequisites

  • A Windows, macOS, or Linux computer where you can install software
  • No programming experience is required
  • About 30 minutes

Step-by-Step Guide

Step 1: Install R, then RStudio

R itself is the language and the engine that runs your code. Download it from CRAN, the Comprehensive R Archive Network, at cran.r-project.org, choosing the installer for your operating system. Then install RStudio Desktop from Posit, which is a free editor built around R. Install R first, because RStudio looks for an existing R installation when it starts. You can run R without RStudio, but RStudio gives you an editor, a console, a plot viewer, a file browser, and package tools in one window, which is why most learners and many professionals use it.

Step 2: Explore the console

When RStudio opens, the Console pane is where R executes commands one at a time. Type an expression and press Enter to see the result immediately. Assign a value to a name with the arrow operator, which reads as 'gets'. R also accepts an equals sign for assignment, but the arrow is the convention in R code and keeps assignment visually separate from naming function arguments. Names are case sensitive, so total and Total are different objects. To see everything you have created in the current session, look at the Environment pane or call ls().

1 + 1
price <- 19.99
quantity <- 3
total <- price * quantity
total          # prints 59.97
sqrt(16)       # 4
?sqrt          # opens the help page for a function

Step 3: Write a script instead of typing in the console

Anything you type in the console is gone when you close R, and nobody can rerun it. Instead, choose File, New File, R Script, and write your code there. Put the cursor on a line and press Ctrl+Enter, or Cmd+Enter on a Mac, to send that line to the console, or select several lines and run them together. Save the file with a .R extension. Lines starting with a hash symbol are comments, which R ignores, so use them to explain why a step exists. A script is the beginning of reproducible work: someone else, or you in six months, can open it and run the same analysis from top to bottom.

# my_first_script.R
# Convert temperatures from Celsius to Fahrenheit
celsius <- c(0, 25, 37, 100)
fahrenheit <- celsius * 9 / 5 + 32
print(fahrenheit)   # 32.0  77.0  98.6 212.0

Step 4: Install and load packages

Packages add functions that are not part of base R. You install a package once per computer with install.packages, and you load it in every session where you need it with library. Beginners often put install.packages inside scripts, which reinstalls the package every time and annoys collaborators, so run it in the console instead and keep only the library calls in the script. The tidyverse is a collection of packages for data work that we will use throughout this course. Installing it downloads many packages and can take a few minutes.

install.packages("tidyverse")   # run once, in the console
library(tidyverse)              # run at the top of each script that uses it

Step 5: Use an RStudio Project and avoid setwd

R reads and writes files relative to its working directory. Many tutorials tell you to call setwd with a long absolute path, which breaks the moment the script moves to another computer. A better habit is to create an RStudio Project with File, New Project. Opening the project sets the working directory to the project folder automatically, so a relative path such as data/sales.csv works everywhere. Keep raw data in a data folder, scripts in the project root, and outputs in an output folder. Run getwd() at any time to see where R is currently looking for files.

getwd()                        # shows the current working directory
dir.create("data")             # make folders inside the project
file.exists("data/sales.csv")  # FALSE until you add the file

Step 6: Keep your sessions clean

By default RStudio offers to save your workspace to a hidden .RData file when you quit and reload it next time. That feels convenient, but it means results can depend on objects you created weeks ago and forgot about. In Tools, Global Options, General, set 'Save workspace to .RData on exit' to Never and untick restoring it at startup. Then restart R regularly with Session, Restart R, and rerun your script from the top. If the script runs cleanly from a fresh session, it is reproducible.

FAQs

Do I need RStudio to use R?
No. R runs on its own from a terminal or other editors such as Visual Studio Code. RStudio is a convenient environment, and its project and package tools are why it is the usual choice for beginners.
What is the difference between <- and = in R?
For ordinary assignment they behave the same. The convention is to use <- for assignment and = only when naming arguments inside a function call, which keeps code easy to read.
Why does library(somepackage) say there is no package called somepackage?
The package is not installed on this computer yet. Run install.packages with the package name in the console once, then call library again. Check the spelling, because package names are case sensitive.

Summary

You now have R and RStudio installed, you know the difference between the console and a script, and you have set up good habits: projects instead of setwd, packages installed once and loaded per script, and a clean session that you can restart at any time. In the next lesson you will learn how R stores data in vectors, which is the foundation of everything else in the language.

Related tutorials

Comments

Log in or create a free account to join the discussion.

  • No comments yet. Be the first to comment.