[R-pkg-devel] Python module dependency

Hanyu Song h@nyu@@ong @end|ng |rom duke@edu
Fri Sep 1 10:14:12 CEST 2023


Hello,

I am writing an R package that depends on a very uncommonly used Python module named "ctef" and I have several questions about it:

a. How shall I write examples for the functions that depend on the Python module? Shall I just do:
#' @examplesIf reticulate::py_module_available('ctef')
#' my_function_that_depends_on_ctef(arg1, arg2)
in case the CRAN testing platform does not have the module?

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.

Are the following lines of code sufficient for that purpose? Do I need to create any virtual environment?

#'  global reference to ctef
#'
#'@description
#'`ctef` will be initialized in .onLoad.
#'
ctef <- NULL

#' Delay load ctef module
#'
#' @description
#' `.onLoad` delays loading ctef module (will only be loaded when accessed via $).
#'
#' @param libname Library name
#' @param pkgname Package name
.onLoad <- function(libname, pkgname) {
    ctef <<- reticulate::import("ctef", delay_load = TRUE)

}

c. How shall I import the module in my R code? For now I included the import function in my_function_that_depends_on_ctef; see below:
my_function_that_depends_on_ctef <- function(X, k) {
 mod <- reticulate::import('ctef',delay_load = TRUE)
  input <- as.matrix(X)
  res <- mod$ctef$ctef(input,as.integer(k))
return(res)
}

Is this correct?

There are not many R packages that depend on a Python module, so the resources are quite limited. Thank you for your help.


Best,
Hanyu Song


	[[alternative HTML version deleted]]



More information about the R-package-devel mailing list