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

fix inability to debug S4 methods #130

Open
mbjones opened this issue Nov 21, 2020 · 0 comments
Open

fix inability to debug S4 methods #130

mbjones opened this issue Nov 21, 2020 · 0 comments
Milestone

Comments

@mbjones
Copy link
Member

mbjones commented Nov 21, 2020

We implement methods in S4 as anonymous functions which are used within the setMethod call to define a method. Because of this, the R debugger does not have access to the source code for that implementation, and debugging is much harder. In addition, IDE tools like RStudio have function lists that are less than useful (just lists of undifferentiated anonymous functions). To fix this, I propose refactoring our code to eliminate use of these anonymous functions by creating non-exported private functions in each class. For example, our current code has methods defined like:

setGeneric("getSize", function(x, ...) { standardGeneric("getSize") })

setMethod("getSize", "DataPackage", { 
    function(x) {
        return(length(x@objects))
    }
})

and I propose we change that to:

setGeneric("getSize", function(x, ...) standardGeneric("getSize") )

getSize_ <- function(x) {
    return(length(x@objects))
}

setMethod("getSize", "DataPackage", getSize_)

The new non-exported getSize_ function now is visible to debugging code and IDEs. In addition, note that I now removed the anonymous block wrapping the standardGeneric call, as recommended by Hadley Wickham in Advanced R.

I've checked in an example of how this works for the DataPackage class in the feature-debuggable-S4 branch. We would need to implement this for all o the classes, which will take a bit of time but is fairly rote if done without other functional changes.

@gothub @amoeba Comments and thoughts appreciated.

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

3 participants