1
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.

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! Coming from Python I get a little confused with the syntax sometimes. The error messages in R are a bit misleading.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.