Search code examples
pythonrcronschedulereticulate

Can't complete Cron Job when python code is embed (through reticulate) inside the R script


I've created an R script that first launches a python script through the py_run_file() function and then executes some R code. I need to schedule the launch of such script at a certain hour of the day, and hence I used the package cronR. Unfortunately, when I call the script function just as a cron job, the job freezes and doesn't end. Here's a reproducible example:

library(reticulate)

today <- Sys.Date()

use_python("~/miniconda3/bin/python3.8",
           required = TRUE)

py_run_file("~/Desktop/test.py")

print(paste("End on the",
            today))

This is the content of the test.py file:

print("python script!")

Do you have any suggestion? Thanks a lot for your help!


Solution

  • for some odd reason, whenever you run an R Cron Job using reticulate package, I figured after a lot of trial and error, we have to use it like this :

    reticulate::use_python(python = '/usr/bin/python3', required = T)
    
    reticulate::source_python("FULL_PATH_TO_PYTHON_FILE.py")
    

    instead of importing the library using library(reticulate) this thing strangely works!