Search code examples
rrlang

Retain comments inside R expression


my_expr <- expr({
  # this is a comment
  this_is_code
})
my_expr

returns:

{
  this_is_code
}

But i'd like it to return:

{
  # this is a comment
  this_is_code
}

Is retaining the comment possible using rlang?


Solution

  • The comment is already retained, it just doesn't get printed to the console. Check out attr(my_expr, "wholeSrcref").