function factory comments with rlang

Can I add comments to functions created by rlang::new_function()?

I can use base R code to create a function that has a comment:

f <- function(x){
  function(t){
   #This is a comment
   t+x
  }
}

And this will output the following:

g <- f(2)
g
function(t){
        #This is a comment
        t + x
    }

(with the x variable hidden within the environment() of g())

I'm wondering whether there is a way to add a comment like this to a function created using new_function() in {rlang}

f <- function(x) {
  new_function(
    args = alist(t=),
    body = call2(quote(`{`),
                 add_comment_here("This is a comment"),
                 expr(t + !!x)
    )
  )
}

Which should output the following:

g <- f(2)
g
function(t){
        #This is a comment
        t + 2
    }

If this is possible, then it should also then be possible to construct comments for functions
which could present more information to an end user about what any hidden variables might contain

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.