Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- INSERT INTO dbo.Plots
- EXEC sp_execute_external_script
- @language = N'R'
- ,@script = N' df <- inputDataSet; #read input data
- image_file = tempfile(); #create a temporary file
- jpeg(filename = image_file, width=500, height=500); #create a JPEG graphic device
- hist(df$Ages); #plot the histogram
- dev.off(); #dev.off returns the number and name of the new active device (after the specified device has been shut down). (device = graphical device)
- #file() opens a file, in this case the image. rb = read binary
- #readBin() reads binary data. what = described the mode of the data. In this case, it''s raw data. n = maximum number of records to read.
- #data.frame converts the data to a data frame, which is required as output by SQL Server. The result is written to the OutputDataset variable.
- OutputDataset <- data.frame(data=readBin(file(image_file,"rb"),what=raw(),n=1e6));
- '
- ,@input_data_1 = N'SELECT Ages = DATEDIFF(YEAR,[BirthDate],GETDATE())
- FROM [AdventureWorksDW2014].[dbo].[DimCustomer];'
- ,@input_data_1_name = N'inputDataSet'
- ,@output_data_1_name = N'OutputDataset'
- INSERT INTO dbo.Plots(plot, name)
- VALUES(
- (EXEC sp_execute_external_script
- @language = N'R'
- ,@script = N' df <- inputDataSet; #read input data
- image_file = tempfile(); #create a temporary file
- jpeg(filename = image_file, width=500, height=500); #create a JPEG graphic device
- hist(df$Ages); #plot the histogram
- dev.off(); #dev.off returns the number and name of the new active device (after the specified device has been shut down). (device = graphical device)
- #file() opens a file, in this case the image. rb = read binary
- #readBin() reads binary data. what = described the mode of the data. In this case, it''s raw data. n = maximum number of records to read.
- #data.frame converts the data to a data frame, which is required as output by SQL Server. The result is written to the OutputDataset variable.
- OutputDataset <- data.frame(data=readBin(file(image_file,"rb"),what=raw(),n=1e6));
- '
- ,@input_data_1 = N'SELECT Ages = DATEDIFF(YEAR,[BirthDate],GETDATE())
- FROM [AdventureWorksDW2014].[dbo].[DimCustomer];'
- ,@input_data_1_name = N'inputDataSet'
- ,@output_data_1_name = N'OutputDataset')
- , 'testname'
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement