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

globalsOf() on locally defined S3 methods (fails) #2

Closed
HenrikBengtsson opened this issue May 19, 2015 · 3 comments
Closed

globalsOf() on locally defined S3 methods (fails) #2

HenrikBengtsson opened this issue May 19, 2015 · 3 comments

Comments

@HenrikBengtsson
Copy link
Collaborator

Background

globalsOf() on locally defined S3 methods identifies the generic functions but not the corresponding S3 methods. This is not surprising.

As long as the S3 methods are implemented in packages and those packages are loaded/attached, calling the generic function - as found by globalsOf() - will work. The S3 method to be dispatched on will be found at run time (since the class of the object needs to be known).

Problem

However, if an S3 method is defined "locally" (e.g. in the global environment as part of prototyping/development), that S3 method will not be part of the set of globals that are exported to the job/task.

Workaround

The workaround is to:

  1. Check whether an identified global is a generic function or not.
  2. If it is a generic function, use methods(<generic>) to search for all matching S3 methods.
  3. For each S3 method, check if it is in a package name space or in another environment (e.g. global environment).
  4. If in a package namespace, add the package to set of required packages to be loaded (conservative approach).
  5. If in a non-package environment, then add the S3 method mth to the list of globals.
  6. Run globalsOf(mth) on added S3 method and repeat to make sure it also gets everything it needs.

UPDATE 2016-04-10: This strategy can really only apply to globalsOf() and not to findGlobals(), because it's only with the former we retrieve objects and can inspect them for being generic functions or not.

@HenrikBengtsson HenrikBengtsson changed the title findGlobals() on locally defined S3 methods (fails) globalsOf() on locally defined S3 methods (fails) Apr 10, 2016
HenrikBengtsson added a commit that referenced this issue Apr 10, 2016
Add c() and unique() for Globals class.
Add internal utility functions isGenericS3(), s3GlobalsOf() and
addS3Methods().
@HenrikBengtsson
Copy link
Collaborator Author

The feature/local-s3 contains a prototype for this. However, many more tests need to be added. For instance, in addition to local S3 methods, also S3 methods in other packages (make sure those are loaded when exported) should be tested.

There should also be similar tests added to the future package to make sure it works when such globals are exported and used in, e.g. external R sessions.

There's also S4 generics/methods. How should/could we handle those?

@HenrikBengtsson
Copy link
Collaborator Author

HenrikBengtsson commented Aug 6, 2018

Related: How to find the location of "registered S3method for [[" methods? For example,

> attr(methods("[["), "info")
                   visible                       from generic  isS4
[[.bibentry          FALSE registered S3method for [[      [[ FALSE
[[.data.frame         TRUE                       base      [[ FALSE
[[.Date               TRUE                       base      [[ FALSE
[[.dendrogram        FALSE registered S3method for [[      [[ FALSE
[[.factor             TRUE                       base      [[ FALSE
[[.listenv           FALSE registered S3method for [[      [[ FALSE
[[.numeric_version    TRUE                       base      [[ FALSE
[[.pdf_doc           FALSE registered S3method for [[      [[ FALSE
[[.person            FALSE registered S3method for [[      [[ FALSE
[[.POSIXct            TRUE                       base      [[ FALSE
[[.POSIXlt            TRUE                       base      [[ FALSE

@HenrikBengtsson
Copy link
Collaborator Author

Moved to Issue #47

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

No branches or pull requests

1 participant