Parse Input

Share:

Description

Parse input, returning either NULL or a vector of the values.

Usage

1
2
3
4
5
grade.isscalar(x, usena=FALSE, useinf=FALSE, quiet=TRUE)

grade.parse(ans, useeval=TRUE, usena=FALSE, useinf=FALSE, quiet=TRUE)
grade.parseset(ans, useeval=TRUE, usena=FALSE, useinf=FALSE, quiet=TRUE)
grade.parsechunk(ans, useeval=TRUE, usena=FALSE, useinf=FALSE, quiet=TRUE)

Arguments

x

argument for grade.isscalar to check

ans

input to parse. Can be a string or a vector

useeval

TRUE or FALSE indicates whether or not to use 'eval' on strings

usena

TRUE or FALSE indicating whether or not NA is an accepted value

useinf

TRUE or FALSE indicating whether or not Inf and -Inf are accepted values

quiet

TRUE or FALSE. If TRUE there are more warning messages when checks fail. Can be helpful for debugging.

Details

grade.isscalar checks to see if x is a finite numeric scalar (vector of length 1). If usena=TRUE, NA is also accepted. If useinf=TRUE, Inf and -Inf are also accepted.

Input to the grade.parse functions can be a string or a vector. grade.parsechunk will only return scalars, the other two will return a vector. All three check return values using grade.isscalar on each element.

grade.parse delegates character types to either grade.parsechunk or grade.parseset. If the string contains any of the characters '[', ']', '(', ')', or ',' the string is sent to grade.parseset. Otherwise it is sent to grade.parsechunk.

If x is a character, grade.parsechunk checks for any of the forbidden characters '[', ']', '(', ')', or ','. If any are found grade.parsechunk refuses to evaluate the string.

If x is a character, grade.parsechunk makes sure that it looks like a vector or set. I.e. it starts with an open bracket or parenthesis and ends with a close bracket or parenthesis. No other brackets or parenthesis are allowed. The middle is expected to be a comma delimited list. See the examples for more clarification.

If useeval=TRUE, text elements are evaluated using eval. If useeval=FALSE text elements are coerced using as.numeric. eval is more forgiving to input, i.e. eval of text input '1/2' returns .25, but as.numeric of text '1/2' returns NA. However, eval does leave an opening for unchecked code to be run in R. Text containing parenthesis or brackets is not put into either eval or as.numeric by the grade.parse functions, but there is still a risk. If you are concerned, set useeval=FALSE.

Value

grade.parse and grade.parseset returns either a vector of the values, or NULL if the input was not valid.

grade.parsechunk returns either a single value, or NULL if the input was not valid.

grade.isscalar returns TRUE if x is a scalar (vector of length 1), FALSE otherwise.

Note

The grade main page contains a discussion of the common parameters correctans, studentans, useeval, usena, useinf, quiet.

See Also

grade grade.set grade.number

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
grade.parse("[1, 2, 3]") # returns c(1,2,3)
grade.parse("[NA, 1, 2]") # returns NULL
grade.parse("[NA, 1, 2]", usena=TRUE) # returns c(NA, 1, 2)
grade.parse("[pi]") # returns 3.141....
grade.parse("[pi]", useeval=FALSE) # returns NULL

grade.parsechunk("1") # 1
grade.parsechunk(",1") # NULL
grade.parsechunk("[1]", quiet=FALSE) # NULL, with error message

grade.parseset("[1,2,3]") # c(1,2,3)

grade.isscalar(1) # TRUE
grade.isscalar(c(1,2)) # FALSE
grade.isscalar(NA) # FALSE
grade.isscalar(NA, usena=TRUE) # TRUE
grade.isscalar(Inf) # FALSE
grade.isscalar(Inf, useinf=TRUE) # TRUE