{"id":73887,"date":"2017-10-04T13:52:34","date_gmt":"2017-10-04T13:52:34","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=73887"},"modified":"2021-09-15T13:22:18","modified_gmt":"2021-09-15T13:22:18","slug":"sql-server-r-services-generating-sparklines-types-spark-graphs","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/databases\/sql-server\/bi-sql-server\/sql-server-r-services-generating-sparklines-types-spark-graphs\/","title":{"rendered":"SQL Server R Services: Generating Sparklines and Other Types of Spark Graphs"},"content":{"rendered":"<h4>The series so far:<\/h4>\n<ol>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/sql\/bi\/sql-server-r-services-basics\/\">SQL Server R Services: The Basics<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/sql\/bi\/sql-server-r-services-digging-r-language\/\">SQL Server R Services: Digging into the R Language<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/sql\/bi\/sql-server-r-services-working-ggplot2-statistical-graphics\/\">SQL Server R Services: Working with ggplot2 Statistical Graphics<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/sql\/bi\/sql-server-r-services-working-data-frames\/\">SQL Server R Services: Working with Data Frames<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/sql\/bi\/sql-server-r-services-generating-sparklines-types-spark-graphs\/\">SQL Server R Services: Generating Sparklines and Other Types of Spark Graphs<\/a><\/li>\n<li><a href=\"https:\/\/www.red-gate.com\/simple-talk\/sql\/bi\/sql-server-r-services-working-multiple-data-sets\/\">SQL Server R Services: Working with Multiple Data Sets<\/a><\/li>\n<\/ol>\n\n<p>Just about anything you can do with the R language you can do in SQL Server R Services. You can transform data, import R packages, and perform a wide range of analytics. You can even generate statistical visualizations that make the data easier to understand, as we discussed in the third article in this series, in which we used the <strong>ggplot2<\/strong> package to create several types of charts.<\/p>\n<p>In this article, we continue our discussion on visualizations, but switch the focus to sparklines and other spark graphs. As with many aspects of the R language, there are multiple options for generating spark graphs. For this article, we\u2019ll focus on using the <strong>sparkTable<\/strong> package, which allows us to create spark graphs and build tables that incorporate those graphs directly, a common use case when working with spark images.<\/p>\n<p>In the examples to follow, we\u2019ll import the <strong>sparkTable<\/strong> package and generate several graphs, based on data retrieved from the <strong>AdventureWorks2014<\/strong> sample database. We\u2019ll also build a table that incorporates the SQL Server data along with the spark graphs. Note, however, that this article focuses specifically on working with the <strong>sparkTable<\/strong> package. If you are not familiar with how to build R scripts that incorporate SQL Server data, refer to the previous articles in this series. You should understand how to use the <strong>sp_execute_external_script<\/strong> stored procedure to retrieve SQL Server data and run R scripts before diving into this article.<\/p>\n<h2>Creating the sparklines and other spark graphs<\/h2>\n<p>Spark graphs are small images embedded directly in text, lists, or tables to provide quick insight into related data. The <strong>sparkTable<\/strong> package supports four graph types: sparklines, sparkbars, sparkboxes, and sparkhists (histograms). Although the process for generating them is similar, each graph takes a different approach to representing the target data.<\/p>\n<p>The best way to understand how the graph types differ from one another is to see them in action. The following T-SQL code includes an R script that imports the <strong>sparkTable<\/strong> package, retrieves the SQL Server data, creates the four types of graphs, and exports each one to a .pdf file:<\/p>\n<pre class=\"theme:ssms2012-simple-talk lang:tsql decode:true\">DECLARE @rscript NVARCHAR(MAX);\r\n  SET @rscript = N'\r\n  \u00a0 # import sparkTable package\r\n  \u00a0 library(sparkTable)\r\n  \u00a0 # retrieve SQL Server data\r\n  \u00a0 salesdf &lt;- InputDataSet\r\n  \u00a0 # define spark objects\r\n  \u00a0 sline &lt;- newSparkLine(values = salesdf$Sales, height = .8, width = 3,\u00a0\r\n  \u00a0 \u00a0 lineWidth = .4, pointWidth = 3, showIQR = TRUE,\u00a0\r\n  \u00a0 \u00a0 allColors = c(\"red3\", \"green4\", \"cyan3\", \"white\", \"blue\", \"gray75\"))\r\n  \u00a0 sbar &lt;- newSparkBar(values = salesdf$Sales, height = .8, width = 3,\u00a0\r\n  \u00a0 \u00a0 barCol = rep(\"blue\", 3))\r\n  \u00a0 sbox &lt;- newSparkBox(values = salesdf$Sales, height = .8, width = 2,\u00a0\r\n  \u00a0 \u00a0 boxCol = c(\"black\", \"green\"))\r\n  \u00a0 shist &lt;- newSparkHist(values = salesdf$Sales, height = .8, width = 2,\u00a0\r\n  \u00a0 \u00a0 barCol = c(\"green\", \"green\", \"black\"))\r\n  \u00a0 # export spark objects\r\n  \u00a0 export(object = sline, outputType = \"pdf\",\u00a0\r\n  \u00a0 \u00a0 filename = file.path(\"C:\\\\DataFiles\", \"SparkLine\"))\r\n  \u00a0 export(object = sbar, outputType = \"pdf\",\u00a0\r\n  \u00a0 \u00a0 filename = file.path(\"C:\\\\DataFiles\", \"SparkBar\"))\r\n  \u00a0 export(object = sbox, outputType = \"pdf\",\u00a0\r\n  \u00a0 \u00a0 filename = file.path(\"C:\\\\DataFiles\", \"SparkBox\"))\r\n  \u00a0 export(object = shist, outputType = \"pdf\",\u00a0\r\n  \u00a0 \u00a0 filename = file.path(\"C:\\\\DataFiles\", \"SparkHist\"))';\r\n  DECLARE @sqlscript NVARCHAR(MAX);\r\n  SET @sqlscript = N'\r\n  \u00a0 SELECT t.Name AS Territories, h.Subtotal AS Sales\r\n  \u00a0 FROM Sales.SalesOrderHeader h INNER JOIN Sales.SalesTerritory t\r\n  \u00a0 \u00a0 ON h.TerritoryID = t.TerritoryID\r\n  \u00a0 WHERE t.[Group] = ''North America'' AND h.Subtotal &gt; 20000;';\r\n  EXEC sp_execute_external_script\r\n  \u00a0 @language = N'R',\r\n  \u00a0 @script = @rscript,\r\n  \u00a0 @input_data_1 = @sqlscript;\u00a0\r\n  GO<\/pre>\n<p>Again, you should refer to the previous articles in this series to understand how the <strong>sp_execute_external_script<\/strong> stored procedure works and how to import R packages and SQL Server data into the R script. With that in mind, let\u2019s jump to the first graph definition in the R script, in which we define is a sparkline graph:<\/p>\n<pre class=\"theme:ssms2012-simple-talk lang:tsql decode:true\">sline &lt;- newSparkLine(values = salesdf$Sales, height = .8, width = 3,\u00a0\r\n  \u00a0 lineWidth = .4, pointWidth = 3, showIQR = TRUE,\u00a0\r\n  \u00a0 allColors = c(\"red3\", \"green4\", \"cyan3\", \"white\", \"blue\", \"gray75\"))<\/pre>\n<p>To create the sparkline, we use the <strong>newSparkLine<\/strong> function, providing several arguments that define the graph. The first argument, <strong>values<\/strong>, specifies the data on which to base the sparkline, which in this case, is the <strong>Sales<\/strong> column in the <strong>salesdf<\/strong> data set. The remaining arguments determine how to format the sparkline:<\/p>\n<ul>\n<li><strong>height:<\/strong> The graph\u2019s total height in inches.<\/li>\n<li><strong>width:<\/strong> The graph\u2019s total width in inches.<\/li>\n<li><strong>lineWidth:<\/strong> The point width of the graph line.<\/li>\n<li><strong>pointWidth:<\/strong> The point width of the dots used to represent the minimum, maximum, and last values.<\/li>\n<li><strong>showIQR:<\/strong> An on\/off toggle that determines whether the interquartile range (IQR) is included in the graph.<\/li>\n<li><strong>allColors:<\/strong> The color to use for each of the graph\u2019s components.<\/li>\n<\/ul>\n<p>The <strong>allColors<\/strong> property takes six values, concatenated using the <strong>c<\/strong> function. The values must be specified in the following order:<\/p>\n<ul>\n<li>Minimal value dot<\/li>\n<li>Maximum value dot<\/li>\n<li>Last value dot<\/li>\n<li>Background<\/li>\n<li>Graph line<\/li>\n<li>IQR range<\/li>\n<\/ul>\n<p>After defining the sparkline and assigning it to the <strong>sline<\/strong> variable, we then define the remaining graphs. But before we get into those, let\u2019s skip ahead to the first instance of the <strong>export<\/strong> function:<\/p>\n<pre class=\"theme:ssms2012-simple-talk lang:tsql decode:true\">export(object = sline, outputType = \"pdf\",\u00a0\r\n  \u00a0 filename = file.path(\"C:\\\\DataFiles\", \"SparkLine\"))<\/pre>\n<p>The <strong>export<\/strong> function lets us save our graph as a .pdf, .eps, .png, or .svg file. The <strong>object<\/strong> argument specifies the object to be exported, in this case, our sparkline graph, <strong>sline<\/strong>. The <strong>outputType<\/strong> argument indicates that the sparkline should be saved to a .pdf file. We then use the <strong>file.path<\/strong> function to provide the path and filename value for the <strong>filename<\/strong> argument.<\/p>\n<p>When we run the T-SQL code, the R engine generates a .pdf file, which contains the sparkline shown in the following figure.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"496\" height=\"131\" class=\"wp-image-73901\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2017\/10\/documents-datafiles-scr.png\" alt=\"..\/..\/..\/..\/..\/..\/..\/..\/..\/Documents\/DataFiles\/Screenshots\/stSqlSrvRSvcs5_\" \/><\/p>\n<p>This image is, of course, much larger than what a typical sparkline would be, but it helps to illustrate how such graphs work. The sparkline plots the values in the <strong>Sales<\/strong> column, indicating the maximum level (green4), minimal level (red3), and last value (cyan3). The gray bar across the bottom of the graph indicates the IQR range (the data\u2019s midrange).<\/p>\n<p>Now let\u2019s return to the other graph definitions. The next on the list is a sparkbar, which we create by using the <strong>newSparkBar<\/strong> function:<\/p>\n<pre class=\"theme:ssms2012-simple-talk lang:tsql decode:true\">sbar &lt;- newSparkBar(values = salesdf$Sales, height = .8, width = 3,\u00a0\r\n  \u00a0 barCol = rep(\"blue\", 3))<\/pre>\n<p>As with the sparkline, our sparkbar definition includes the <strong>values<\/strong>, <strong>height<\/strong>, and <strong>width<\/strong> arguments. The fourth argument, <strong>barCol<\/strong>, defines the sparkbar\u2019s colors. The argument takes three values, specified in the following order:<\/p>\n<ol>\n<li>Negative-valued bars<\/li>\n<li>Positive-valued bars<\/li>\n<li>Bar outlines<\/li>\n<\/ol>\n<p>Given that we\u2019re working only with positive numeric values and there are many of them, we\u2019ll assign the same color to all settings to avoid unnecessary clutter and to keep things simple. The <strong>rep<\/strong> function provides an easy way to repeat the value <strong>blue<\/strong> three times.<\/p>\n<p>After we define our sparkbar, we can use the <strong>export<\/strong> function to save the sparkbar to a .pdf file. We call the function just as we do for the sparkline, only this time, specifying the sparkbar graph, <strong>sbar<\/strong>. When we run the T-SQL code, the R engine generates a .pdf file that contains the following graph.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"498\" height=\"133\" class=\"wp-image-73902\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2017\/10\/documents-datafiles-scr-1.png\" alt=\"..\/..\/..\/..\/..\/..\/..\/..\/..\/Documents\/DataFiles\/Screenshots\/stSqlSrvRSvcs5\" \/><\/p>\n<p>The sparkbar plots a vertical line for each value in the <strong>Sales<\/strong> column, providing quick insight into the range of values that make up the data set.<\/p>\n<p>Next, we use the <strong>newSparkBox<\/strong> function to define a sparkbox, taking a similar approach to how we defined the sparkline and sparkbar:<\/p>\n<pre class=\"theme:ssms2012-simple-talk lang:tsql decode:true\">sbox &lt;- newSparkBox(values = salesdf$Sales, height = .8, width = 2,\u00a0\r\n  \u00a0 boxCol = c(\"black\", \"green\"))<\/pre>\n<p>In this case, the <strong>boxCol<\/strong> argument takes only two values: the box outline color and the box fill color. When we run the T-SQL code, the <strong>export<\/strong> function creates a .pdf file that contains the following graph.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"332\" height=\"134\" class=\"wp-image-73903\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2017\/10\/documents-datafiles-scr-2.png\" alt=\"..\/..\/..\/..\/..\/..\/..\/..\/..\/Documents\/DataFiles\/Screenshots\/stSqlSrvRSvcs5\" \/><\/p>\n<p>The graph shows the distribution of data based on five values: minimum, first quartile, median, third quartile, and maximum. The orange dots are the outliers that fall outside the typical range of data.<\/p>\n<p>Next, we use the <strong>newSparkHist<\/strong> function to create a sparkhist:<\/p>\n<pre class=\"theme:ssms2012-simple-talk lang:tsql decode:true\">shist &lt;- newSparkHist(values = salesdf$Sales, height = .8, width = 2,\u00a0\r\n  \u00a0 barCol = c(\"green\", \"green\", \"black\"))<\/pre>\n<p>The <strong>barCol<\/strong> argument works just like it does for sparkbar. In this case, however, we\u2019re specifying that the bars are green and the outlines are black, giving us the following graph.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"335\" height=\"133\" class=\"wp-image-73904\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2017\/10\/documents-datafiles-scr-3.png\" alt=\"..\/..\/..\/..\/..\/..\/..\/..\/..\/Documents\/DataFiles\/Screenshots\/stSqlSrvRSvcs5_\" \/><\/p>\n<p>The sparkhist displays a histogram that provides us with a breakdown of data ranges (in bins) based on the distribution of values. As with the other graphs, the sparkhist offers yet another glimpse into the nature of our data.<\/p>\n<h2>Creating the graphical table<\/h2>\n<p>Creating spark graphs individually can be handy for certain applications, but you might also want to construct tables that incorporate those graphs directly. The <strong>sparkTable<\/strong> package lets you build graphical tables that include both data values and spark graphs, which we can then export to either an HTML file or a LaTeX file.<\/p>\n<p>To create a graphical table, we can take the following steps:<\/p>\n<ol>\n<li>Define the spark graphs.<\/li>\n<li>Define the table\u2019s column structure and column names.<\/li>\n<li>Specify the source data to use for the graphical table\u2019s columns.<\/li>\n<li>Prepare the source dataset for generating the table.<\/li>\n<li>Generate the table.<\/li>\n<li>Export the table to an HTML or LaTeX file.<\/li>\n<\/ol>\n<p>The following R script uses these steps to generate an HTML file that contains a graphical table:<\/p>\n<pre class=\"theme:ssms2012-simple-talk lang:tsql decode:true\">DECLARE @rscript NVARCHAR(MAX);\r\n  SET @rscript = N'\r\n  \u00a0 # import sparkTable package\r\n  \u00a0 library(sparkTable)\r\n  \u00a0 # retrieve SQL Server data\r\n  \u00a0 salesdf &lt;- InputDataSet\r\n  \u00a0 # define spark objects\r\n  \u00a0 sline &lt;- newSparkLine(height = .8, width = 3,\u00a0\r\n  \u00a0 \u00a0 lineWidth = .4, pointWidth = 3, showIQR = TRUE,\u00a0\r\n  \u00a0 \u00a0 allColors = c(\"red3\", \"green4\", \"cyan3\", \"white\", \"blue\", \"gray75\"))\r\n  \u00a0 sbar &lt;- newSparkBar(height = .8, width = 3,\u00a0\r\n  \u00a0 \u00a0 barCol = rep(\"blue\", 3))\r\n  \u00a0 sbox &lt;- newSparkBox(height = .8, width = 2,\u00a0\r\n  \u00a0 \u00a0 boxCol = c(\"black\", \"green\"))\r\n  \u00a0 shist &lt;- newSparkHist(height = .8, width = 2,\u00a0\r\n  \u00a0 \u00a0 barCol = c(\"green\", \"green\", \"black\"))\r\n  \u00a0 # define table output\r\n  \u00a0 content &lt;- list(\r\n  \u00a0 \u00a0 function(x) {round(mean(x), 1)}, sline, sbar, sbox, shist\r\n  \u00a0 )\r\n  \u00a0 names(content) &lt;-\u00a0\r\n  \u00a0 \u00a0 c(\"Sales Mean\", \"Spark Line\", \"Spark Bar\", \"Spark Box\", \"Spark Histogram\")\r\n  \u00a0 # point columns to source data\r\n  \u00a0 cols &lt;- rep(\"Sales\", 5)\r\n    # transform data to long format and create spark table\r\n  \u00a0 reshape &lt;- reshapeExt(salesdf, varying=list(2))\r\n  \u00a0 stable &lt;-\u00a0\r\n  \u00a0 \u00a0 newSparkTable(dataObj = reshape, tableContent = content, varType = cols)\r\n  \u00a0 # export table to HTML file\r\n  \u00a0 export(stable, outputType = \"html\",\r\n  \u00a0 \u00a0 filename=file.path(\"C:\\\\DataFiles\", \"SalesReport\"),\r\n  \u00a0 \u00a0 graphNames = file.path(\"C:\\\\DataFiles\", \"SalesReport\"))';\r\n  DECLARE @sqlscript NVARCHAR(MAX);\r\n  SET @sqlscript = N'\r\n  \u00a0 SELECT t.Name AS Territories, h.Subtotal AS Sales\r\n  \u00a0 FROM Sales.SalesOrderHeader h INNER JOIN Sales.SalesTerritory t\r\n  \u00a0 \u00a0 ON h.TerritoryID = t.TerritoryID\r\n  \u00a0 WHERE t.[Group] = ''North America'' AND h.Subtotal &gt; 20000;';\r\n  EXEC sp_execute_external_script\r\n  \u00a0 @language = N'R',\r\n  \u00a0 @script = @rscript,\r\n  \u00a0 @input_data_1 = @sqlscript;\u00a0\r\n  GO<\/pre>\n<p>We\u2019ll review the R script one element at a time to better understand what\u2019s going on, starting with the spark graph definitions. To define the graphs, we take an approach similar to the one we followed when creating them individually, except that we don\u2019t include the <strong>values<\/strong> argument, as shown in the following sparkline definition:<\/p>\n<pre class=\"theme:ssms2012-simple-talk lang:tsql decode:true\">sline &lt;- newSparkLine(height = .8, width = 3,\u00a0\r\n  \u00a0 lineWidth = .4, pointWidth = 3, showIQR = TRUE,\u00a0\r\n  \u00a0 allColors = c(\"red3\", \"green4\", \"cyan3\", \"white\", \"blue\", \"gray75\"))<\/pre>\n<p>As you can see, the only argument missing from the <strong>newSparkLine<\/strong> function is <strong>values<\/strong>. We associate the source data with the sparkline later in the script, so we don\u2019t need to do it here. To define the other spark graphs, we take the same approach.<\/p>\n<p>Next, we must define the structure of our graphical table. We do this by creating a list that includes one element for each of the table\u2019s columns:<\/p>\n<pre class=\"theme:ssms2012-simple-talk lang:tsql decode:true\">content &lt;- list(\r\n  \u00a0 function(x) {round(mean(x), 1)}, sline, sbar, sbox, shist\r\n  )\r\n  names(content) &lt;-\u00a0\r\n  \u00a0 c(\"Sales Mean\", \"Spark Line\", \"Spark Bar\", \"Spark Box\", \"Spark Histogram\")<\/pre>\n<p>The list can include user-defined function definitions or any of the four types of spark graphs. In this case, the first column is a simple function that finds the mean aggregate of the specified data. Ultimately, this column will be used for the aggregated data in the <strong>Sales<\/strong> column in the source dataset. The remaining four columns are the spark graphs: sparkline (<strong>sline<\/strong>), sparkbar (<strong>sbar<\/strong>), sparkbox (<strong>sbox<\/strong>), and sparkhist (<strong>shist<\/strong>).<\/p>\n<p>After we define the <strong>content<\/strong> list, we use that <strong>names<\/strong> function to assign a name to each value in the list. These will serve as the column names in our graphical table.<\/p>\n<p>The next step is to specify the columns in the <strong>salesdf<\/strong> dataset that map to the five columns in the graphical table. Because we\u2019ll be using the data from the <strong>Sales<\/strong> column for each column in the graphical table, we can use the <strong>rep<\/strong> function to create the list:<\/p>\n<pre class=\"theme:ssms2012-simple-talk lang:tsql decode:true\">cols &lt;- rep(\"Sales\", 5)<\/pre>\n<p>With our structure in place, we\u2019re just about ready to define the graphical table. But first we must use the <strong>reshapeExt<\/strong> function to transform the <strong>Sales<\/strong> column to long format:<\/p>\n<pre class=\"theme:ssms2012-simple-talk lang:tsql decode:true\">reshape &lt;- reshapeExt(salesdf, varying = list(2))<\/pre>\n<p>When calling the <strong>reshapeExt<\/strong> function, we first specify the source dataset, <strong>salesdf<\/strong>, and then provide a value for the <strong>varying<\/strong> argument, which is a list of the columns to be transformed. Because the <strong>Sales<\/strong> column is the second column in the dataset and is the only column that we need to transform, the list contains only the value <strong>2<\/strong>.<\/p>\n<p>After we reshape the <strong>Sales<\/strong> column, we can use the <strong>newSparkTable<\/strong> function to create a graphical table based on the <strong>reshape<\/strong> dataset, the <strong>content<\/strong> table structure, and the <strong>cols<\/strong> data mapping:<\/p>\n<pre class=\"theme:ssms2012-simple-talk lang:tsql decode:true\">stable &lt;-\u00a0\r\n  \u00a0 newSparkTable(dataObj = reshape, tableContent = content, varType = cols)<\/pre>\n<p>You might have noticed that the table structure does not include the <strong>Territories<\/strong> column that\u2019s in the <strong>reshape<\/strong> and <strong>salesdf<\/strong> data sets. That\u2019s because the <strong>newSparkTable<\/strong> function automatically uses the first column in the source dataset as the bases for aggregating the data and generating the subsequent graphs. As a result, the graphical table will also include a unique list of territories in its first column.<\/p>\n<p>With our table definition in place, we can export it to an .html file, using the same <strong>export<\/strong> function we used for spark graphs, but taking a slightly different approach:<\/p>\n<pre class=\"theme:ssms2012-simple-talk lang:tsql decode:true\">export(stable, outputType = \"html\",\r\n  \u00a0 filename=file.path(\"C:\\\\DataFiles\", \"SalesReport\"),\r\n  \u00a0 graphNames = file.path(\"C:\\\\DataFiles\", \"SalesReport\"))';<\/pre>\n<p>When calling the <strong>export<\/strong> function, we specify the graphical table (<strong>stable<\/strong>) and the output type (<strong>html<\/strong>). We then provide the path and file name for the .html file and the individual graph files, which will be numbered sequentially, using the specified name as a prefix. When we run our T-SQL code, the R engine creates an .html file that contains the following markup.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1097\" height=\"728\" class=\"wp-image-73905\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2017\/10\/documents-datafiles-scr-4.png\" alt=\"..\/..\/..\/..\/..\/..\/..\/..\/..\/Documents\/DataFiles\/Screenshots\/stSqlSrvRSvcs5\" \/><\/p>\n<p>You should be able to view the .html file in any browser so you can see for yourself what the table will look like when rendered in HTML. The following figure shows how it appeared on my system when I opened the file in Chrome.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1153\" height=\"533\" class=\"wp-image-73906\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2017\/10\/documents-datafiles-scr-5.png\" alt=\"..\/..\/..\/..\/..\/..\/..\/..\/..\/Documents\/DataFiles\/Screenshots\/stSqlSrvRSvcs5\" \/><\/p>\n<p>The table is shown with the default styles being used. No doubt you\u2019ll want to apply specific styles to your tables, whether through style sheets or other mechanisms, as well as format and size the graphs specific to your needs and data. In the meantime, this should give you a taste of what you can do with the <strong>sparkTable<\/strong> package. Once you get the hang of how to use it, the process is actually fairly straightforward.<\/p>\n<h2>Exporting to a .tex file<\/h2>\n<p>You can also export your graphical table to a .tex file. A .tex file is a plain-text document that conforms to the formatting standards defined by the LaTeX document preparation system, which is used for typesetting technical and scientific documentation.<\/p>\n<p>To create a .tex file for your graphical table, rather than an .html file, you simply change the value of the <strong>outputType<\/strong> argument in the <strong>export<\/strong> function from <strong>html<\/strong> to <strong>tex<\/strong>:<\/p>\n<pre class=\"theme:ssms2012-simple-talk lang:tsql decode:true\">export(stable, outputType = \"tex\",\r\n  \u00a0 filename=file.path(\"C:\\\\DataFiles\", \"SalesReport\"),\r\n  \u00a0 graphNames = file.path(\"C:\\\\DataFiles\", \"SalesReport\"))';<\/pre>\n<p>As expected, the <strong>export<\/strong> statement generates the specified .tex file, which includes our graphical table. Because the file is in plain text, we can open it in any text editor. For example, the following figure shows the file opened in Notepad++, complete with the LaTeX markup.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1097\" height=\"722\" class=\"wp-image-73907\" src=\"https:\/\/www.red-gate.com\/simple-talk\/wp-content\/uploads\/2017\/10\/documents-datafiles-scr-6.png\" alt=\"..\/..\/..\/..\/..\/..\/..\/..\/..\/Documents\/DataFiles\/Screenshots\/stSqlSrvRSvcs\" \/><\/p>\n<p>This is as far as we can go with the .tex file for now. LaTeX is a much broader and involved conversation than what we can cover here. However, if you\u2019re familiar with the LaTeX ecosystem, you can use the .tex files you generate through R Services to produce documents that contain graphical tables.<\/p>\n<h2>Working with the sparkTable package<\/h2>\n<p>Not surprisingly, there\u2019s a lot more to the <strong>sparkTable<\/strong> package than what we\u2019ve just touched upon (and a lot more complex analytics that we can perform in R), but these examples should give you a good sense of how you can use the package to create spark graphs and the graphical tables to contain them. The best part is that we can do all this within the context of R Services, making it possible to easily incorporate SQL Server data into our spark graphs and graphical tables. To learn more about working with the <strong>sparkTable<\/strong> package, check out the article <a href=\"https:\/\/journal.r-project.org\/archive\/2015\/RJ-2015-003\/RJ-2015-003.pdf\">sparkTable: Generating Graphical Tables for Websites and Documents with R<\/a>.<\/p>\n<p>You can also use other R packages and approaches to create spark graphs and graphical tables. I encourage you to dig around to see what options are out there. Keep in mind, however, that one of the biggest challenges with the R language is that the documentation is often inadequate or geared toward advanced developers and analysts, without the careful explanations necessary to fully understand each language element.<\/p>\n<p>SQL Server developers new to R can have a tough time making sense of an R package or particular R logic. Often their best recourse is basic trial-and-error. Perhaps with the R language now integrated into SQL Server, we\u2019ll start seeing more useful information about R that makes the prospects of diving into the language more palatable for beginners.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By being able to run R from SQL Server, you have available to you not just a convenient way of performing analysis on data but also a wide range of more specialized graphical facilities.  Rob Sheldon illustrates the point by demonstrating how you can create sparklines that can then be embedded in reports.&hellip;<\/p>\n","protected":false},"author":221841,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143528],"tags":[5134],"coauthors":[6779],"class_list":["post-73887","post","type-post","status-publish","format-standard","hentry","category-bi-sql-server","tag-sql-prompt"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/73887","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/221841"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=73887"}],"version-history":[{"count":9,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/73887\/revisions"}],"predecessor-version":[{"id":79284,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/73887\/revisions\/79284"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=73887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=73887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=73887"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=73887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}