Troubleshooting common S3 method issues in R packages, focusing on `plot.foo` and visibility during package checks.
---
This video is based on the question https://stackoverflow.com/q/63494609/ asked by the user 'Mikael Jagan' ( https://stackoverflow.com/u/12685768/ ) and on the answer https://stackoverflow.com/a/63496479/ provided by the user 'MrFlick' ( https://stackoverflow.com/u/2372064/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Exported S3 method not found during check
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Resolving the Exported S3 Method Not Found Issue in R Packages
When working with R packages, particularly those that use S3 methods, developers can run into confusing situations. One common issue is encountering an error related to calling specific methods, such as plot.foo, even after they seem well-defined within the package. This guide will help demystify the error message: "Error in plot.foo(foo_out): could not find function 'plot.foo'", by explaining how R handles exported S3 methods and providing a solution to ensure your methods work correctly during package checks.
The Problem Explained
In your package, it appears that you have defined a function foo that creates objects of class "foo", and you also have a method plot.foo specifically for plotting these objects. Here’s a brief overview of the relevant code:
[[See Video to Reveal this Text or Code Snippet]]
You have directly called plot.foo(foo_object) in your @ examples section, but while it works fine in your R session using devtools::load_all, you encounter an issue when running devtools::check. This is because devtools does not recognize the method when explicitly calling it by name, leading to confusion about why it works in one context but fails in another.
Understanding the Behavior
Why devtools::check Fails
The primary reason for this discrepancy is that devtools::check analyzes your package in a way that respects the scope of method visibility. By convention, S3 methods are not exported as standalone functions even if you declare them with an @ export tag. Their primary purpose is to work with the generic function they are associated with. Despite plot.foo being defined, R treats it as an internal method meant for use only through the generic plot function.
When you run check, R looks for publicly available functions; since plot.foo is not typically called directly, it fails to recognize it unless explicitly exported. You can easily verify this by running methods(plot) to see a list of available methods. The methods that have asterisks next to them are not meant to be called directly.
The Solution
To resolve this issue, if you truly want plot.foo to be callable by name, you need to export it explicitly. That said, best practices suggest that it's typically unnecessary to export class-specific implementations of generic functions, as users should access these through the generic call.
Steps to Export plot.foo
Here’s how to modify the definition of your plot.foo function to ensure it’s available everywhere, including when using devtools::check:
[[See Video to Reveal this Text or Code Snippet]]
Key Changes:
Add the line # ' @ export plot.foo: This explicitly tells R to export the plot.foo function so that it can be found and called directly.
Maintain clarity: Remember that while exporting is possible, you should always consider if method-specific access aligns with user expectations. For the best practices, leveraging the generic plot function is sufficient for typical use cases.
Conclusion
When you're working with S3 methods in R, understanding how export visibility works is crucial to ensuring smooth package development and user experience. If you find your exported methods are not recognized during checks, remember to verify both the exports and the conventions behind S3 method usage. By adhering to these principles, you'll create more robust and user-friendly R packages.
Feel free to test your code again after implementing the discussed changes, and enjoy the world of S3 methods!
Информация по комментариям в разработке