Relational databases via ODBC
Source: Dev.to
Introduction
With a different function (and often a different package) for almost every file format, it’s easy to feel overwhelmed—especially when juggling multiple arguments and dependencies. However, once you understand which tools to use for which data types, importing data into R becomes straightforward and efficient.
This guide is designed to be your one‑stop reference. The next time you search for “How do I load XYZ file into R?” you’ll know exactly where to look.
What This Tutorial Covers
We’ll walk through importing the most commonly used data formats in R, including:
- TXT and CSV files
- JSON and XML data
- HTML tables
- Excel workbooks
- SAS, SPSS, Stata datasets
- MATLAB and Octave files
- Relational databases via ODBC
We’ll also share a handy importing hack for quick, ad‑hoc analysis.
Preparing Your R Workspace Before Importing Data
Setting the Working Directory
Most projects store related files in a single folder. Setting this folder as your working directory simplifies file imports.
# Show current working directory
getwd()
To change your working directory:
setwd("path/to/your/folder")
Once set, R will automatically look for files in this location—saving you from repeatedly typing long file paths.
Cleaning the Workspace
Leftover objects from previous sessions can cause subtle and frustrating errors. Starting clean is often best.
rm(list = ls())
Pro tip: Avoid saving the workspace on exit unless necessary. Fresh sessions reduce debugging headaches.
Loading TXT, CSV, and Other Delimited Files
Reading Text Files (.txt)
Delimited text files use separators such as tabs, commas, or semicolons.
Example structure
Category V1 V2A 3 2B 5 6B 2 3A 4 8A 7 3
# Default (tab‑delimited)
df This is a lifesaver for exploratory work, though formatting issues may occur.
Using Packages for Advanced Imports
Before using package‑based import functions:
install.packages("packageName") library(packageName)
Importing JSON Files
install.packages("rjson")
library(rjson)
# From a local file or URL
json_data **Next steps?** If you’re moving toward predictive modeling, scaling analytics, or deploying AI‑driven solutions, explore our **AI Consulting Services** to accelerate outcomes.
About Perceptive Analytics
“To enable businesses to unlock value in data.”
For over 20 years, we’ve partnered with more than 100 clients—from Fortune 500 companies to mid‑sized firms—to solve complex data‑analytics challenges. Our services include delivering end‑to‑end Tableau consulting services and operating as a trusted Power BI consulting company, turning data into strategic insight.
We would love to talk to you!
You. Do reach out to us.