You could also use missing()
to test whether or not the argument y
was supplied:
fooBar <- function(x,y){
if(missing(y)) {
x
} else {
x + y
}
}
fooBar(3,1.5)
# [1] 4.5
fooBar(3)
# [1] 3
More Related Contents:
- Transform the form of the function's result and get the maximum value
- how to apply a function on a data set?
- Error: could not find function … in R
- How can I view the source code for a function?
- How to use R’s ellipsis feature when writing your own function?
- data.table objects assigned with := from within function not printed
- Returning multiple objects in an R function [duplicate]
- What ways are there to edit a function in R?
- dplyr: “Error in n(): function should not be called directly”
- converting multiple columns from character to numeric format in r
- Function to calculate R2 (R-squared) in R
- Reading multiple files and calculating mean based on user input
- Apply a function to every row of a matrix or a data frame
- Finding out which functions are called within a given function [duplicate]
- R function with no return value
- How do I override a non-visible function in the package namespace?
- What is the meaning of the dollar sign “$” in R function()?
- How to compare functions?
- Return a data frame from function
- R: Obtaining Rules from a Function
- Object not found error with ddply inside a function
- Call by reference in R (using function to modify an object)
- R: x ‘probs’ outside [0,1]
- R function not returning values
- Using get() with replacement functions
- Getting a function name as a string
- Run a custom function on a data frame in R, by group
- R specify function environment
- How to use data.table within functions and loops?
- Test if an argument of a function is set or not in R