Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document caller_arg() limitation in print() #1690

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

DanChaltiel
Copy link

Hi,

I stumbled upon the caller_arg() limitation when redefining a print method for hours without understanding, and thought it might deserve a bit of documentation.

As stated in https://stackoverflow.com/a/4963041/3888000:

This is a rather special case, as R substitutes foo by its value before calling print when you type the name at the command line.
[...]
There is no way on earth you'll extract the name of the object from anywhere. It's simply not there in the callstack.

Here is a rlang specific reprex:

my_obj=1
class(my_obj)="bar"
print.bar = function(x) rlang::caller_arg(x)
mean.bar = function(x) rlang::caller_arg(x)
my_obj
#> x
mean(my_obj)
#> [1] "my_obj"

#however, this works as intended
print(my_obj)
#> [1] "my_obj"

Created on 2024-02-21 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant