2

If I use

d <- function(x){deparse(substitute(x))}

for letters or number all works fine. d(a1) gives "a1", for example. But using special characters results in an error. I want to use d(+) and get "+" as result.

From comments:

I want "+" == d(+) to give a TRUE. In other words, I do not want to use d(`+`). Is this possible? The function is part of a code that will await input from non-R-users and that is why I want to avoid using `` for special characters (I do not want explain to every user what a special character is).

  • @Hong I expect to get the exact thing I type in. So `"+" == d(+)` must be `TRUE` just like `"a1" == d(a1)"` is. Any idea? Yeah, that is nonsense but that is not what I look for. –  Feb 13 '20 at 20:12
  • 1
    `+` is a special character, so the parser will obviously complain about `d(+)`. Whenever you want to deal with a special operator in a way that the parser may not think is appropriate, you need to use backticks, as in `d(\`+\`)`. This is the same reason you cannot look at the function definition of `%in%` on the command line by typing in `%in%`, the parser expects there to be something around it (and complains about "unexpected SPECIAL"); but if you type `%in%`, you are telling the R parser that you are trying to deal with the SPECIAL in a way that is ... a different kind of special. – r2evans Feb 13 '20 at 20:24
  • That parser-error kicks well before your function `d` is even called, so it's not something I think you can work-around in its definition. – r2evans Feb 13 '20 at 20:26
  • @r2evans Yes, I know that this is the reason I get the error. So does this mean there is no solution to it? There is no function that gives `"+" == d(+)` a `TRUE`? I know using some `""` is not such a big deal but in the context where I want to use it for not-R-users it would be great to be able using `d(+)`. Otherwise I have to explain special characters first.. –  Feb 13 '20 at 20:26
  • If you add a `message` as the first line of your `d` function and then try `d(+)`, it means that your function is not even being called yet, so I believe there is no way to define your "normal" function such that the parser will handle parsing in the calling environment any differently. – r2evans Feb 13 '20 at 20:43
  • Side note: votes to close can happen with good intentions, and they can happen as a fly-by 3-second snap judgement. Or something in between. I understand the frustration, but I also understand that the ability to VTC anonymously is by-design (though there is frequently discussion on meta for a drive-by VTC to be given a modal encouraging a "comment to improve" or similar). – r2evans Feb 13 '20 at 20:46
  • 3
    So I might easily be wrong (my level of R-guru-ness is not interstellar), I suspect that you are outta luck trying to get what you want/need here. – r2evans Feb 13 '20 at 20:47
  • 4
    I agree with @r2evans here. Code like `d(+)` will never pass the R parser so it will never run. You're going to have to use some sort of variable quoting or strings. – MrFlick Feb 13 '20 at 21:20
  • Okay, so I've learnt something new about R. A tiny bit unsatisfying but well, variable quoting here I come. Thank you all for the comments. –  Feb 13 '20 at 21:53

0 Answers0