SQL Server – R-Code Won’t Find Package psych

rsql server

I tried to run R code in SQL Server but it won't run although it runs or R. How can I get R running properly on SQL Server such that it finds the package psych? I tested examples in https://docs.microsoft.com/en-us/sql/advanced-analytics/tutorials/rtsql-working-with-inputs-and-outputs to make sure that at least some R code works properly in SQL Server.

I tried

EXECUTE sp_execute_external_script    
    @language = N'R'    
   , @script = N'a1=c(4,3,4,5,1,5,5,5,5,5,5,4,4,4,5,4,5,5,5,4,5,5,4,4,5,5,3,4,4,4,4,5,5,4,3,5,1,5,5,5,5,4,4,4,5,5,4,5,5,4,5,5,5,4,4,1,5,5,3,5,5,4,5,5,4,4,4,4,5,5,4,4,4,4,5,1,5,5,5,4,4,3,5,4,5,5,4,4,4,5,5,5,5,5,3,5,4,5,5,5,5,5,5,5,4,5,4,4,5,4,5,5,5,4,5,4,4,5,4,5,5,2,4,4,5,3,4,4,4,5,4,5,3,5,5,5,4,5,5,2,1,5,5,5,4,4,4,4,5,5,4,4,5)
a1=c(4,3,4,5,1,5,5,5,5,5,5,4,4,4,5,4,5,5,5,4,5,5,4,4,5,5,3,4,4,4,4,5,5,4,3,5,1,5,5,5,5,4,4,4,5,5,4,5,5,4,5,5,5,4,4,1,5,5,3,5,5,4,5,5,4,4,4,4,5,5,4,4,4,4,5,1,5,5,5,4,4,3,5,4,5,5,4,4,4,5,5,5,5,5,3,5,4,5,5,5,5,5,5,5,4,5,4,4,5,4,5,5,5,4,5,4,4,5,4,5,5,2,4,4,5,3,4,4,4,5,4,5,3,5,5,5,4,5,5,2,1,5,5,5,4,4,4,4,5,5,4,4,5)
a2=c(4,4,4,5,5,5,5,5,4,4,4,4,4,5,5,5,5,5,5,4,5,4,4,5,5,5,4,4,5,4,5,5,5,4,4,4,1,5,5,4,5,3,4,4,4,5,5,5,5,5,5,5,5,5,4,1,4,5,4,4,4,5,5,4,5,5,4,4,4,4,4,3,5,4,5,5,5,5,4,4,4,2,4,5,4,5,5,4,2,5,5,4,4,5,4,5,5,5,4,4,5,4,4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5,3,4,3,4,3,4,5,2,5,5,5,4,4,4,4,4,4,5,5,2,4,5,4,5,5,5,5,4,5,1,5,4,3,5,3,4,4,4,4,4,1,5,4,4,4,4,5,5,5,4,5,4,4,4,4,5,4,5,4,3,5,5,5,3,4,4,4,4,5,4,4,5,5,3,4,4,4,4,4,5,5,2,5,5,4,5,4,2,5,5,4,4,4,4,5,4,5,3,5,4,3,4,4,4,5,5,3,4,3,5,2,5,4,4,2,4,4,4,4,4,4,4,4,4,4,3,4,4,4,4,4,5,5,4,4,4,5,4,3,4,3,2,4,4,5,4)
a20=c(5,5,5,5,5,5,4,5,3,4,5,4,5,5,5,5,5,5,5,4,5,5,4,5,5,4,5,4,5,5,5,5,4,5,4,5,5,5,5,5,5,5,5,4,5,5,5,5,1,4,5,5,5,4,5,5,5,5,4,5,5,5,5,5,5,5,5,4,5,5,5,5,4,4,5,5,5,5,5,5,5,3,5,5,5,5,5,5,4,5,5,5,5,4,4,4,5,5,5,5,1,5,4,5,5,4,4,5,5,5,4,5,1,4,5,5,5,5,4,5,2,4,1,5,5,4,5,5,4,5,1,4,4,5,5,5,4,5,5,5,5,5,4,5,4,4,4,4,5,4.56579,5,4,4)
options(warn=-1);a=cbind(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20)
library(psych);
result = tryCatch(alpha(a), warning = function(w) {tryCatch(alpha(a,check.keys=TRUE),warning = function(w) { print("Error, lets try some likelihood!" ) })})
cor(a, method = c("spearman"))'    
   , @input_data_1 = N' SELECT 1 as Temp1'    
   WITH RESULT SETS (([Col1] char(20) NOT NULL));    

The error was

Msg 39004, Level 16, State 20, Line 1
A 'R' script error occurred during execution of 'sp_execute_external_script' with HRESULT 0x80004004.

Msg 39019, Level 16, State 1, Line 1
An external script error occurred:
Error in library(psych) : there is no package called 'psych'
Calls: source -> withVisible -> eval -> eval -> library

Best Answer

To use libraries in R via SQL Server, you need to ensure that the packages are in the default library. R itself is more flexible about this, but SQL Server requires stricter loading:

Therefore, when you migrate R solutions to run in SQL Server, it is important that you do the following:

  • Install any packages that you need to the default library. Edit code to ensure that packages are loaded from the default library, not from ad hoc directories or user libraries.
  • Check your code to make sure that there are no calls to uninstalled packages.
  • Modify any code that would try to install packages dynamically.