You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
, @input_data_1 =N'Select "RentalCount", "Year" ,"Month", "Day", "WeekDay", "Snow", "Holiday" from rental_data where Year = 2015'
82
+
, @input_data_1_name =N'rental_score_data'
83
+
, @params =N'@rx_model varbinary(max)'
84
+
, @rx_model = @rx_model
85
+
with result sets (("RentalCount_Predicted"float, "RentalCount_Actual"float,"Month"float,"Day"float,"WeekDay"float,"Snow"float,"Holiday"float, "Year"float));
86
+
87
+
END;
63
88
GO
64
89
65
-
--Stored procedure that takes model name and new data as inout parameters and predicts the rental count for the new data
66
-
DROPPROCEDUREIFEXISTS predict_rentals;
90
+
---------------- STEP 5 - Create DB table to store predictions -----------------------
91
+
DROPTABLEIFEXISTS [dbo].[rental_predictions];
92
+
GO
93
+
--Create a table to store the predictions in
94
+
CREATETABLE [dbo].[rental_predictions](
95
+
[RentalCount_Predicted] [int] NULL,
96
+
[RentalCount_Actual] [int] NULL,
97
+
[Month] [int] NULL,
98
+
[Day] [int] NULL,
99
+
[WeekDay] [int] NULL,
100
+
[Snow] [int] NULL,
101
+
[Holiday] [int] NULL,
102
+
[Year] [int] NULL
103
+
) ON [PRIMARY]
104
+
GO
105
+
106
+
107
+
---------------- STEP 6 - Save the predictions in a DB table -----------------------
108
+
TRUNCATETABLE rental_predictions;
109
+
--Insert the results of the predictions for test set into a table
110
+
INSERTINTO rental_predictions
111
+
EXEC predict_rentalcount 'rxDTree';
112
+
113
+
-- Select contents of the table
114
+
SELECT*FROM rental_predictions;
115
+
116
+
------------- STEP 7 - Alternative to the previous stored procedure - Uses new data to predict future rental counts
117
+
--Stored procedure that takes model name and new data as input parameters and predicts the rental count for the new data
-**Update history:** Getting started tutorial for R Services
32
32
@@ -43,20 +43,62 @@ After that, you can download a DB backup file and restore it using Setup.sql. [D
43
43
<!-- Examples -->
44
44
1. SQL Server 2016 (or higher) with R Services installed
45
45
2. SQL Server Management Studio
46
-
3. R IDE Tool like Visual Studio
47
-
46
+
3. R IDE Tool like Visual Studio RTVS
47
+
48
+
## Run this sample app
49
+
1. From SQL Server Management Studio or SQL Server Data Tools connect to your SQL Server 2016 or vNext SQL database and execute setup.sql to restore the sample DB
50
+
2. From SQL Server Management Studio or SQL Server Data Tools, execute Predictive Model.sql script to set up tables, train model, predict using that model etc.
51
+
This is all covered step by step in the [tutorial](http://aka.ms/sqldev/R)
52
+
53
+
3. Navigate to the folder where you have downloaded sample and run **npm install** in command window, or run setup.bat if you are on Windows operating system. This command will install necessary npm packages defined in project.json.
54
+
55
+
4. Locate db.js file in the project, change database connection info in createConnection() method to reference your database. the following tokens should be replaced:
56
+
1. SERVERNAME - name of the database server.
57
+
2. DATABASE - Name of database where Todo table is stored.
58
+
3. USERNAME - SQL Server login that can access table data and execute stored procedures.
59
+
4. PASSWORD - Password associated to SQL Server login.
60
+
61
+
```
62
+
var config = {
63
+
server : "SERVER.database.windows.net",
64
+
userName: "USER",
65
+
password: "PASSWORD",
66
+
// If you have a named instance, you can put the instance name here:
67
+
options: { encrypt: true, database: 'DATABASE' }
68
+
};
69
+
```
70
+
71
+
5. Run sample app by opening a command window, navigate to the location where here you have downloaded sample and run **node bin\www**
72
+
6. Go to a browser and navigate to the following [link] (http://localhost:3000/client.html). You should now see an HTML table containing the predictions generated using R in SQL Server.
48
73
49
74
<aname=sample-details></a>
50
-
## Sample Details
51
75
52
-
### PredictiveModel.R
76
+
## Sample details
77
+
78
+
This sample application shows how to create a predictive model and generate predictions using the model. It also shows how to build a simple REST API service tthat gets data from the DB.
79
+
NodeJS REST API is used to implement REST Service in the example.
53
80
81
+
### Predictive Model.R
54
82
The R script that generates a predictive model and uses it to predict rental counts
55
83
56
-
### PredictiveModel.SQL
84
+
### Predictive Model.SQL
85
+
Takes the R code in PredictiveModel.R and deploys it inside SQL Server. Creating stored procedures and tables for training, storing models and creating stored procedures for prediction.
86
+
87
+
### app.js
88
+
File that contains startup code.
89
+
### db.js
90
+
File that contains functions that wrap Tedious library
91
+
### predictions.js
92
+
File that contains action that will be called to get the predictions
57
93
58
-
Takes the R code in PredictiveModel.R and uses it inside SQL Server. Creating stored procedures for training and prediction.
94
+
Service uses Tedious library for data access and built-in JSON functionalities that are available in SQL Server 2016 and Azure SQL Database.
59
95
96
+
<aname=disclaimers></a>
97
+
98
+
## Disclaimers
99
+
The code included in this sample is not intended demonstrate some general guidance and architectural patterns for web development.
100
+
It contains minimal code required to create a REST API.
101
+
You can easily modify this code to fit the architecture of your application.
60
102
61
103
62
104
<aname=related-links></a>
@@ -67,5 +109,5 @@ Takes the R code in PredictiveModel.R and uses it inside SQL Server. Creating st
67
109
For additional content, see these articles:
68
110
69
111
[SQL Server R Services - Upgrade and Installation FAQ](https://msdn.microsoft.com/en-us/library/mt653951.aspx)
70
-
71
-
[Other SQL Server R Services Tutorials](https://msdn.microsoft.com/en-us/library/mt591993.aspx)
112
+
[Other SQL Server R Services Tutorials](https://msdn.microsoft.com/en-us/library/mt591993.aspx)
113
+
[Watch a presentation about predictive modeling in SQL Server, that also goes through this sample](https://www.youtube.com/watch?v=YCyj9cdi4Nk&feature=youtu.be)
0 commit comments