Data Science Manager - Accenture
M.S. in Predictive Analytics - DePaul University
Me
Malter Analytics
GitHub
LinkedIn
YouTube Channel
Twitter
Kaggle
Other Work
General Assembly
AriBall
Media
Built In
This post is about how to connect to a Microsoft SQL Server database from within R. This process allows you to manipulate and run SQL queries on live data directly in R. Step 1 is not neccessary depending on the process used in Step 2, but the directions used to create an ODBC connection in Step 1 are for Windows computers.
Note: This step is not neccessary if you use the second option in Step 2.
There are two ways to connect to the database using the RODBC package.
con <- odbcConnect(dsn, uid = "", pwd = "")
con <- odbcDriverConnect("Driver= {SQL Server};
Server=XXX; Database=XXX;
Uid=XXX; Pwd=XXX")
Once your connection has been made, you can run any SQL query exaclty as you would within Microsoft SQL Server
df <- sqlQuery(channel = con, "SELECT column_name1, column_name2
FROM table_name
WHERE column_name1 operator value;")
From here, your data is now stored within the df variable and you can work on your data frame as you normally would within R.
comments powered by Disqus