<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/using-r-in-sql-server-reporting-services-ssrs/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://tomaztsql.wordpress.com/2018/03/04/using-r-in-sql-server-reporting-services-ssrs/"> R – TomazTsql</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>SQL Server Reporting services (SSRS) is an outstanding tool for creating, deploying and managing paginated, mobile, KPI reports as well as Power BI reports. Tool provides simple way to share and get data insights in your corporate environment.</p>
<p><img data-orig-file="https://tomaztsql.files.wordpress.com/2018/03/ss-reporting-services-all-together.png?w=456" data-orig-size="469,331" data-medium-file="https://tomaztsql.files.wordpress.com/2018/03/ss-reporting-services-all-together.png?w=456?w=300" data-attachment-id="4948" data-image-meta="{" data-image-description="" src="https://tomaztsql.files.wordpress.com/2018/03/ss-reporting-services-all-together.png?w=456" data-permalink="https://tomaztsql.wordpress.com/2018/03/04/using-r-in-sql-server-reporting-services-ssrs/ss-reporting-services-all-together/#main" data-large-file="https://tomaztsql.files.wordpress.com/2018/03/ss-reporting-services-all-together.png?w=456?w=469" alt="ss-reporting-services-all-together" data-image-title="ss-reporting-services-all-together" sizes="(max-width: 469px) 100vw, 469px" data-comments-opened="1" class="wp-image-4948" data-recalc-dims="1"></p>
<p>(Photo source: <a rel="nofollow" href="https://docs.microsoft.com/en-us/sql/reporting-services/create-deploy-and-manage-mobile-and-paginated-reports" target="_blank">Microsoft docs</a>)</p>
<p>Using the privileges of R language to enrich your data, your statistical analysis or visualization is a simple task to get more out of your reports.</p>
<p>The best practice to embed R code into SSRS report is to create stored procedure and output the results to report. To demonstrate this, we will create two reports; one that will take two input parameters and the second one to demonstrate the usage of R visualization.</p>
<p>First, We will create a designated database and generate some sample data:</p>
<pre>CREATE DATABASE R_SSRS;
GO
USE R_SSRS;
GO

CREATE TABLE R_data (
 v1 INT ,v2 INT,v_low INT,v_high INT,letter CHAR(1));
GO

CREATE OR ALTER PROCEDURE generate_data
AS
BEGIN
  INSERT INTO R_data(v1,v2,v_low,v_high,letter)
  SELECT TOP 10
    CAST(v1.number*RAND() AS INT) AS v1
   ,CAST(v2.number*RAND() AS INT) AS v2
   ,v1.low AS v_low
   ,v1.high AS v_high
   ,SUBSTRING(CONVERT(varchar(40), NEWID()),0,2) AS letter
  FROM master..spt_values AS v1
  CROSS JOIN master..spt_values AS v2
  WHERE  v1.[type] = 'P' AND v2.[type] = 'P'
 ORDER BY NEWID() ASC;
END
GO
EXEC generate_data;
GO 10</pre>
<p>For the first report we will create a dynamic report, where user will be able to select two parameters. Based on select of the parameters, the resulted dataset will be passed to R, where with additional R code, we will generate summary statistics.</p>
<p>The idea is, to have the values “v1” and “letter” parametrized:</p>
<pre>SELECT * FROM R_data
WHERE 
 v1 > 400 -- replace this with parameter; e.g.: @v1
AND letter IN ('1','2','3') -- replace this with parameter; e.g.: @letters</pre>
<p>So in this case, we will create a stored procedure, that will get the data from T-SQL and call external procedure for execution of R Code. This procedure will be as:</p>
<pre>CREATE OR ALTER PROCEDURE sp_R1(
   @v1 INT
  ,@lett VARCHAR(20)
) AS
BEGIN
 DECLARE @myQuery NVARCHAR(1000)

  CREATE TABLE #t (let CHAR(1))
  INSERT INTO #t
  SELECT value
  FROM STRING_SPLIT(@lett,',')
 
 SET @myQuery = N'
    SELECT * FROM R_data
    WHERE 
    v1 > '+CAST(@v1 AS VARCHAR(10))+'
    AND letter IN (SELECT * FROM #t)'

EXEC sp_execute_external_script
 @language = N'R'
 ,@script = N'
   df &lt;- InputDataSet 
   OutputDataSet &lt;- data.frame(summary(df))'
  ,@input_data_1 = @myQuery
WITH RESULT SETS
((v1 NVARCHAR(100)
 ,v2 NVARCHAR(100)
 ,freq NVARCHAR(100)))
END;
GO</pre>
<p>By testing, we can see that passing both parameters (one single-valued and one multi-valued) will return result of R code:</p>
<pre>EXEC sp_R1 @v1 = 200, @lett = '1,2,3'</pre>
<p>Both of these parameters will be used in report for selection on data (or filtering the data). In <a rel="nofollow" href="https://www.microsoft.com/en-us/download/details.aspx?id=53613" target="_blank">SQL Server 2016 Report Builder</a>, we create a blank report, add data source and create a new dataset, using stored procedure:</p>
<p><img data-orig-file="https://tomaztsql.files.wordpress.com/2018/03/report1.png?w=456" data-orig-size="501,414" data-medium-file="https://tomaztsql.files.wordpress.com/2018/03/report1.png?w=456?w=300" data-attachment-id="4954" data-image-meta="{" data-image-description="" src="https://tomaztsql.files.wordpress.com/2018/03/report1.png?w=456" data-permalink="https://tomaztsql.wordpress.com/2018/03/04/using-r-in-sql-server-reporting-services-ssrs/report1/#main" data-large-file="https://tomaztsql.files.wordpress.com/2018/03/report1.png?w=456?w=501" alt="report1" data-image-title="report1" sizes="(max-width: 501px) 100vw, 501px" data-comments-opened="1" class="wp-image-4954" data-recalc-dims="1"></p>
<p>Once the dataset is added and stored procedure tested, reporting builder will automatically create report parameters:</p>
<p><img data-orig-file="https://tomaztsql.files.wordpress.com/2018/03/report2.png?w=456" data-orig-size="235,256" data-medium-file="https://tomaztsql.files.wordpress.com/2018/03/report2.png?w=456?w=235" data-attachment-id="4955" data-image-meta="{" data-image-description="" src="https://tomaztsql.files.wordpress.com/2018/03/report2.png?w=456" data-permalink="https://tomaztsql.wordpress.com/2018/03/04/using-r-in-sql-server-reporting-services-ssrs/report2/#main" data-large-file="https://tomaztsql.files.wordpress.com/2018/03/report2.png?w=456?w=235" alt="report2" data-image-title="report2" sizes="(max-width: 235px) 100vw, 235px" data-comments-opened="1" class="wp-image-4955" data-recalc-dims="1"></p>
<p>In addition, we need to set up the parameters, so, adding additional dataset for the paramteres to have a controlled set of available values, and adding this pool of values to each of the parameters, by selecting the dataset, where the parameters will be taken from.</p>
<p><img data-orig-file="https://tomaztsql.files.wordpress.com/2018/03/report3.png?w=379&amp;h=275&amp;fit=379%2C275&amp;resize=379%2C275" data-orig-size="530,385" data-medium-file="https://tomaztsql.files.wordpress.com/2018/03/report3.png?w=379&amp;h=275&amp;fit=379%2C275&amp;resize=379%2C275?w=300" data-attachment-id="4956" data-image-meta="{" data-image-description="" src="https://tomaztsql.files.wordpress.com/2018/03/report3.png?w=379&h=275&fit=379%2C275&resize=379%2C275" data-permalink="https://tomaztsql.wordpress.com/2018/03/04/using-r-in-sql-server-reporting-services-ssrs/report3/#main" data-large-file="https://tomaztsql.files.wordpress.com/2018/03/report3.png?w=379&amp;h=275&amp;fit=379%2C275&amp;resize=379%2C275?w=530" alt="report3" data-image-title="report3" sizes="(max-width: 379px) 100vw, 379px" width="379" data-comments-opened="1" class="wp-image-4956" data-recalc-dims="1" height="275"></p>
<p>For the multi-valued parameter, we also need to specify that user can select more than one value at the time. By letting the reporting builder know that “lett” parameter will be parsed back as a string of multiple values, we must select “Allow multiple values”.</p>
<p><img data-orig-file="https://tomaztsql.files.wordpress.com/2018/03/report4.png?w=356&amp;h=326&amp;fit=356%2C326&amp;resize=356%2C326" data-orig-size="421,385" data-medium-file="https://tomaztsql.files.wordpress.com/2018/03/report4.png?w=356&amp;h=326&amp;fit=356%2C326&amp;resize=356%2C326?w=300" data-attachment-id="4957" data-image-meta="{" data-image-description="" src="https://tomaztsql.files.wordpress.com/2018/03/report4.png?w=356&h=326&fit=356%2C326&resize=356%2C326" data-permalink="https://tomaztsql.wordpress.com/2018/03/04/using-r-in-sql-server-reporting-services-ssrs/report4/#main" data-large-file="https://tomaztsql.files.wordpress.com/2018/03/report4.png?w=356&amp;h=326&amp;fit=356%2C326&amp;resize=356%2C326?w=421" alt="report4" data-image-title="report4" sizes="(max-width: 356px) 100vw, 356px" width="356" data-comments-opened="1" class="wp-image-4957" data-recalc-dims="1" height="326"></p>
<p>Once this is completed, both parameters will be shown as a drop-down selection boxes, where user will get the results back, based upon the selection made.</p>
<p><img data-orig-file="https://tomaztsql.files.wordpress.com/2018/03/report5.png?w=420&amp;h=190&amp;fit=420%2C190&amp;resize=420%2C190" data-orig-size="508,230" data-medium-file="https://tomaztsql.files.wordpress.com/2018/03/report5.png?w=420&amp;h=190&amp;fit=420%2C190&amp;resize=420%2C190?w=300" data-attachment-id="4958" data-image-meta="{" data-image-description="" src="https://tomaztsql.files.wordpress.com/2018/03/report5.png?w=420&h=190&fit=420%2C190&resize=420%2C190" data-permalink="https://tomaztsql.wordpress.com/2018/03/04/using-r-in-sql-server-reporting-services-ssrs/report5/#main" data-large-file="https://tomaztsql.files.wordpress.com/2018/03/report5.png?w=420&amp;h=190&amp;fit=420%2C190&amp;resize=420%2C190?w=508" alt="report5" data-image-title="report5" sizes="(max-width: 420px) 100vw, 420px" width="420" data-comments-opened="1" class="wp-image-4958" data-recalc-dims="1" height="190"></p>
<p>At the end, report should look very similar to this, where the table is result generated by R code, the rest is T-SQL results and the beautiful Reporting Services.<img data-orig-file="https://tomaztsql.files.wordpress.com/2018/03/report6.png?w=450&amp;h=473&amp;fit=556%2C473" data-orig-size="744,633" data-medium-file="https://tomaztsql.files.wordpress.com/2018/03/report6.png?w=450&amp;h=473&amp;fit=556%2C473?w=300" data-attachment-id="4959" data-image-meta="{" data-image-description="" src="https://tomaztsql.files.wordpress.com/2018/03/report6.png?w=450&h=473&fit=556%2C473" data-permalink="https://tomaztsql.wordpress.com/2018/03/04/using-r-in-sql-server-reporting-services-ssrs/report6/#main" data-large-file="https://tomaztsql.files.wordpress.com/2018/03/report6.png?w=450&amp;h=473&amp;fit=556%2C473?w=700" alt="report6" data-image-title="report6" sizes="(max-width: 556px) 100vw, 556px" data-comments-opened="1" class="wp-image-4959" data-recalc-dims="1"></p>
<p>For the second report, we will create a new stored procedure, that will generate  a boxplot for selected values retrieved from the user selection.</p>
<p>The procedure will roughly remain the same, just the R code will be changed in order to be able to return the graph (complete procedure is in the code):</p>
<pre>EXEC sp_execute_external_script
 @language = N'R'
 ,@script = N'
  df &lt;- InputDataSet 
  image_file &lt;- tempfile()
  jpeg(filename = image_file, width = 400, height = 400)
  boxplot(df$v1~df$letter)
  dev.off()
  OutputDataSet &lt;- data.frame(data=readBin(file(image_file, &quot;rb&quot;), what=raw(), n=1e6))'</pre>
<p> </p>
<p>In addition to that, on our new report, we will need to show the returned value of this procedure (or simply result) as a graph.</p>
<p>On report canvas drag an image and change the settings of this image as following:</p>
<p><img data-orig-file="https://tomaztsql.files.wordpress.com/2018/03/report7.png?w=456" data-orig-size="886,395" data-medium-file="https://tomaztsql.files.wordpress.com/2018/03/report7.png?w=456?w=300" data-attachment-id="4964" data-image-meta="{" data-image-description="" src="https://tomaztsql.files.wordpress.com/2018/03/report7.png?w=456" data-permalink="https://tomaztsql.wordpress.com/2018/03/04/using-r-in-sql-server-reporting-services-ssrs/report7/#main" data-large-file="https://tomaztsql.files.wordpress.com/2018/03/report7.png?w=456?w=700" alt="report7" data-image-title="report7" sizes="(max-width: 700px) 100vw, 700px" data-comments-opened="1" class="wp-image-4964" data-recalc-dims="1"></p>
<p>Users will be now able to selected the values and based on their selection, the R graph generated inside SSRS report will be changed respectively.</p>
<p>This next Figure show just this, how simple it is for the user to change the selection (add or remove new values) and the boxplot graph is redrawn with updated values.</p>
<p><img data-orig-file="https://tomaztsql.files.wordpress.com/2018/03/report8.png?w=456" data-orig-size="1524,850" data-medium-file="https://tomaztsql.files.wordpress.com/2018/03/report8.png?w=456?w=300" data-attachment-id="4965" data-image-meta="{" data-image-description="" src="https://tomaztsql.files.wordpress.com/2018/03/report8.png?w=456" data-permalink="https://tomaztsql.wordpress.com/2018/03/04/using-r-in-sql-server-reporting-services-ssrs/report8/#main" data-large-file="https://tomaztsql.files.wordpress.com/2018/03/report8.png?w=456?w=700" alt="report8" data-image-title="report8" sizes="(max-width: 700px) 100vw, 700px" data-comments-opened="1" class="wp-image-4965" data-recalc-dims="1"></p>
<p>Both demos should help you leverage the usage of R scripts and R visualizations in your corporate reporting service and bring some additional user interface dynamics into it.</p>
<p>As always, the complete code and reports are available at <a rel="nofollow" href="https://github.com/tomaztk/R_SSRS_reports" target="_blank">Github</a>. And code will also hold my random generated dataset, if you want to get same results as in this blogpost.</p>
<p>Happy RCoding <img alt="🙂" src="https://i0.wp.com/s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f642.png?w=456&ssl=1" class="wp-smiley" data-recalc-dims="1"></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/03/04/using-r-in-sql-server-reporting-services-ssrs/"> 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/mCmpfiankUI" height="1"></a></div>