-2

I am trying to build a function that takes 3 arguments, a vector, whether to display or not a graph, and if yes, the type of graph. Unfortunately, i get an error and don't understand why. (This is for a class but i already validated the points through reasoning, i just want to progress on my code...)

my_function <- function(x, display = FALSE, type) {
  if (display = TRUE & type = "hist") {
    hist(x)
  } if (display = TRUE & type = "plot") {
    plot(x)
  } else { 
    summary(x) 
    }
}

I get multiples: Error: unexpected '}' in " }"

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
Thomas
  • 9
  • 1
  • Also have a look at [Error: unexpected symbol/input/string constant/numeric constant/SPECIAL in my code](http://stackoverflow.com/questions/25889234/error-unexpected-symbol-input-string-constant-numeric-constant-special-in-my-co) for a general overview. – Ronak Shah Mar 02 '17 at 04:38
  • I'm pretty sure you need a new line before the second `if` statement. R is picky like that in some cases. – Daniel D. Mar 02 '17 at 04:40
  • @DanielD. I'm pretty sure he wants `else if` logic, but maybe I'm wrong. – Tim Biegeleisen Mar 02 '17 at 04:40
  • @TimBiegeleisen oh could be. Also, the boolean compare needs a double equals `==` instead of single equals `=` in the `if` statements. – Daniel D. Mar 02 '17 at 04:45
  • @Thomas Besides the syntactical errors, I wonder about the semantics. Do you expect that the function _always_ returns `summary(x)` and in case of `display == TRUE` _additionally_ creates a graph (either `hist(x)` or `plot(x)`) as side effect? Or, are these considered as three equivalent alternatives, i.e., either `summary(x)` _or_ `hist(x)` _or_ `plot(x)`? – Uwe Mar 02 '17 at 06:46

1 Answers1

3

You are using assignments in your if conditions instead of equality checks. In addition, you need to preface every if in a chain, other than the first one, with else. Try this code:

my_function <- function(x, display = FALSE, type) {
    if (display == TRUE & type == "hist") {
        hist(x)
    } else if (display == TRUE & type == "plot") {
        plot(x)
    } else { 
        summary(x) 
    }
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • While Tim has corrected the syntatical errors, I'm questioning the semantics of the function. What will happen if someone is calling `my_function(some_vector, TRUE, "polt"`? Due to the typo in the third parameter, `summary(x)` is returned. Perhaps, this is the OP's intent to tolerate such typos but it's not save. I've spent hours on debugging stuff like this. My suggestion: `my_function <- function(x, display = FALSE, type = c("hist", "plot") {`; `type <- match.arg(type)` ; ... This will throw an error in case of `type = "polt"`. – Uwe Mar 02 '17 at 06:54
  • @UweBlock R cannot save you from yourself. :) – Roman Luštrik Mar 02 '17 at 13:50
  • Thanks for the help! Looking at Tim's correction i was able to get where i was wrong and could finish my function (i was trying to build it step by step). Thanks for the match.arg(), i didnt know this one and it will come in handy! – Thomas Mar 03 '17 at 03:50