<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/running-r-scripts-within-in-database-sql-server-machine-learning/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://tomaztsql.wordpress.com/2018/10/14/running-r-scripts-within-in-database-sql-server-machine-learning/"> R – TomazTsql</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>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.</p>
<p>Using R function <a rel="nofollow" href="https://www.rdocumentation.org/packages/base/versions/3.5.1/topics/source?tap_a=5644-dce66f&tap_s=10907-287229" target="_blank">source</a> is the function to achieve this. Storing all definitions, functions, classes on one place can help enterprises achieve faster installation and safer environment usage.</p>
<p>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.</p>
<p><img data-orig-file="https://tomaztsql.files.wordpress.com/2018/10/2018-10-14-20_42_44-rstudio.png?w=456" data-orig-size="615,506" data-medium-file="https://tomaztsql.files.wordpress.com/2018/10/2018-10-14-20_42_44-rstudio.png?w=456?w=300" data-attachment-id="5139" data-image-meta="{" data-image-description="" src="https://tomaztsql.files.wordpress.com/2018/10/2018-10-14-20_42_44-rstudio.png?w=456" data-permalink="https://tomaztsql.wordpress.com/2018/10/14/running-r-scripts-within-in-database-sql-server-machine-learning/2018-10-14-20_42_44-rstudio/" data-large-file="https://tomaztsql.files.wordpress.com/2018/10/2018-10-14-20_42_44-rstudio.png?w=456?w=605" alt="2018-10-14 20_42_44-RStudio.png" data-image-title="2018-10-14 20_42_44-RStudio" sizes="(max-width: 605px) 100vw, 605px" data-comments-opened="1" class="wp-image-5139" data-recalc-dims="1"></p>
<p>And the rest is just to refer to this file in any R environment or R script with simple call using source.</p>
<pre>setwd(&quot;C:\\MyFiles\\R_setup&quot;)
source(&quot;util.R&quot;)</pre>
<p>Any new R script will have now all the functions and libraries included by default. Testing it with the simple function call is straightforward:</p>
<pre>x &lt;- c(1,2,3,4,5)
y &lt;- c(2,3,4,5,6)

mysummary(x)
sss(x,y)</pre>
<p>and the result is, as expected</p>
<p><img data-orig-file="https://tomaztsql.files.wordpress.com/2018/10/2018-10-14-20_49_11-rstudio.png?w=456" data-orig-size="212,197" data-medium-file="https://tomaztsql.files.wordpress.com/2018/10/2018-10-14-20_49_11-rstudio.png?w=456?w=212" data-attachment-id="5140" data-image-meta="{" data-image-description="" src="https://tomaztsql.files.wordpress.com/2018/10/2018-10-14-20_49_11-rstudio.png?w=456" data-permalink="https://tomaztsql.wordpress.com/2018/10/14/running-r-scripts-within-in-database-sql-server-machine-learning/2018-10-14-20_49_11-rstudio/" data-large-file="https://tomaztsql.files.wordpress.com/2018/10/2018-10-14-20_49_11-rstudio.png?w=456?w=212" alt="2018-10-14 20_49_11-RStudio" data-image-title="2018-10-14 20_49_11-RStudio" sizes="(max-width: 212px) 100vw, 212px" data-comments-opened="1" class="wp-image-5140" data-recalc-dims="1"></p>
<p>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.</p>
<p>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:</p>
<pre>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(&quot;C:\\MyFiles\\R_setup\\util.R&quot;) 
    x &lt;- InputDataSet
    x &lt;- as.numeric(x)
   out &lt;- addOne(x)
  OutputDataSet &lt;- data.frame(as.numeric(out))'
,@input_data_1 = N'SELECT n FROM (VALUES(1)) AS tbl(n)'
WITH RESULT SETS
((
FunctionResult VARCHAR(100)
))</pre>
<p>Result is the same as if ran from and other R environment, but this time, I am running it from SQL Server.</p>
<p><img data-orig-file="https://tomaztsql.files.wordpress.com/2018/10/2018-10-14-21_00_02-read_external_r_script_from_r_database_services-sql-tomazk_mssqlserver2017-adv.png?w=456" data-orig-size="207,112" data-medium-file="https://tomaztsql.files.wordpress.com/2018/10/2018-10-14-21_00_02-read_external_r_script_from_r_database_services-sql-tomazk_mssqlserver2017-adv.png?w=456?w=207" data-attachment-id="5142" data-image-meta="{" data-image-description="" src="https://tomaztsql.files.wordpress.com/2018/10/2018-10-14-21_00_02-read_external_r_script_from_r_database_services-sql-tomazk_mssqlserver2017-adv.png?w=456" data-permalink="https://tomaztsql.wordpress.com/2018/10/14/running-r-scripts-within-in-database-sql-server-machine-learning/2018-10-14-21_00_02-read_external_r_script_from_r_database_services-sql-tomazk_mssqlserver2017-adv/" data-large-file="https://tomaztsql.files.wordpress.com/2018/10/2018-10-14-21_00_02-read_external_r_script_from_r_database_services-sql-tomazk_mssqlserver2017-adv.png?w=456?w=207" alt="2018-10-14 21_00_02-read_external_r_script_from_R_database_Services.sql - TOMAZK_MSSQLSERVER2017.Adv" data-image-title="2018-10-14 21_00_02-read_external_r_script_from_R_database_Services.sql – TOMAZK_MSSQLSERVER2017.Adv" sizes="(max-width: 207px) 100vw, 207px" data-comments-opened="1" class="wp-image-5142" data-recalc-dims="1"></p>
<p>There are couple of thoughts to point out, though:</p>
<p>1) have the util.R file or any external R file validated (!)</p>
<p>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:</p>
<pre><code></code>if(!is.element('AGD', installed.packages()[,1])){
install.packages('AGD')} else {
library(AGD)}</pre>
<p>instead of just referencing the package by using library(AGD).</p>
<p>3) there should be more IF statements used regarding the R core engine and to check the availability and compatibility of new packages.</p>
<p>4) not all packages can be used with Microsoft R engine and the current R version.</p>
<p> </p>
<p>Enjoy R and SQL Server and happy coding.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://tomaztsql.wordpress.com/2018/10/14/running-r-scripts-within-in-database-sql-server-machine-learning/"> R – TomazTsql</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/_VB5g6U8Rak" height="1"></a></div>