【问题标题】:in R, how do I quote a comment using quote or bquote?在 R 中,如何使用 quote 或 bquote 引用评论?
【发布时间】:2021-03-02 10:51:14
【问题描述】:
quote(# this is a comment)

我该如何做类似上述的事情?

【问题讨论】:

  • 评论不是代码,所以你不能引用它。展示您的完整用例可能会很有用。

标签: r metaprogramming non-standard-evaluation


【解决方案1】:

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"

【讨论】:

    【解决方案2】:

    目前尚不清楚您要达到的目标,但以下事情应该可行:

    1. 您可以轻松地将主题标签存储在字符串中:

      string&lt;- "# this is a comment"

    2. 如果您需要将其放在引号中,您可以这样做:

      dQuote("# this is a comment",q = options(useFancyQuotes=FALSE))

    这会返回:"\"# this is a comment\""

    选项useFancyQuotes=FALSE 确保使用“正常”引号(而不是印刷引号)。如果您忽略此参数,结果将是 "“# this is a comment”"

    1. 您也可以只写quote("# this is a comment"),它将返回"# this is a comment"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-27
      • 1970-01-01
      • 2012-01-13
      • 1970-01-01
      • 2013-07-29
      • 1970-01-01
      • 2014-12-13
      相关资源
      最近更新 更多