R is short for SSIS

[This article was first published on You Know, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

R is Short for SSIS Data scientists often identify a need to join data from different, unlinked servers. One standard tool for accomplishing this is an SSIS package to consolidate the data onto one of the servers. For the analyst who wants to keep everything in one file for simplicity and repeatabililty, there is another option: the RODBC package (authored by Brian Ripley).

To avoid issues of uninstalled packages, I use this general method.
pkg <- c("RODBC", "ggplot2")
inst <- pkg %in% installed.packages()
if (length(pkg[!inst]) > 0) install.packages(pkg[!inst])
lapply(pkg, library, character.only = TRUE)
First query data from the first server:
channel1 <- odbcDriverConnect(connection = "Driver={SQL Server};Server=yourserver;Database=yourdatabase;Trusted_Connection=Yes;")
query1 <- "select * from customers where contractID IS NOT NULL"
data1 <- sqlQuery(channel1, query1)
odbcClose(channel1)
Then query data from the second server:
channel2 <- odbcDriverConnect(connection = "Driver={SQL Server};Server=yourserver;Database=yourdatabase;Trusted_Connection=Yes;")
query2 <- "select * from products"
data2 <- sqlQuery(channel2, query2)
odbcClose(channel2)
Join (merge) the two resulting dataframes:
data.merge <- merge(data1, data2, by = "InvoiceID")
Do something interesting and save the results:
p <- ggplot(data = data.merge) + something_worth_graphing...
p
ggsave(filename = "NeatChart.pdf", plot = p)
Now you will have one R file that pulls all of the data you need, processes it, and saves the output.
Bonus idea: The tables to query may be quite large. Peel out the limiting factor (such as a list of customer IDs) and use the paste0 command to assemble a dynamic second query (using the WHERE clause).

To leave a comment for the author, please follow the link and comment on their blog: You Know.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)