summaryrefslogtreecommitdiff
path: root/R/check_inputs.R
blob: ccc54d475526f54038d5d76a07912bd64ebf77b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
## Check input data and return a valid matrix object
##
.check_input_data_matrix <- function(x , y , na.rm){

    if(is.null(y)){
        if(!is.array(x) &  !is.data.frame(x) & !is.matrix(x)){
            stop('"x" must be a numeric matrix or any other array-type object that can be converted or a "matrix" object with with ncol>=2.\n\n')  
        }

        if(is.vector(x)){
            stop('"x" must be a numeric matrix or any other array-type object that can be converted or a "matrix" object.  with with ncol>=2.\n\n')  
        }

        if(!is.matrix(x)){
            x <- data.matrix(x)
        }
        
        if( !is.numeric(x) ){
            stop('"x" must be numeric.')
        }
        
        if(nrow(x)<2 | ncol(x)<2){
            stop('nrow(x)>=2 and ncol(xa)>=2 are required\n\n')  
        }

        is_na_data <- is.na(x)
        if(any(is_na_data)){
            if(na.rm==FALSE){
                stop('"x" contains NA records. You may want to filter NAs by setting "na.rm=TRUE" (see documentation for more details).\n\n')
            }else{
                idx_na <- which( rowSums(is_na_data) >=1 )
                x   <- x[-idx_na , , drop = FALSE]
                if(nrow(x)<2){
                    stop('nrow(x)<2 after NA removal.\n\n')  
                }   
            }
        }

        if(any(!is.finite(x))){
            stop('"x" contains Inf values\n\n')  
        }
        
    }else{

        if(!is.vector(x) | !is.vector(y)){
            stop('If "y" is given, "x" and  "y" must be both numeric.\n\n')
        }
        
        if(!is.numeric(x) | !is.numeric(y)){
            stop('"x" and "y" must be numeric.\n\n')
        }

        if(length(x) != length(y)){
            stop('"x" and "y" have different length.\n\n')
        }

        x <- cbind( x , y , deparse.level = 0) 
        
        is_na_data <- is.na(x)
        if(any(is_na_data)){
            if(na.rm==FALSE){
                stop('"x" or "y" contains NA records. You may want to filter NAs by setting "na.rm=TRUE" (see documentation for more details).\n\n')
            }else{
                idx_na <- which( rowSums(is_na_data) >=1 )
                x   <- x[-idx_na , , drop = FALSE]
                if(nrow(x)<2){
                    stop('length(x)<2 and/or length(y)<2 after NA removal.\n\n')  
                }   
            }
        }

        if(any(!is.finite(x))){
            stop('"x" and/or "y"  contains Inf values\n\n')  
        }   
    }

    
    return(x)
    
} ### END function