I tried to write a procedure to compute Cronbach alpha if there are more than one parameter and return 0 othwerwise. It works for correct input but fails if number of field alpha calls is 0 or 1. How can I check if the input is suitable for alpha? I tried to check it number of parameters is less than 2 but it somehow fails.
USE [Jaakko]
GO
/****** Object: StoredProcedure [dbo].[sp_alpha] Script Date: 15.8.2017 11.30.30 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_alpha]
@input nvarchar(max)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
EXEC sp_execute_external_script
@language = N'R'
, @script = N'library(psych);
if (dim(Test)<2) {
return(0)
} else {
a = tryCatch(alpha(Test), warning = function(w) {alpha(Test,check.keys=TRUE)})
str(a)
}'
, @input_data_1 = @input
, @input_data_1_name = N'Test'
END
I call the procedure as
EXEC sp_alpha 'SELECT cast(ky1 as float) FROM VUODET'
It outputs
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 R[1, 2] : subscript out of bounds
Calls: source ... tryCatch -> tryCatchList -> tryCatchOne -> doTryCatch -> alpha
In addition: Warning message:
In if (dim(Test) < 2) { :
the condition has length > 1 and only the first element will be used
Error in ScaleR. Check the output for more information.
Error in eval(expr, envir, enclos) :
Error in ScaleR. Check the output for more information.
Calls: source -> withVisible -> eval -> eval -> .Call
Execution halted
User contributions licensed under CC BY-SA 3.0