-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparseCharInput.R
executable file
·70 lines (60 loc) · 2.64 KB
/
parseCharInput.R
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
parseCharInput <- function(
input, # Comma separated character vector
# to split into numeric values.
convertToNumeric = TRUE, # Convert to numeric at the end?
sort = FALSE, # Sort the values ?
expected, # Expected length of the output
msg = "wrong length", # Error message when wrong length
checkdup = FALSE,
missingMsg = 'Unknown', # Error message if missing input
checkProb = FALSE, # Check that this sums up to one
valid = FALSE
){
###############################################################################
# Mango Solutions, Chippenham SN14 0SQ 2006
# parseCharInput.R Fri Wed Jun 06 12:55:37 BST 2007 @538 /Internet Time/
#
# Author: Romain
###############################################################################
# DESCRIPTION: parses a csv character string
# KEYWORDS: check, component:support
###############################################################################
missing(input) && ectdStop(missingMsg)
if(is.null(input)) return(NULL)
out <- if(is.numeric(input)) {
input
} else {
# Handles the commas (TESTFIX):
if(any(regexpr(",$", input) > 0))
ectdStop("Traling comma not accepted")
if(any(regexpr("^,", input) > 0))
ectdStop("Leading comma not accepted")
if(any(regexpr(",{2,}", input) > 0))
ectdStop("The separator should only be **one** comma")
# A numeric value is made numbers, dots, plus, minus and
# possibly e for the scientific notation (is there a need
# to be more clever with the scientific notation?)
if(convertToNumeric && any(regexpr("[^0-9eE\\.,[:space:]\\+\\-]", input)>0))
ectdStop("Impossible to convert to numbers")
out <- unlist(strsplit(input, "[[:space:]]*,[[:space:]]*"))
if(convertToNumeric) {
out <- as.numeric(out)
}
out
}
valid && validNames( out )
if(!missing(expected) && length(out) != expected)
ectdStop(msg)
if(checkdup && any(duplicated(out)))
ectdStop( "Duplicated values in " %.% deparse(substitute(input)))
# Convert to numeric and sort if required.
if(checkProb ){
if(!is.numeric(out) || sum(out) != 1 )
ectdStop("Parsed values don't sum up to one")
}
if(sort) {
sort(out)
} else {
out
}
}