Skip to content
Prev 7658 / 12125 Next

[R-pkg-devel] Question on best approach to develop R package that wraps old complex Python2 software

I would suggest the reticulate library in R. The few most important for
your case are reticulate::use_python_version and reticulate::import.
For example, in your R package, you should start with:


# change this to the name of the module you need
numpy <- NULL


.onLoad <- function (libname, pkgname)
{
    reticulate::use_python_version("2.7")  # change this as you need to


    # .onLoad happens before the namespace is locked, so this is legitimate
    numpy <<- reticulate::import("numpy", delay_load = list(
        on_error = function(c) stop(
            "unable to import 'numpy', try ",
            sQuote("reticulate::py_install(\"numpy\")"),
            " if it is not installed:\n  ",
            conditionMessage(c)
        )
    ))
}


when your package's namespace is loaded, this will load the version of
python you need to use, and will lazy-import the module you need for your
python session.

On Tue, Jan 25, 2022 at 8:52 AM Alexandru Voda <alexandru.voda at seh.ox.ac.uk>
wrote:

  
  
Message-ID: <CAPcHnpS4XCEfCCHRODr+oyxhm0iPg0cmhdGObSj=VkPv9mKg=A@mail.gmail.com>
In-Reply-To: <CWXSPR01MB0039FE96F0768FF9931DD3DEEF5F9@CWXSPR01MB0039.GBRP265.PROD.OUTLOOK.COM>