Search code examples
rnse

Given an expression of the form `fn(NULL)` how do I replace the null with `lhs=rhs`?


Consider

ex1 = quote(fn(NULL))

and suppose I wish to make ex1 equals to fn(lhs=rhs) in expression form, how do I do that?

since

ex1[[2]] = quote(lhs=rhs)

gives ex1 = fn((lhs=rhs)) and I can't seem to get rid of the parenthesis.


Solution

  • You can do

    ex1 <- quote(fn(NULL))
    ex1
    #> fn(NULL)
    
    ex1[[2]] <- quote(rhs)
    names(ex1)[2] <- "lhs"
    ex1
    #> fn(lhs = rhs)
    

    Created on 2022-01-29 by the reprex package (v2.0.1)