Confusing error message when function is called with string argument

Here is a function that has a bug:

f_with_arg <- function(an_argument) {
  # Oops! Object a_bug does not exist
  this_is <- a_bug
}

If I call this function with an integer argument, I get a very sensible error message:

> f_with_arg(0)
Error in f_with_arg(0) : object 'a_bug' not found

But if I call it with a string, I get a non-sensical error message:

> f_with_arg("a_string")
Error in f_with_arg("a_string") : object '"a_string"' not found

Is this expected? It makes it hard to find my typos (of which I make many!).

Curiously, a list that doesn't have a string does what one would expect, while a list with a string produces the same problematic error message:

> f_with_arg(list(foo=1))
Error in f_with_arg(list(foo = 1)) : object 'a_bug' not found
> f_with_arg(list(foo=1, bar="a_string"))
Error in f_with_arg(list(foo = 1, bar = "a_string")) : 
  object '"a_string"' not found

It's been many moons since I've been mystified by R like this. Can you please help me understand?

Thank you,

-John

I get a reasonable error message.

f_with_arg <- function(an_argument) {
+   # Oops! Object a_bug does not exist
+   this_is <- a_bug
+ }
> f_with_arg("a_string")
Error in f_with_arg("a_string") : object 'a_bug' not found

I'm using R 4.3.1

Aha! I was on 4.3.0 (on MacOS, if it matters). Upgrading to 4.3.1 fixes the issue.

I didn't see anything relevant in the release notes. :person_shrugging:

Thanks for the reply!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.