Running R scripts within in-database SQL Server Machine Learning

Having all the R functions, all libraries and any kind of definitions (URL, links, working directories, environments, memory, etc) in one file is nothing new, but sometimes a lifesaver.

Using R function source is the function to achieve this. Storing all definitions, functions, classes on one place can help enterprises achieve faster installation and safer environment usage.

So the idea is simple. Stack all the needed functions, classes, libraries and configurations you want to use in a specific environment and save it in a R  file.  Create a file with all the needed setups of libraries and functions, as seen below. We will call this file as util.R.

2018-10-14 20_42_44-RStudio.png

And the rest is just to refer to this file in any R environment or R script with simple call using source.

setwd("C:\\MyFiles\\R_setup")
source("util.R")

Any new R script will have now all the functions and libraries included by default. Testing it with the simple function call is straightforward:

x <- c(1,2,3,4,5)
y <- c(2,3,4,5,6)

mysummary(x)
sss(x,y)

and the result is, as expected

2018-10-14 20_49_11-RStudio

In my previous blog posts, I have mentioned also calling R scripts from SQL Server tables, external files and others. Using R source function is another way to pull in predetermined and preinstalled functions also in SQL Server in-database machine learning service (or machine learning server), that will make setting up same environments on client machines in enterprise environment much easier and faster. With just one update of the file, all client machines can read same definitions.

Using same util.R file, I have placed the file in the location where SQL Server R and workers will have access granted. By using sp_execute_external_script and external R file, this can be simplified:

USE AdventureWorks;
/* SELECT n FROM (VALUES(1),(2),(3),(4)) AS tbl(n) */
exec sp_execute_external_script
  @language = N'R'
 ,@script = N'
#have access to this folder for SQL SERVER /R workers  
   source("C:\\MyFiles\\R_setup\\util.R") 
    x <- InputDataSet
    x <- as.numeric(x)
   out <- addOne(x)
  OutputDataSet <- data.frame(as.numeric(out))'
,@input_data_1 = N'SELECT n FROM (VALUES(1)) AS tbl(n)'
WITH RESULT SETS
((
FunctionResult VARCHAR(100)
))

Result is the same as if ran from and other R environment, but this time, I am running it from SQL Server.

2018-10-14 21_00_02-read_external_r_script_from_R_database_Services.sql - TOMAZK_MSSQLSERVER2017.Adv

There are couple of thoughts to point out, though:

1) have the util.R file or any external R file validated (!)

2) all the libraries, that util.R file is referring to, must be installed. If not, the external file will not be loaded. This can simply be achieved by using:

if(!is.element('AGD', installed.packages()[,1])){
install.packages('AGD')} else {
library(AGD)}

instead of just referencing the package by using library(AGD).

3) there should be more IF statements used regarding the R core engine and to check the availability and compatibility of new packages.

4) not all packages can be used with Microsoft R engine and the current R version.

 

Enjoy R and SQL Server and happy coding.

Tagged with: , , , , , , ,
Posted in Uncategorized
2 comments on “Running R scripts within in-database SQL Server Machine Learning
  1. […] Running R scripts within in-database SQL Server Machine Learning […]

    Like

  2. Jared Prins says:

    This is very helpful, thank you.

    Your website and Packt publishing book “SQL Server 2017 machine learning services with R” have been a real help for me. Thank you for these resources!

    I’ve been searching for approaches to handling R package management and deployment for in-database SQL 2016. My list of stored procedures that use R is growing, and I have the need to separate my R libraries for different scripts. That is, my R code relies on different R library versions, but they all need to run on the same server.

    On my local system, I can use packrat. But can the script and the packrat library be moved to the server?

    I’ve also been wondering about the use of checkpoint or miniCRAN.

    Regardless of the solution, it seems to require having different library locations. I’m unsure how to approach this on the server and what some best practices might be. Any guidance you can provide is much appreciated.

    Cheers.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Categories
Follow TomazTsql on WordPress.com
Programs I Use
Programs I Use
Programs I Use
Rdeči Noski – Charity

Rdeči noski

100% of donations made here go to charity, no deductions, no fees. For CLOWNDOCTORS - encouraging more joy and happiness to children staying in hospitals (http://www.rednoses.eu/red-noses-organisations/slovenia/)

€2.00

Top SQL Server Bloggers 2018
TomazTsql

Tomaz doing BI and DEV with SQL Server and R, Python and beyond

Discover

A daily selection of the best content published on WordPress, collected for you by humans who love to read.

Revolutions

Tomaz doing BI and DEV with SQL Server and R, Python and beyond

tenbulls.co.uk

attaining enlightenment with sql server, .net, biztalk, windows and linux

SQL DBA with A Beard

He's a SQL DBA and he has a beard

Reeves Smith's SQL & BI Blog

A blog about SQL Server and the Microsoft Business Intelligence stack with some random Non-Microsoft tools thrown in for good measure.

SQL Server

for Application Developers

Business Analytics 3.0

Data Driven Business Models

SQL Database Engine Blog

Tomaz doing BI and DEV with SQL Server and R, Python and beyond

Search Msdn

Tomaz doing BI and DEV with SQL Server and R, Python and beyond

R-bloggers

Tomaz doing BI and DEV with SQL Server and R, Python and beyond

Ms SQL Girl

Julie Koesmarno's Journey In Data, BI and SQL World

R-bloggers

R news and tutorials contributed by hundreds of R bloggers

Data Until I Die!

Data for Life :)

Paul Turley's SQL Server BI Blog

sharing my experiences with the Microsoft data platform, SQL Server BI, Data Modeling, SSAS Design, Power Pivot, Power BI, SSRS Advanced Design, Power BI, Dashboards & Visualization since 2009

Grant Fritchey

Intimidating Databases and Code

Madhivanan's SQL blog

A modern business theme

Alessandro Alpi's Blog

SQL Server, Azure and DLM in a nutshell :D

Paul te Braak

Business Intelligence Blog

Sql Server Insane Asylum (A Blog by Pat Wright)

Information about SQL Server from the Asylum.

Gareth's Blog

A blog about Life, SQL & Everything ...

SQLPam's Blog

Life changes fast and this is where I occasionally take time to ponder what I have learned and experienced. A lot of focus will be on SQL and the SQL community – but life varies.

%d bloggers like this: