On Fri, 1 Sep 2023 08:14:12 +0000 Hanyu Song <hanyu.s...@duke.edu> wrote:
> b. I read from the documentation of the R package "reticulate" that > we should delay load the Python modules, but it is not entirely clear > to me how to do it. > Do I need to create any virtual environment? Not in the package code (unless the package is specifically designed to manage python virtual environments and therefore clearly has user's permission to access the filesystem). Virtual environments are here, like R libraries, for the user to create and manage. > Are the following lines of code sufficient for that purpose? > > ctef <- NULL > .onLoad <- function(libname, pkgname) { > ctef <<- reticulate::import("ctef", delay_load = TRUE) > } Yes, according to https://cran.r-project.org/package=reticulate/vignettes/package.html. It's important both to avoid a plain ctef <- reticulate::import("ctef") in the package namespace (because that's evaluated at source package installation time / binary package build time, and you don't want to capture references to a Python module that could have been moved since the R package has been installed) and to delay it further using the delay_load = TRUE argument (to let the user alter the Python search path even after loading your R package that depends on the python module). > c. How shall I import the module in my R code? Now that you have the object created above and initialised from .onLoad, your function_that_depends_on_ctef should be able to just use it: function_that_depends_on_ctef <- function(X, k) { input <- as.matrix(X) ctef$ctef$ctef(input,as.integer(k)) } Make sure to declare the dependency in SystemRequirements: of your DESCRIPTION. -- Best regards, Ivan ______________________________________________ R-package-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel