1

When typing

gsub(" ", "", gsub("\"", "", deparse(quote(2025-B3))))

the output is [1] "2025-B3"

Typing

gsub(" ", "", gsub("\"", "", deparse(quote("2025-B3"))))

also gives [1] "2025-B3"

I would like to create the function dummyFunction which has the following property:

Typing

dummyFunction("2025-01A")
# as well as 
dummyFunction(2025-01A)

both give the output [1] "2025-01A"

How can that be achieved?

3
  • 7
    There might be great reasons to do this, but it sounds like it has the potential to be an XY problem, where there might be good reasons to approach this in an entirely different way. Can you describe more about your situation and motivation for this approach? Commented Sep 21 at 23:41
  • 1
    Are you sure the code as provided works? I get a variety of errors using R 4.2.0 on mac. Commented Sep 21 at 23:46
  • 1
    In your functions, do you mean gsub("\"", "", ...) in your second gsub function? i.e. gsub(" ", "", gsub("\"", "", deparse(quote("2025-B3"))))? As currently written, I am getting errors due to incorrect syntax. Commented Sep 22 at 0:16

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.