title: SQL Server R multiple result sets
body: <p>Is it possible to get more than one table as a result from executing R script with SQL Server 2016+? Let's take a random simple example from the internet (no need to post mine to not overcomplicate the issue): </p>
  
  <pre><code>EXEC sp_execute_external_script
    @language =N'R',
    @script=N'OutputDataSet&lt;-InputDataSet',
    @input_data_1 =N'SELECT 1 AS hello'
    WITH RESULT SETS (([hello] int not null));
  </code></pre>
  
  <p>as posted in <a href="https://docs.microsoft.com/en-us/sql/advanced-analytics/tutorials/rtsql-using-r-code-in-transact-sql-quickstart" rel="noreferrer">here</a>.</p>
  
  <p>Here the result is returned as a single table. Let's say I do various calculations with the data and now I want to return multiple tables as a result. </p>
  
  <p>For example:</p>
  
  <pre><code>a&lt;-InputDataSet
  
  b&lt;-InputDataSet + 5
  </code></pre>
  
  <p>These would return two different tables as results. Now I cannot figure any nice way to return the data in two separate tables as it only returns one table. Obviously, I can return it like this:</p>
  
  <pre><code>OutputDataSet&lt;-data.frame(a, b)
  </code></pre>
  
  <p>But dealing with different functions and different data it soon becomes quite a hassle. For example I use a function <code>lm</code>. Now one dataset would be calculated estimated values and another would be the coefficients of each column participating in the equation. Again, of course I can join these two datatables and deal with them later, but the output result becomes colossal in many cases.</p>
  
  <p>The parameters to the procedure look like: <code>..., @output_data_1_name</code>, but there is no <code>@output_data_2_name</code>, etc. thus I do not see a way. Maybe it is possible to create the <code>OutputDataSet</code> so it holds multiple tables? If so - I am not aware of such way in R due to my lack of experience with it.</p>
  
  <p><strong>tldr;</strong> is it possible to return multiple result sets or my only solution is to manually construct the output in R code so I would always get one?</p>