Test if an argument of a function is set or not in R
You use the function missing()
for that.
f <- function(p1, p2) {
if(missing(p2)) {
p2=p1^2
}
p1-p2
}
Alternatively, you can set the value of p2 to NULL by default. I sometimes prefer that solution, as it allows for passing arguments to nested functions.
f <- function(p1, p2=NULL) {
if(is.null(p2)) {
p2=p1^2
}
p1-p2
}
f.wrapper <-function(p1,p2=NULL){
p1 <- 2*p1
f(p1,p2)
}
> f.wrapper(1)
[1] -2
> f.wrapper(1,3)
[1] -1
EDIT: you could do this technically with missing()
as well, but then you would have to include a missing()
statement in f.wrapper
as well.
Is there R code to test if variable is a function
You can use is.function
to check if some object is a function, for example:
is.function(mean)
or is.function(`[[`)
would return TRUE, also on a side note there is another function is.primitive which tests for builtins and specials, but in your case you probably would want is.function
In your case :
myfun <- function(fun){
if(is.function(fun)){
return(fun(1))
} else {
warning("You are not passing fun as function")
}
}
Testing using:
fun <- function(x)x+2
would yield 3
, but fun <- 1
it would give you warning for calls on myfun(fun)
check Argument in a function r
If I understand you right, you probably want this:
lm.IV2 <- function(endogen, IV=NULL, exogen, df) {
if (! is.null(IV)) {
...
}
else {
...
}
}
Simple syntax question regarding f. in R
The dot at the end has no effect on the syntax, it's just like any other legal character in an identifier. A dot at the beginning has very little effect, but it does mean ls()
won't display the variable by default.
In the examples you posted, the author probably used f.
to remind the reader that the default value was f
, and g.
reminds the reader of g
.
Partial argument matching means that a user could write test(g = somefn)
and R would treat somefn
as the value of the g.
argument.
In the referenced question where you saw this code, the original function had header
g <- function(x, T, f=f) {
This won't work, because the default value has the same name as the argument, and defaults are evaluated in the frame of the function. So it says "If the argument f
is not specified, set it to the argument f
." That makes no sense. The corrected version that you posted says "If the argument f.
is not specified, set it to the global variable f
."
Related Topics
Update Handsontable by Editing Table And/Or Eventreactive
Cartesian Product with Dplyr R
R Assigning Ggplot Objects to List in Loop
Combine Two Data Frames with All Posible Combinations
Combine Several Data Frames in the Global Environment by Row (Rbind)
Plotly: Updating Data with Dropdown Selection
Dplyr Broadcasting Single Value Per Group in Mutate
How to Convert Time (Mm:Ss) to Decimal Form in R
Connect to Postgres via Ssl Using R
R- How to Dynamically Name Data Frames
Fixing Cluttered Titles on Graphs
Counting Number of Instances of a Condition Per Row R
Different Legend-Keys Inside Same Legend in Ggplot2
Error in Loading Rgl Package with MAC Os X
Protect/Encrypt R Package Code for Distribution
Conditional Binary Join and Update by Reference Using the Data.Table Package