{"id":1947,"date":"2016-05-24T16:30:00","date_gmt":"2016-05-24T14:30:00","guid":{"rendered":"https:\/\/www.sqlinthewild.co.za\/?p=1947"},"modified":"2016-05-23T23:09:26","modified_gmt":"2016-05-23T21:09:26","slug":"sql-server-2016-features-r-services","status":"publish","type":"post","link":"https:\/\/www.sqlinthewild.co.za\/index.php\/2016\/05\/24\/sql-server-2016-features-r-services\/","title":{"rendered":"SQL Server 2016 features: R services"},"content":{"rendered":"<p>One of the more interesting features in SQL 2016 is the integration of the R language.<\/p>\n<p>For those who haven\u2019t seen it before, R is a statistical and data analysis language. It\u2019s been around for ages, and has become popular in recent years.<\/p>\n<p>R looks something like this (and I make no promises that this is well-written R). Taken from a <a href=\"https:\/\/github.com\/GilaMonsterZA\/Code-Off\/tree\/master\/code_off-10\">morse-code related challenge<\/a><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">MessageLetters &lt;- str_split(Message, &quot;&quot;)\r\n\r\nMessageEncoded &lt;- list(1:length(MessageLetters))\r\n\r\nListOfDots &lt;- lapply(lapply(c(MaxCharacterLength:1), function(x) rep.int(&quot;.&quot;, times = x)), function(x) str_c(x, collapse=''))\r\nListOfDashes &lt;- lapply(lapply(c(MaxCharacterLength:1), function(x) rep.int(&quot;-&quot;, times = x)), function(x) str_c(x, collapse=''))\r\n<\/pre>\n<p>If you&#8217;re interested in learning R, I found the <a href=\"http:\/\/www.amazon.com\/Learning-R-Richard-Cotton\/dp\/1449357105\/\">Learning R<\/a> book to be very good.<\/p>\n<p>SQL 2016 offers the ability to run R from a SQL Server session. It\u2019s not that SQL suddenly understands R, it doesn\u2019t. Instead it can call out to the R runtime, pass data to it and get data back<\/p>\n<p>Installing the R components are very easy.<\/p>\n<p><a href=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2016\/05\/2016-05-23_09-39-43.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;\" title=\"2016-05-23_09-39-43\" src=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2016\/05\/2016-05-23_09-39-43_thumb.png\" alt=\"2016-05-23_09-39-43\" width=\"209\" height=\"143\" border=\"0\" \/><\/a><\/p>\n<p>And there\u2019s an extra licence to accept.<\/p>\n<p><a href=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2016\/05\/2016-05-23_09-44-48.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"1950\" data-permalink=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2016\/05\/24\/sql-server-2016-features-r-services\/2016-05-23_09-44-48\/\" data-orig-file=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2016\/05\/2016-05-23_09-44-48.png\" data-orig-size=\"795,502\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"2016-05-23_09-44-48\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2016\/05\/2016-05-23_09-44-48-300x189.png\" data-large-file=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2016\/05\/2016-05-23_09-44-48.png\" class=\"alignnone wp-image-1950\" src=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2016\/05\/2016-05-23_09-44-48.png\" alt=\"2016-05-23_09-44-48\" width=\"480\" height=\"303\" srcset=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2016\/05\/2016-05-23_09-44-48.png 795w, https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2016\/05\/2016-05-23_09-44-48-300x189.png 300w, https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2016\/05\/2016-05-23_09-44-48-768x485.png 768w, https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2016\/05\/2016-05-23_09-44-48-765x483.png 765w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/a><\/p>\n<p>It\u2019s worth noting that the pre-installed Azure gallery image for RC3 does not include the R services. Whether the RTM one will or not remains to be seen, but I\u2019d suggest installing manually for now.<\/p>\n<p>Once installed, it has to be enabled with sp_configure.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">EXEC sp_configure 'external scripts enabled', 1\r\nRECONFIGURE<\/pre>\n<p>It\u2019s not currently very intuitive to use. The current way R code is run is similar to dynamic SQL, with the same inherent difficulties in debugging.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">EXEC sp_execute_external_script\r\n  @language = N'R',\r\n  @script = N'data(iris)\r\n    OutputDataSet &lt;- head(iris)'\r\n  WITH RESULT SETS ((&#x5B;Sepal.Length] NUMERIC(4,2) NOT NULL, &#x5B;Sepal.Width] NUMERIC(4,2) NOT NULL, &#x5B;Petal.Length] NUMERIC(4,2) NOT NULL, &#x5B;Petal.Width]\u00a0 NUMERIC(4,2) NOT NULL, &#x5B;Species] VARCHAR(30)));\r\ngo<\/pre>\n<p>It\u2019s possible to pass data in as well, using a parameter named @input_data_1 (there\u2019s no @input_data_2) and from what I can tell from the documentation @parameter1, which takes a comma-delimited list of values for parameters defined with @params. There\u2019s no examples using these that I can find, so it\u2019s a little unclear how they precisely work.<\/p>\n<p>See <a title=\"https:\/\/msdn.microsoft.com\/en-us\/library\/mt604368.aspx\" href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/mt604368.aspx\">https:\/\/msdn.microsoft.com\/en-us\/library\/mt604368.aspx<\/a> and <a title=\"https:\/\/msdn.microsoft.com\/en-us\/library\/mt591993.aspx\" href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/mt591993.aspx\">https:\/\/msdn.microsoft.com\/en-us\/library\/mt591993.aspx<\/a> for more details.<\/p>\n<p>It&#8217;s not fast. The above piece of T-SQL took ~4 seconds to execute. This is on an Azure A3 VM. Not a great machine admittedly, but the R code, which just returns the first 6 rows of a built-in data set, ran in under a second on my desktop. This is likely not something you&#8217;ll be doing as part of an OLTP process.<\/p>\n<p>I hope this external_script method is temporary. It\u2019s ugly, hard to troubleshoot, and it means I have to write my R somewhere else, probably R Studio, maybe Visual Studio, and move it over once tested and working. I\u2019d much rather see something like<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">CREATE PROCEDURE GetIrisData\r\n  WITH Language = 'R' -- or USQL or Python or \u2026\r\n  AS\r\n\u2026\r\nGO<\/pre>\n<p>Maybe in SQL Server 2020?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the more interesting features in SQL 2016 is the integration of the R language. For those who haven\u2019t seen it before, R is a statistical and data analysis language. It\u2019s been around for ages, and has become popular&#8230; <a class=\"read-more-button\" href=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2016\/05\/24\/sql-server-2016-features-r-services\/\">(Read more)<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"New Blog Post: SQL Server 2016 features: R services","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15,16],"tags":[],"class_list":["post-1947","post","type-post","status-publish","format-standard","hentry","category-sql-server","category-syndication"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7h6n-vp","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/1947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/comments?post=1947"}],"version-history":[{"count":9,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/1947\/revisions"}],"predecessor-version":[{"id":1957,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/1947\/revisions\/1957"}],"wp:attachment":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/media?parent=1947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/categories?post=1947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/tags?post=1947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}