簡體   English   中英

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

[英]deparse expressions containing `:=`?

表達式包含:=不要很好地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))))"

我想從call2得到以下輸出: "f(a := b(c := d))"

我正在尋找一個解決方案:= =在所有情況下都是=<-


解決方法

此解決方法使用<<-具有相似或相同的優先級並且不經常使用的事實。 我代替:=<<-在原來的呼叫,然后deparses很好,我gsub:= 我想要一個干凈而通用的解決方案。

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

您可以使用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