简体   繁体   English

包含`:=`的deparse表达式?

[英]deparse expressions containing `:=`?

Expressions containing := don't deparse nicely : 表达式包含:=不要很好地deparse

call1 <- quote(f(a = b(c = d)))
call2 <- quote(f(a := b(c := d)))

# nice
deparse(call1)
#> [1] "f(a = b(c = d))"

# not nice
deparse(call2)
# [1] "f(`:=`(a, b(`:=`(c, d))))"

I would like to get the following output from call2 : "f(a := b(c := d))" . 我想从call2得到以下输出: "f(a := b(c := d))"

I'm looking for a general solution that deparses := just like = or <- in all situations. 我正在寻找一个解决方案:= =在所有情况下都是=<-


A workaround 解决方法

This workaround uses the fact that <<- has similar or same precedence and is not often used. 此解决方法使用<<-具有相似或相同的优先级并且不经常使用的事实。 I substitute := by <<- in the original call, then it deparses nicely, and I gsub it back to := . 我代替:=<<-在原来的呼叫,然后deparses很好,我gsub:= I would like a clean and general solution though. 我想要一个干净而通用的解决方案。

gsub("<<-",":=", deparse(do.call(
  substitute, list(call2, list(`:=` = quote(`<<-`))))))
#> [1] "f(a := b(c := d))"

You can achieve your desired result using rlang::expr_deparse() which offers some printing improvements. 您可以使用rlang::expr_deparse()来实现您想要的结果,它提供了一些打印改进。

rlang::expr_deparse(call2)

[1] "f(a := b(c := d))"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM