Reticulate: Use own Conda environment on an R package

Created on 6 Jul 2020  路  5Comments  路  Source: rstudio/reticulate

Hi, I'm developing an R package (deepredeff) that uses the keras and tensorflow R packages. I've followed the Managing an R Package鈥檚 Python Dependencies to set the Config/reticulate: section on the DESCRIPTION file as well as the .onLoad() function.

Issue

The package works as expected if the user uses the autoconfiguration from reticulate::configure_environment() (i.e., using the r-reticulate environment on Miniconda). However, I would also give the option to use an existing TensorFlow installation if the user already has it installed on their system.

The problem is that I get the following error when using reticulate::use_condaenv():

library(deepredeff)
reticulate::use_condaenv("tensorflow", conda = "/usr/local/Caskroom/miniconda/base/bin/conda")
reticulate::conda_binary()
#> [1] "/usr/local/Caskroom/miniconda/base/bin/conda"
reticulate::repl_python()
#> Error: Specified conda binary '/Users/ruth/Library/r-miniconda/bin/conda' does not exist.

If I run repl_python() again (on the same session), somehow it's able to locate the Conda environment specified earlier:

reticulate::repl_python()
#> Python 3.7.5 (/usr/local/Caskroom/miniconda/base/envs/tensorflow/bin/python)
#> Reticulate 1.16 REPL -- A Python interpreter in R.
#> exit

Note

This issue only happens if library(deepredeff) is loaded at the beginning. Without, reticulate never tries to load the r-reticulate environment.

More details

Actually, I found this issue when trying to run my predict_effector() function on my preexisting Conda installation. The first time it's run it gives an Error, and I if I run it again, it successfully runs on my specified Conda environment without issues.

pred_result <- deepredeff::predict_effector(
  input = bacteria_fasta_path,
  taxon = "bacteria"
)
># WARNING: incompatible requirements for package 'tensorflow' detected!
>#       source    package version   pip
># 1 deepredeff tensorflow   2.0.0 FALSE
># 2 tensorflow tensorflow      NA  TRUE
># WARNING: tensorflow [2.0.0] will be used.
># Error: 
pred_result <- deepredeff::predict_effector(
  input = bacteria_fasta_path,
  taxon = "bacteria"
)
># 2020-07-06 10:48:16.407641: I tensorflow/core/platform/cpu_feature_guard.cc:145] This TensorFlow binary is optimized with Intel(R) MKL-DNN to use the following CPU instructions in performance critical operations:  SSE4.1 SSE4.2 AVX AVX2 FMA
># To enable them in non-MKL-DNN operations, rebuild TensorFlow with the appropriate compiler flags.
># 2020-07-06 10:48:16.409637: I tensorflow/core/common_runtime/process_util.cc:115] Creating new thread pool with default inter op setting: 4. Tune using inter_op_parallelism_threads for best performance.
># Loaded models successfully!
># Model used for taxon bacteria: ensemble_weighted.

Questions

  • How can I set up my package so that users are able to use either r-reticulate or their own Conda/TensorFlow installation properly (by that I mean without getting an error at the first run).

    • Is there anything wrong or that I can change on my Config/reticulate: section or my .onLoad() function?

    • Is any of this actually possible?

Thank you very much.

Most helpful comment

Thanks for following up. I've reproduced this locally and I believe this should now be fixed in the development version of reticulate.

Can you try installing the development version of reticulate, with:

remotes::install_github("rstudio/reticulate")

and let me know if the issue appears to be resolved in your case?

All 5 comments

Thanks for the bug report!

This looks like a real bug, but does it make a difference if you use required = TRUE? That is:

reticulate::use_condaenv("tensorflow", conda = "/usr/local/Caskroom/miniconda/base/bin/conda", required = TRUE)

Hi @kevinushey, thank you very much for your reply. I have tried using required = TRUE in reticulate::use_condaenv() as you suggested both before and after loading my package. However, it still gave me the same error. Thank you very much.

Thanks for following up. I've reproduced this locally and I believe this should now be fixed in the development version of reticulate.

Can you try installing the development version of reticulate, with:

remotes::install_github("rstudio/reticulate")

and let me know if the issue appears to be resolved in your case?

Hi @kevinushey. I tried to install the development version of reticulate, and it solved the issue. It didn't give any error message while running my function. Also, if there is no environment specified, my package uses r-reticulate as expected. Thank you very much, Kevin!

Great! I'm glad to hear it. I'll try to get a new version of reticulate submitted to CRAN soon as well.

Was this page helpful?
0 / 5 - 0 ratings