I've done this before when I am working with folks or students who dont commonly use R and I dont trust that they know the difference between entering "this" and this. So to save me some repeated explaining, I generally add a work-around if-then statement that separates the substitute to capture the input without evaluating with deparse unless needed. A generalized example is something like this:
dummyFunction <- \(x) {
x <-substitute(x)
if(is.symbol(x)){
paste0("Testing ", deparse(x))
}
else {
paste0("Testing ", x)
}
}
dummyFunction(this)
# [1] "Testing this"
dummyFunction("this")
# [1] "Testing this"
To be clear, this is not good practice for solid, reliable, or safe coding. But it has saved me plenty of of emails for non-critical tasks :)
However, in your specific case your desired input dummyFunction(2025-B3) is never going to be possible in R because unquoted objects can’t start with a number/digit, so 2025-B3 will always be parsed as arithmetic (2025 - B3), not as a single symbol. You will need to pull the quote() to the function call, i.e., dummyFunction(quote(2025-B3)). You will also need to gsub that text out as well, so:
dummyFunction <- \(x) {
y <- substitute(x)
if(!is.character(y)){
gsub("\\bquote\\(|\\)", "",
gsub(" ", "", gsub("\"", "", deparse(y)))
)
}
else {
gsub(" ", "", gsub("\"", "", x))
}
}
dummyFunction(quote(2025-B3))
# [1] "2025-B3"
dummyFunction("2025-B3")
# [1] "2025-B3"
Note that I changed is.symbol() to !is.character() here because y <- substitute(x) becomes the expression quote(2025 - B3) (a call, not a symbol).
Importantly, this will only work on inputs like quote(2025-B3) and NOT on quote(2025-01A) because B3 is a valid variable name (even though its not defined), so 2025-B3 is legal syntax (R thinks "2025 minus something, somewhere called B3"). However, R does not like digits to start names and so 01A is not a valid number or name, so quote(2025-01A) is invalid syntax and never reaches the function at all.
Hopefully for these reasons you can see the moral of the story is that you shouldn't do this.
gsub("\"", "", ...)in your secondgsubfunction? i.e.gsub(" ", "", gsub("\"", "", deparse(quote("2025-B3"))))? As currently written, I am getting errors due to incorrect syntax.