Created
September 27, 2016 21:22
-
-
Save ramnov/f7feec84e71218eb2c7a74d094484eb2 to your computer and use it in GitHub Desktop.
Stored Procedure to plot distribution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE PROCEDURE [dbo].[PlotDistribution] | |
AS | |
BEGIN | |
SET NOCOUNT ON; | |
EXECUTE sp_execute_external_script | |
@language = N'R', | |
@script = N' | |
library("reshape2") | |
library("ggplot2") | |
# creating output directory | |
mainDir <- ''C:\\temp\\plots'' | |
dir.create(mainDir, recursive = TRUE, showWarnings = FALSE) | |
setwd(mainDir); | |
print("Creating output plot files:", quote=FALSE) | |
# Open a jpeg file and output ggplot in that file. | |
dest_filename = tempfile(pattern = ''ggplot_'', tmpdir = mainDir) | |
dest_filename = paste(dest_filename, ''.jpg'',sep="") | |
print(dest_filename, quote=FALSE); | |
jpeg(filename=dest_filename, height=3900, width = 6400, res=300); | |
#filtering numeric columns | |
numeric_cols <- sapply(loans, is.numeric) | |
#turn the data into long format (key->value) | |
loans.lng <- melt(loans[,numeric_cols], id="is_bad") | |
#plot the distribution for is_bad={0/1} for each numeric column | |
print(ggplot(aes(x=value, group=is_bad, colour=factor(is_bad)), data=loans.lng) + geom_density() + facet_wrap(~variable, scales="free")) | |
dev.off() | |
', | |
@input_data_1 = N'SELECT * FROM [dbo].[LoanStats]', | |
@input_data_1_name = N'loans' | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment