quote(# this is a comment)
How can I do something like the above?
quote()
captures the original code in its wholeSrcref
attribute, which preserves comments:
x <- quote({
## This is a comment
})
src <- attributes(x)$wholeSrcref # <--- preserves the comment
However, this returns an object of class srcref
, not a true expression that can be passed to eval()
. Depending on what you are trying to do, you may find these functions for manipulating srcref objects useful. For example,
as.character(src)[2]
[1] " ## This is a comment"