I'm experimenting with simple manipulation of R code and trying to generate an object equivalent to
substitute(function(x) x)
I am aware I can do something around these lines
as.call(list(as.symbol("function"), as.pairlist(alist(x=)), as.symbol("x")))
but I am looking for a way to achieve as.pairlist(alist(x=)) without resorting alist or, if that's not possible, allowing me to generate equivalent expression without hard-coding the thing or parsing strings.
I was tinkering with things like as.call(list(as.symbol("="), as.symbol("x"))), but that's seems to be a dead end for now.
A programmatic alternative to
as.pairlist(alist(...))is to initialize a pairlist of positive length then assign names (well, "tags") and values:More generally:
We condition on
n >= 1Lbecause a zero-length pairlist isNULLand assigning names or values toNULLis an error.We condition on
missing(v) || !is.null(v)because we don't want to assignNULLtor[[i]](which would decrease the length ofrby 1) and we don't want to callis.nullwith no argument: