1 Reticulate Basics

Go to the RMD, R, PDF, or HTML version of this file. Go back to fan’s REconTools Package, R Code Examples Repository (bookdown site), or Intro Stats with R Repository (bookdown site).

1.1 Basic Python Tests with RMD

Could specify: python, engine.path = “C:/ProgramData/Anaconda3/envs/wk_pyfan/python.exe”, this is already set inside Rprofile: knitr::opts_chunk$set(engine.path = “C:/ProgramData/Anaconda3/envs/wk_pyfan/python.exe”)

1+1
## 2

1.2 Install and Python Path

Install reticulate from github directly to get latest version: devtools::install_github(“rstudio/reticulate”)

Check python version on computer:

Sys.which('python')
##                                               python 
## "C:\\PROGRA~3\\ANACON~1\\envs\\wk_pyfan\\python.exe"

After installing reticulate, load in the library: library(reticulate). With “py_config()” to see python config. First time, might generate “No non-system installation of Python could be found.” and ask if want to install Miniconda. Answer NO.

Correct outputs upon checking py_config():

python:         C:/ProgramData/Anaconda3/python.exe
libpython:      C:/ProgramData/Anaconda3/python37.dll
pythonhome:     C:/ProgramData/Anaconda3
version:        3.7.9 (default, Aug 31 2020, 17:10:11) [MSC v.1916 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:/ProgramData/Anaconda3/Lib/site-packages/numpy
numpy_version:  1.19.1

python versions found:
 C:/ProgramData/Anaconda3/python.exe
 C:/ProgramData/Anaconda3/envs/wk_cgefi/python.exe
 C:/ProgramData/Anaconda3/envs/wk_jinja/python.exe
 C:/ProgramData/Anaconda3/envs/wk_pyfan/python.exe

Set which python to use:

# Sys.setenv(RETICULATE_PYTHON = "C:/programdata/Anaconda3/python.exe")
# Sys.setenv(RETICULATE_PYTHON = "C:/ProgramData/Anaconda3/envs/wk_pyfan/python.exe")
library(reticulate)
# What is the python config
py_config()
## python:         C:/ProgramData/Anaconda3/envs/wk_pyfan/python.exe
## libpython:      C:/ProgramData/Anaconda3/envs/wk_pyfan/python38.dll
## pythonhome:     C:/ProgramData/Anaconda3/envs/wk_pyfan
## version:        3.8.5 (default, Sep  3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)]
## Architecture:   64bit
## numpy:          C:/ProgramData/Anaconda3/envs/wk_pyfan/Lib/site-packages/numpy
## numpy_version:  1.19.1
## 
## NOTE: Python version was forced by use_python function
# set python
# use_python("C:/programdata/Anaconda3/python.exe")
# use_python("C:/ProgramData/Anaconda3/envs/wk_pyfan/python.exe")
use_condaenv('wk_pyfan')
# Sys.which('python')
py_run_string('print(1+1)')

1.3 Error

1.3.1 py_call_impl error

The error appeared when calling any python operations, including “1+1”, resolved after installing reticulate from github: devtools::install_github(“rstudio/reticulate”)

Error in py_call_impl(callable, dots$args, dots$keywords) :
  TypeError: use() got an unexpected keyword argument 'warn'