Search code examples
pythonrdevtoolsreticulate

Attaching python script while building r package


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 ?


Solution

  • 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.