简体   繁体   中英

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.

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")
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM