8

I have not found some R package ( there is no one, trust me) for my tasks, but there is one in python. So i wrote python script and used reticulaye::py_run_file('my_script.py') in some functions. But after building and installation, package can't find that script. Where should i put this script in order to use it after installation directly from package. And another thing, i need to install miniconda reticulate::install_miniconda(). Does anyone know the way to install it automatically after install.package command ?

1 Answers1

14

Typically non-R code goes in ./inst/python/your_script.py (likewise for JS, etc). Anything in the inst folder will be installed into your package's root directory unchanged.

To call these files in your package functions, use something like:

reticulate::py_run_file(system.file("python", "your_script.py", package = "yourpkg"))

See: http://r-pkgs.had.co.nz/inst.html


For your second question, you should prompt the user before installing anything, but you would usually call any external installers in a special function called .onLoad with arguments libname and pkgname. This is a function that is automatically executed when you call library(yourpkg).

.onLoad <- function(libname, pkgname) {
  user_permission <- utils::askYesNo("Install miniconda? downloads 50MB and takes time")

  if (isTRUE(user_permission)) {
    reticulate::install_miniconda()
    } else {
    message("You should run `reticulate::install_miniconda()` before using this package")
   }
}

You can put this function in any of your package R files.

Brian
  • 7,900
  • 1
  • 27
  • 41
  • 3
    If that may help, in my case, if the script goes in `./inst/python/your_script.py`, then to call it, I had to explicitly include the python path, so: ```reticulate::py_run_file(system.file("python/your_script.py", package = "yourpkg"))``` – khyox Dec 02 '21 at 00:28