如何检查R中参数的数量是否小于2?

时间:2017-08-15 08:49:13

标签: sql-server r

如果有多个参数并且返回0,我尝试编写一个计算Cronbach alpha的过程。它适用于正确的输入但如果字段alpha调用的数量为0或1则失败。如何检查输入是否适合alpha?我试着检查一下参数的数量是否小于2,但它以某种方式失败了。

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

我将程序称为

EXEC sp_alpha 'SELECT cast(ky1 as float) FROM VUODET'

输出

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

0 个答案:

没有答案