| title | Lesson 2 Prepare R tutorial environment using PowerShell (SQL Server Machine Learning) | Microsoft Docs |
|---|---|
| description | Tutorial showing how to embed R in SQL Server stored procedures and T-SQL functions |
| ms.prod | sql |
| ms.technology | machine-learning |
| ms.date | 06/07/2018 |
| ms.topic | tutorial |
| author | HeidiSteen |
| ms.author | heidist |
| manager | cgronlun |
[!INCLUDEappliesto-ss-xxxx-xxxx-xxx-md-winonly]
This article is part of a tutorial for SQL developers on how to use R in SQL Server.
In this step, you'll run a PowerShell script to create the database objects required for the walkthrough. The script creates and loads a database using sample data obtained in the previous step. It also creates functions and stored procedures used throughout the tutorial.
Among the downloaded files, you should see a PowerShell script (RunSQL_SQL_Walkthrough.ps1) that prepares the environment for the walkthrough. Actions performed by the script include:
-
Install the SQL Native Client and SQL command-line utilities, if not already installed. These utilities are required for bulk-loading the data to the database using bcp.
-
Create a database and tables on the [!INCLUDEssNoVersion] instance, and bulk-insert data sourced from a .csv file.
-
Create multiple SQL functions and stored procedures.
By default, the script assumes a SQL Server database user login and password. If you are db_owner under your Windows user account, you can use your Windows identity to create the objects. To do so, open RunSQL_SQL_Walkthrough.ps1 in a code editor and append -T to the bcp bulk insert command (line 238):
bcp $db_tb in $csvfilepath -t ',' -S $server -f taxiimportfmt.xml -F 2 -C "RAW" -b 200000 -U $u -P $p -T
Open a PowerShell command prompt as administrator and run the following command.
.\RunSQL_SQL_Walkthrough.ps1You are prompted to input the following information:
-
Server instance where [!INCLUDErsql_productname] has been installed. On a default instance, this can be as simple as the machine name.
-
Database name. For this tutorial, scripts assume
TaxiNYC_Sample. -
User name and user password. Enter a SQL Server database login for these values. Alternatively, if you modified the script to accept a trusted Windows identity, press Enter to leave these values blank. Your Windows identity is used on the connection.
-
Fully qualified file name for the sample data downloaded in the previous lesson. For example:
C:\tempRSQL\nyctaxi1pct.csv
After you provide these values, the script executes immediately. During script execution, all placeholder names in the [!INCLUDEtsql] scripts are updated to use the inputs you provide.
When script execution is finished, confirm the database objects exist on the [!INCLUDEssNoVersion] instance using [!INCLUDEssManStudioFull]. You should see the database, tables, functions, and stored procedures.
Note
If the database objects already exist, they cannot be created again.
If the table already exists, the data will be appended, not overwritten. Therefore, be sure to drop any existing objects before running the script.
The following table summarizes the objects created in this lesson. Although you only run one PowerShell script (RunSQL_SQL_Walkthrough.ps1), that script calls other SQL scripts in turn to create the objects in your database. Scripts used to create each object are mentioned in the description.
| Object name | Object type | Description |
|---|---|---|
| TaxiNYC_Sample | database | Created by the create-db-tb-upload-data.sql script. Creates a database and two tables: dbo.nyctaxi_sample table: Contains the main NYC Taxi dataset. A clustered columnstore index is added to the table to improve storage and query performance. The 1% sample of the NYC Taxi dataset is inserted into this table. dbo.nyc_taxi_models table: Used to persist the trained advanced analytics model. |
| fnCalculateDistance | scalar-valued function | Created by the fnCalculateDistance.sql script. Calculates the direct distance between pickup and dropoff locations. This function is used in Create data features, Train and save a model and Operationalize the R model. |
| fnEngineerFeatures | table-valued function | Created by the fnEngineerFeatures.sql script. Creates new data features for model training. This function is used in Create data features and Operationalize the R model. |
| PlotHistogram | stored procedure | Created by the PlotHistogram.sql script. Calls an R function to plot the histogram of a variable and then returns the plot as a binary object. This stored procedure is used in Explore and visualize data. |
| PlotInOutputFiles | stored procedure | Created by the PlotInOutputFiles.sql script. Creates a graphic using an R function and then saves the output as a local PDF file. This stored procedure is used in Explore and visualize data. |
| PersistModel | stored procedure | Created by the PersistModel.sql script. Takes a model that has been serialized in a varbinary data type, and writes it to the specified table. |
| PredictTip | stored procedure | Created by the PredictTip.sql script. Calls the trained model to create predictions using the model. The stored procedure accepts a query as its input parameter and returns a column of numeric values containing the scores for the input rows. This stored procedure is used in Operationalize the R model. |
| PredictTipSingleMode | stored procedure | Created by the PredictTipSingleMode.sql script. Calls the trained model to create predictions using the model. This stored procedure accepts a new observation as input, with individual feature values passed as in-line parameters, and returns a value that predicts the outcome for the new observation. This stored procedure is used in Operationalize the R model. |
| TrainTipPredictionModel | stored procedure | Created by the TrainTipPredictionModel.sql script. Trains a logistic regression model by calling an R package. The model predicts the value of the tipped column, and is trained using a randomly selected 70% of the data. The output of the stored procedure is the trained model, which is saved in the table nyc_taxi_models. This stored procedure is used in Train and save a model. |
