This guide covers how to read data files in R across different environments, including:
-
RStudio (Desktop & Server)
-
Google Colab (with R kernel)
-
Posit.cloud
We’ll cover:
-
File types (
.csv,.xlsx) and required packages -
Reading from local machine
-
Reading from a different directory
-
Reading from an online source (e.g., GitHub)
A CSV (Comma-Separated Values) file stores data as plain text, where each row represents one record and each value is separated by a comma.
They are lightweight, portable, and can be opened in many programs including Excel, Notepad, R, and Python.
CSV files are commonly used for data exchange because they are simple and widely supported.
When working with CSV files in R, you can read them from:
-
The same directory as your project or script.
-
A different folder on your computer using the full file path.
-
An online source, such as a file hosted on GitHub, using its direct link.
Excel files have extensions .xls or .xlsx. They can store multiple sheets, formatting, formulas, and charts, but R will only import the raw tabular data.
When working with Excel files in R, you can:
Read data from the default first sheet.
-
Specify a sheet name or number to read from a particular sheet.
-
Read from files stored locally or from a direct download link online.
-
Keep your data files organized in a dedicated data folder inside your project.
-
Always check your current working directory to ensure R is looking in the right place.
-
Use relative paths for file locations when possible so your project is portable.
-
After importing, always inspect the first few rows, the structure, and the summary statistics of your dataset to confirm it loaded correctly.