R is short for SSIS

May 18, 2014
By

(This article was first published on You Know, and kindly contributed to R-bloggers)

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 his blog: You Know.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.