Attaching and linking non R files (i.e. scripts from other languages like Python scripts) while building R packages

What are the options to build an R package that links non R files (besides C/C++/Fortran files)?

There are plenty of examples for these, e.g. you can search in the cran organization on GitHub: https://github.com/search?q=org%3Acran%20language%3APython&type=code

In general you can put any Python file into /inst and then call it either via the reticulate package, or directly with an external Python interpreter.

2 Likes

If you're using reticulate as the bridge between Python and R, you can place your Python files under inst/python/my_pkg_tools/, and then use reticulate::import_from_path(). E.g., in keras3 we have a handful of python files under inst/python/kerastools/*.py, and then we access them from R like this:

import_kerastools <- function (submodules = NULL) {
  module <- paste0(c("kerastools", submodules), collapse = ".")
  path <- system.file("python", package = "keras3")
  reticulate::import_from_path(module, path = path)
}
1 Like

This topic was automatically closed after 45 days. New replies are no longer allowed.


If you have a query related to it or one of the replies, start a new topic and refer back with a link.