So I'm trying to write this function:
myfun <- function(data,value=NULL,former_value=NULL,text=NULL,d=1,e=1,f=1){
## if the user specifies an input to `value` and/or `former_value` and/or `text`
## then grab those columns from the df `data`, so somethin like this:
if (!is.null(value)) v = pull(data,!!value)
if (!is.null(former_value)) f = pull(data,!!former_value)
if (!is.null(text)) t = pull(data,!!text)
## Then, do stuff with the SE inputs `d`,`e`,`f`
}
I know that answers such as this one, and also many of the tidyverse functions use ...
to deal with optional inputs, but in my case, I need the inputs to match to specific things, and thus they are not interchangeable... so I'm not sure how to use ...
if I expect certain specific things from it.
What have I tried?
- Well, there's the obvious
is.null(c)
check I put above, but it doesn't work becausec
might exist in global. - I can first
y=enquo(x)
and then check the status of!!y
, but doing so is a bit hard because the outputted object objects are more or less similar in structure. The best way I found is quite convoluted... likeas.character(syms(v)[[2]]) != "." & as.character(syms((v))[[2]]
but there must be a better way.
Or am I just trying to hard? I have two easy options... I can 1) Just eliminate the whole NSE
and just make the inputs vectors before the function call, or 2) make the inputs mandatory. But dammit, if I always took the easy way I would never learn! So is there any hope?