Skip to content

Commit 296e936

Browse files
XuhuaHuangCopilot
andcommitted
Finalize notes on R functions
Co-authored-by: Copilot <copilot@github.com>
1 parent 4d46366 commit 296e936

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

R/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,42 @@ which(x > 3) # returns c(4, 5)
216216

217217
Vectorized logical operations and logical indexes are extremely useful to compare, find, replace data elements.
218218

219+
Write functions in `R` to turn repeated, complex, or parameterized logic into reusable, modular, and reliable building blocks. Eliminate repetition, abstraction, modularity, reproducibility, encapsulation, vectorized design, code clarity and readability.
220+
221+
Big computation job can be broken into many small jobs which can be executed by a sequences of R functions either in sequential or in parallel.
222+
223+
Before any real computation, should use some procedures to check inputs: checking data type, data coercing, logical testing, etc.
224+
219225
- Domain: arguments lists
220226
- Function body: expressions that define the function
221227
- Range: return values
228+
229+
If the end of the function body is not an explicit `return` statement, the value of the last evaluated expression is returned.
230+
231+
### Variable Number of Arguments
232+
233+
- `...` in a function argument list is used to specify that an arbitrary number of arguments are to be passed to a function within the body of the function.
234+
- `list(...)` to capture the variable arguments as a list
235+
236+
### Required and Optional Arguments
237+
238+
- Required arguments are those arguments for which the function definition provides neither a default value nor instructions on what to do if the argument is missing. All other arguments are optional.
239+
240+
### Testing Argument Lists
241+
242+
- `is.type(x)` to check if an argument is of a specific type, `is.vector`, `is.matrix`, `is.data.frame`, `is.integer`, `is.double`, `is.character`
243+
- `as.type(x)` to convert an argument to a specific type, `as.vector`, `as.matrix`, `as.data.frame`, `as.integer`, `as.double`, `as.character`
244+
245+
### Logical Operators
246+
247+
- `&` vectorized AND
248+
- `|` vectorized OR
249+
- `&&` control flow AND
250+
- `||` control flow OR
251+
252+
### Error Handling
253+
254+
- `stop("error message")` to throw an error and stop execution
255+
- `warning("warning message")` to throw a warning and continue execution
256+
- `try(expr)` to execute an expression and catch any errors without stopping execution
257+
- `tryCatch(expr, error = function(e) { ... })` to execute an expression and handle errors with a custom function

0 commit comments

Comments
 (0)