O.rka
O.rka

Reputation: 30687

Syntax error with simple R function (Error: unexpected '}' in "}")

assert = function (expr, error) {
    # Original source: @max-gasner
    # https://stackoverflow.com/questions/8343509/better-error-message-for-stopifnot
    if (! expr) stop(error, call. = FALSE)
}


boolean = function(x, true_values=c("true", "t", "yes", "1"), false_values=c("false", "f", "no", "0"), assertion_message="Please choose either: TRUE or FALSE"){
    option = NULL
    x = tolower(as.character(x))
    (if x %in% true_values){
        option = TRUE
    }
    (if x %in% false_values){
        option = FALSE
    }
    assert(is.logical(options), assertion_message)
    return option
}

Here's what happens when in rstudio:

> assert = function (expr, error) {
+     # Original source: @max-gasner
+     # https://stackoverflow.com/questions/8343509/better-error-message-for-stopifnot
+     if (! expr) stop(error, call. = FALSE)
+ }
> 
> 
> boolean = function(x, true_values=c("true", "t", "yes", "1"), false_values=c("false", "f", "no", "0"), assertion_message="Please choose either: TRUE or FALSE"){
+     option = NULL
+     x = tolower(as.character(x))
+     (if x %in% true_values){
Error: unexpected symbol in:
"    x = tolower(as.character(x))
    (if x"
>         option = TRUE
>     }
Error: unexpected '}' in "    }"
>     (if x %in% false_values){
Error: unexpected symbol in "    (if x"
>         option = FALSE
>     }
Error: unexpected '}' in "    }"
>     assert(is.logical(options), assertion_message)
Error in stop(error, call. = FALSE) : 
  object 'assertion_message' not found
>     return(option)
Error: no function to return from, jumping to top level
> }
Error: unexpected '}' in "}"

I checked this: Error: unexpected '}' in " }" and it's not from unicode characters.

Upvotes: 1

Views: 551

Answers (1)

akrun
akrun

Reputation: 887118

The issue is in the placement of (

(if x %in% true_values)
^^

Similarly

(if x %in% false_values)
^^

It would be

if(x %in% true_values)

and

if(x %in% false_values)

Upvotes: 2

Related Questions