【发布时间】:2021-03-02 10:51:14
【问题描述】:
quote(# this is a comment)
我该如何做类似上述的事情?
【问题讨论】:
-
评论不是代码,所以你不能引用它。展示您的完整用例可能会很有用。
标签: r metaprogramming non-standard-evaluation
quote(# this is a comment)
我该如何做类似上述的事情?
【问题讨论】:
标签: r metaprogramming non-standard-evaluation
quote() 在其wholeSrcref 属性中捕获原始代码,该属性保留了 cmets:
x <- quote({
## This is a comment
})
src <- attributes(x)$wholeSrcref # <--- preserves the comment
但是,这会返回一个 srcref 类的对象,而不是可以传递给 eval() 的真正表达式。根据您要执行的操作,您可能会发现 these functions for manipulating srcref objects 很有用。例如,
as.character(src)[2]
[1] " ## This is a comment"
【讨论】:
目前尚不清楚您要达到的目标,但以下事情应该可行:
您可以轻松地将主题标签存储在字符串中:
string<- "# this is a comment"
如果您需要将其放在引号中,您可以这样做:
dQuote("# this is a comment",q = options(useFancyQuotes=FALSE))
这会返回:"\"# this is a comment\""。
选项useFancyQuotes=FALSE 确保使用“正常”引号(而不是印刷引号)。如果您忽略此参数,结果将是 "“# this is a comment”"
quote("# this is a comment"),它将返回"# this is a comment"
【讨论】: