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.

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

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

and the result is, as expected

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:

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

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:

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.

Related