Enabling R commands on T-SQL of MS SQL Server

Enabling R commands on T-SQL of MS SQL Server
I have installed Microsoft SQL server 2017 and SQL server management studio on my laptop computer with windows 10 Professional.  I am able to connect to the database from management studio.  I have also installed R (version 3.5.0 (The R foundation for Statistical Computing).
I learnt that from SQL server 2016 onwards, MS SQL server permits R commands on T-SQL.
I am familiar with both T-SQL and R commands.
I want to know how to enable execution of R commands on T-SQL.
Srinivas ManthaConsultant Anesthesiologist and Pain PhysicianAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vitor MontalvãoMSSQL Senior EngineerCommented:
I've wrote some articles about using R in SQL Server.
You'll need first to Setup R Services for SQL Server.
Then you can use the sp_execute_external_script  stored procedure to run R scripts in SQL Server.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Srinivas ManthaConsultant Anesthesiologist and Pain PhysicianAuthor Commented:
After running the following command  on query window on SQL Server Management Studio
EXEC sp_configure  'external scripts enabled', 1
GO
RECONFIGURE WITH OVERRIDE
GO

I got the following message
Configuration option 'external scripts enabled' changed from 0 to 1. Run the RECONFIGURE statement to install.
How should I go about now
Srinivas ManthaConsultant Anesthesiologist and Pain PhysicianAuthor Commented:
After going through the described steps, I executed the following command

EXECUTE sys.sp_execute_external_script
@language = N'R'
@script = N 'cat("hello world")'

I got the following message

Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '@script'.

Did I do any mistake
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

Vitor MontalvãoMSSQL Senior EngineerCommented:
It misses a comma before @script:
EXECUTE sys.sp_execute_external_script
@language = N'R'
,@script = N 'cat("hello world")'

Open in new window

Srinivas ManthaConsultant Anesthesiologist and Pain PhysicianAuthor Commented:
Now the message says
Msg 102, Level 15, State 1, Line 9
Incorrect syntax near 'cat("hello world")'.
Vitor MontalvãoMSSQL Senior EngineerCommented:
Oh, it's the space between the N and single quote. I didn't realize there's one. Just remove it and it should run fine:
EXECUTE sys.sp_execute_external_script
@language = N'R'
,@script = N'cat("hello world")'

Open in new window

Srinivas ManthaConsultant Anesthesiologist and Pain PhysicianAuthor Commented:
Msg 39023, Level 16, State 1, Procedure sys.sp_execute_external_script, Line 1 [Batch Start Line 16]
'sp_execute_external_script' is disabled on this instance of SQL Server. Use sp_configure 'external scripts enabled' to enable it.

Then I referred to web resource

https://tomaztsql.wordpress.com/2016/07/26/enabling-sp_execute_external_script-to-run-r-scripts-in-sql-server-2016/

to solve the issue
Then I stopped the stopped  and restarted the services (SQL Server Launchpad (SQLSERVER).
Still I got the same error message
I do not where I am going wrong
Vitor MontalvãoMSSQL Senior EngineerCommented:
Did you really restart the correct service for the correct instance/server?
Srinivas ManthaConsultant Anesthesiologist and Pain PhysicianAuthor Commented:
I restarted the computer.  Now it is taking the command and giving the output. Thank you. Now I want to give simple regular commands e.g.
<-a<-c(3,4,6,7)
<-mean(a)
How should I go about, what command should I give.
Can you suggest any book or  web resource that I can use to give R commands including taking data from columns of MSSQL
Vitor MontalvãoMSSQL Senior EngineerCommented:
Solution provided.
Author should open a new question for the new issue.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
R

From novice to tech pro — start learning today.