Giter VIP home page Giter VIP logo

Comments (8)

bwiernik avatar bwiernik commented on June 18, 2024 1

If the object is an S3 object, this should work --

getS3method("predict", class(object)[1]) |> rlang::ns_env_name()

If the object is S4 (eg, from lme4) --

attr(findClass('lmerMod')[[1]], "name")

from insight.

tripartio avatar tripartio commented on June 18, 2024

@bwiernik Thanks for your suggestions. I will try to test them soon when I have a free moment.

Could such functionality be integrated as a function in the package?

from insight.

bwiernik avatar bwiernik commented on June 18, 2024

Can you say more about the use case? If you have the package to fit the model, then the package should certainly be available to supply its methods

from insight.

tripartio avatar tripartio commented on June 18, 2024

@bwiernik, the context is parallel processing with my ale package. Most of the package functions receive a model as input and then analyze the model for various interpretable machine learning (IML) tasks.

So, for example, when my ale() function runs sequentially (without parallel processing), there is no problem. As you indicated, as long as the package with which the model was created is installed on the system, the ale() function runs fine. But since I added parallel processing, the process has started choking, even though I use furrr, an advanced parallel processing package that takes care of almost everything automagically. The problem is that furrr has to send the necessary package environments to each parallel worker so that each worker can independently run the code. It cannot automatically detect that my code needs further packages, so it chokes. So, my code needs to tell furrr which extra packages are needed for the parallel workers to do their tasks.

The current version of my code requires users to specify a model_packages argument just for the sake of parallel processing. I would like to modify my code so that it automatically detects the model's package so that users would not need to supply this argument. This is what I would like insight to do for me.

So, I think that my use case could be generalized to parallel processing when the source package of certain complex objects (in my case, models) needs to be detected.

from insight.

tripartio avatar tripartio commented on June 18, 2024

If the object is an S3 object, this should work --

getS3method("predict", class(object)[1]) |> rlang::ns_env_name()

@bwiernik Thanks; I have now implemented this check for S3 objects in my package and it now automatically detects and loads the appropriate packages.

I don't understand the S4 check code you gave (probably because I rarely work with S4 objects), so my package is now configured to automatically check for the S3 package and then give a graceful error message if it cannot be detected. Then users can explicitly specify the packages with my existing manual mechanism. That is acceptable, since it should work automatically for most users and only require manual intervention for a few complicated cases.

Would it be feasible to incorporate such a check into the {insight} package, extended with checks for S4 objects as well?

Regardless, I appreciate your help. Your little S3 code has let me simplify my function usage for most users.

from insight.

bwiernik avatar bwiernik commented on June 18, 2024

R has two widely used class systems—S3 and S4 (and several less-used ones). S3 is most widely used, but some major modeling packages do use S4 (lme4 and OpenMx are the first ones that come to mind).

The function isS4() can detect if an object is S4 or not. The code above will return the namespace associated with the class of the S4 model.

from insight.

tripartio avatar tripartio commented on June 18, 2024

If the object is S4 (eg, from lme4) --

attr(findClass('lmerMod')[[1]], "name")

But you specified the name of the S4 class in the code. I am not sufficiently familiar with S4 to convert this to code where I have an object of undetermined type and then probe its namespace (as with your S3 snippet above).

from insight.

bwiernik avatar bwiernik commented on June 18, 2024

Oh sorry

attr(findClass(object)[[1]], "name")

from insight.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.