Add error messages when using + with layers and scales #2057
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In my experience, one of the most common and inscrutable error messages for new users is when they forget a
+
and get the error message:non-numeric argument to binary operator
. For example, something like this:I would like to add a more informative error message in the style of #2056 and the error messages in dplyr, e.g. this.
The obvious approach would be to add a function(s)
+.Layer
or+.ggproto
, but when there is a valid addition of aggplot
andLayer
object that produced an "Incompatible Methods" error since S3 generics in the Ops group dispatch on both arguments and only work if only one has a specific method, or both have the same method. Something which it seems like you encountered before here.So the only way I could make this work is to make
ggplot
,theme
andggproto
inherit from the same S3 class, and define the+
method with that class. As far as I can tell, currently thegg
class is used for this purpose with respect to theggplot
andtheme
classes, with only the+.gg
method defined. My solution was to change the class given to defaultggproto()
objects toc("ggproto", "gg")
. This feels unclean, but didn't break any of the tests. But, it seemed like the simplest way accomplish the stated objective. Given that this is adding a class toggproto
objects, it may not be worth it to change the error messages, but I thought I'd propose the change.