Sunday, May 18, 2014

R is short for SSIS

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)
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)
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...
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).

No comments:

Post a Comment