huesecon
huesecon

Reputation: 143

Curly Brackets Within Functions in R

I’ve come across functions with the following format x({…}) instead of just x( ). As an example:

suppressWarnings({ yahoo_answer <- tryCatch({ getSymbols(ticker, src ="yahoo")
 }, error = function(err) { NA }) })

Here suppressWarnings is a function, but inside of it is code enclosed by curly brackets.

What is the purpose of the curly brackets within functions? I have an idea of how and why it works here for tryCatch in particular, but I don’t know enough to generalize this and put this into my own code in other applications. Can anyone help me understand how and when you would use this kind of structure?

this answer in stackoverflow kind of touches on it:

How to write trycatch in R

I also posted this on another forum, but no luck.

Upvotes: 0

Views: 4175

Answers (1)

Thierry
Thierry

Reputation: 18487

The curly brackets allow to place multiple lines of code within suppressWarnings(). In your example they are not required since you have only one command.

suppressWarnings({
    warning("test1")
    warning("test2")
})

Upvotes: 2

Related Questions