| mdeparse | R Documentation |
R's built-in deparse rather messes up a:=b, a?b, and ?a, destroying their elegance. This doesn't— though for ?a it does wrap the result in superfluous parentheses. There might be a few superfluous parens in other cases too, but base::deparse does that too (to safeguard reparsability).
This works (quickly) by first using substitute to replace calls to := and ? by fake user-defined operators (%<something>%), then calling deparse, then gsub to re-replace the user-def-ops by := and ?. The <something> is meant to be a string that could never occur accidentally in deparse output (ie no character constant or name could ever deparse to it)— I hope I got it right!
I am not entirely sure about precedence. Because user-defined ops have higher precedence than := or ?, what I think happens is that deparse puts in extra parentheses to safeguard precedence. I don't remove them, so I think the end result is correct in the sense that parse(text=mdeparse(expr)) will keep precedence correct, though maybe with extra parens.
Apparently rlang::expr_deparse also handles := and ? sensibly (and doesn't put the parens on ?a), but it is slow because it does everything itself, and crikey it is a big dependency. mdeparse is fast because it's a beautiful hack.
While I was thinking about this, I came upon the doubt package, which is a really clever thing— kudos to the author!
mdeparse(expr, ...)
expr |
what to deparse |
... |
other args for |
Character vector.
deparse( quote( a:=b)) # [1] "`:=`(a, b)"
mdeparse( quote( a:=b)) # [1] "a := b"
mdeparse( quote( a?b)) # [1] "a ? b"
deparse( quote( a?b)) # [1] "`?`(a, b)"
mdeparse( quote( ?b)) # [1] "(?b)" best I could do--- sorry!
deparse( quote( ?b)) # [1] "`?`(b)"
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.