-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
參考資料
- R Interface to Python
- Python setup in R with Reticulate
- keras, kerasR, reticulate & Tensorflow in R-Studio with Python from Anaconda
- How to Set Up Python's Scikit-Learn in R in 5 minutes
安裝 Anaconda
brew install --cask anaconda3
安裝 R 與 RStudio
brew install r或brew install --cask r或brew install -s r(Apple Silicon M1)brew install --cask rstudio
執行 R 之後安裝 reticulate Package
> install.packages("tidyverse")
> install.packages("reticulate")
> library("reticulate")
> options(reticulate.conda_binary = "/usr/local/anaconda3/bin/conda")
或
> options(reticulate.conda_binary = "/opt/homebrew/Caskroom/miniforge/base/envs/ai/bin/conda")建立 Conda Environment
執行 py_config() 指令,預設會建立一個 r-reticulate Conda Environment:
> py_config()
Collecting package metadata (current_repodata.json): ...working... done
Solving environment: ...working... done
## Package Plan ##
environment location: /usr/local/anaconda3/envs/r-reticulate
added / updated specs:
- numpy
- python=3.6
The following packages will be downloaded:
package | build
---------------------------|-----------------
certifi-2020.12.5 | py36h79c6626_1 143 KB conda-forge
libblas-3.9.0 | 8_openblas 11 KB conda-forge
libcblas-3.9.0 | 8_openblas 11 KB conda-forge
libcxx-11.1.0 | habf9029_0 1.0 MB conda-forge
libffi-3.3 | h046ec9c_2 45 KB conda-forge
libgfortran-5.0.0 |9_3_0_h6c81a4c_20 19 KB conda-forge
libgfortran5-9.3.0 | h6c81a4c_20 1.7 MB conda-forge
liblapack-3.9.0 | 8_openblas 11 KB conda-forge
libopenblas-0.3.12 |openmp_h54245bb_1 8.8 MB conda-forge
llvm-openmp-11.1.0 | hda6cdc1_0 268 KB conda-forge
ncurses-6.2 | h2e338ed_4 881 KB conda-forge
numpy-1.19.5 | py36h08dc641_1 5.0 MB conda-forge
pip-21.0.1 | pyhd8ed1ab_0 1.1 MB conda-forge
python-3.6.13 |h7728216_0_cpython 20.6 MB conda-forge
python_abi-3.6 | 1_cp36m 4 KB conda-forge
readline-8.1 | h05e3726_0 266 KB conda-forge
setuptools-49.6.0 | py36h79c6626_3 916 KB conda-forge
sqlite-3.35.2 | h44b9ce1_0 1.7 MB conda-forge
tk-8.6.10 | h0419947_1 3.3 MB conda-forge
wheel-0.36.2 | pyhd3deb0d_0 31 KB conda-forge
xz-5.2.5 | haf1e3a3_1 228 KB conda-forge
zlib-1.2.11 | h7795811_1010 102 KB conda-forge
------------------------------------------------------------
Total: 46.0 MB
The following NEW packages will be INSTALLED:
ca-certificates conda-forge/osx-64::ca-certificates-2020.12.5-h033912b_0
certifi conda-forge/osx-64::certifi-2020.12.5-py36h79c6626_1
libblas conda-forge/osx-64::libblas-3.9.0-8_openblas
libcblas conda-forge/osx-64::libcblas-3.9.0-8_openblas
libcxx conda-forge/osx-64::libcxx-11.1.0-habf9029_0
libffi conda-forge/osx-64::libffi-3.3-h046ec9c_2
libgfortran conda-forge/osx-64::libgfortran-5.0.0-9_3_0_h6c81a4c_20
libgfortran5 conda-forge/osx-64::libgfortran5-9.3.0-h6c81a4c_20
liblapack conda-forge/osx-64::liblapack-3.9.0-8_openblas
libopenblas conda-forge/osx-64::libopenblas-0.3.12-openmp_h54245bb_1
llvm-openmp conda-forge/osx-64::llvm-openmp-11.1.0-hda6cdc1_0
ncurses conda-forge/osx-64::ncurses-6.2-h2e338ed_4
numpy conda-forge/osx-64::numpy-1.19.5-py36h08dc641_1
openssl conda-forge/osx-64::openssl-1.1.1j-hbcf498f_0
pip conda-forge/noarch::pip-21.0.1-pyhd8ed1ab_0
python conda-forge/osx-64::python-3.6.13-h7728216_0_cpython
python_abi conda-forge/osx-64::python_abi-3.6-1_cp36m
readline conda-forge/osx-64::readline-8.1-h05e3726_0
setuptools conda-forge/osx-64::setuptools-49.6.0-py36h79c6626_3
sqlite conda-forge/osx-64::sqlite-3.35.2-h44b9ce1_0
tk conda-forge/osx-64::tk-8.6.10-h0419947_1
wheel conda-forge/noarch::wheel-0.36.2-pyhd3deb0d_0
xz conda-forge/osx-64::xz-5.2.5-haf1e3a3_1
zlib conda-forge/osx-64::zlib-1.2.11-h7795811_1010
Downloading and Extracting Packages
python_abi-3.6 | 4 KB | ########## | 100%
ncurses-6.2 | 881 KB | ########## | 100%
sqlite-3.35.2 | 1.7 MB | ########## | 100%
numpy-1.19.5 | 5.0 MB | ########## | 100%
libopenblas-0.3.12 | 8.8 MB | ########## | 100%
wheel-0.36.2 | 31 KB | ########## | 100%
python-3.6.13 | 20.6 MB | ########## | 100%
xz-5.2.5 | 228 KB | ########## | 100%
readline-8.1 | 266 KB | ########## | 100%
libcxx-11.1.0 | 1.0 MB | ########## | 100%
libblas-3.9.0 | 11 KB | ########## | 100%
certifi-2020.12.5 | 143 KB | ########## | 100%
zlib-1.2.11 | 102 KB | ########## | 100%
tk-8.6.10 | 3.3 MB | ########## | 100%
libcblas-3.9.0 | 11 KB | ########## | 100%
pip-21.0.1 | 1.1 MB | ########## | 100%
llvm-openmp-11.1.0 | 268 KB | ########## | 100%
libgfortran-5.0.0 | 19 KB | ########## | 100%
libffi-3.3 | 45 KB | ########## | 100%
liblapack-3.9.0 | 11 KB | ########## | 100%
libgfortran5-9.3.0 | 1.7 MB | ########## | 100%
setuptools-49.6.0 | 916 KB | ########## | 100%
Preparing transaction: ...working... done
Verifying transaction: ...working... done
Executing transaction: ...working... done
#
# To activate this environment, use
#
# $ conda activate r-reticulate
#
# To deactivate an active environment, use
#
# $ conda deactivate
python: /usr/local/anaconda3/bin/python3
libpython: /usr/local/anaconda3/lib/libpython3.8.dylib
pythonhome: /usr/local/anaconda3:/usr/local/anaconda3
version: 3.8.8 (default, Feb 24 2021, 13:46:16) [Clang 10.0.0 ]
numpy: /usr/local/anaconda3/lib/python3.8/site-packages/numpy
numpy_version: 1.19.2
python versions found:
/usr/local/anaconda3/bin/python3
/usr/bin/python3
/usr/local/bin/python3
/usr/bin/python
> conda_list()
name python
1 r-reticulate /usr/local/anaconda3/envs/r-reticulate/bin/python
重新執行 R 並且載入 reticulate Package
之後每次都要執行的步驟:
> library("reticulate")
> options(reticulate.conda_binary = "/usr/local/anaconda3/bin/conda")
> conda_list()
name python
1 r-reticulate /usr/local/anaconda3/envs/r-reticulate/bin/python
> use_condaenv("r-reticulate")
> py_config()
python: /usr/local/anaconda3/envs/r-reticulate/bin/python
libpython: /usr/local/anaconda3/envs/r-reticulate/lib/libpython3.6m.dylib
pythonhome: /usr/local/anaconda3/envs/r-reticulate:/usr/local/anaconda3/envs/r-reticulate
version: 3.6.13 | packaged by conda-forge | (default, Feb 19 2021, 05:36:16) [GCC Clang 11.0.1]
numpy: /usr/local/anaconda3/envs/r-reticulate/lib/python3.6/site-packages/numpy
numpy_version: 1.19.5
python versions found:
/usr/local/anaconda3/envs/r-reticulate/bin/python
/usr/bin/python3
/usr/local/bin/python3
/usr/bin/python
> Sys.which("python")
python
"/usr/local/anaconda3/envs/r-reticulate/bin/python"
直接執行 Python 指令
> repl_python()
Python 3.6.13 (/usr/local/anaconda3/envs/r-reticulate/bin/python)
Reticulate 1.18 REPL -- A Python interpreter in R.
>>> print("Hello, Python!")
Hello, Python!
>>> exit
>
安裝 Python Package
安裝到預設的 r-reticulate Conda Environment:
> py_install("pandas")
Collecting package metadata (current_repodata.json): ...working... done
Solving environment: ...working... done
## Package Plan ##
environment location: /usr/local/anaconda3/envs/r-reticulate
added / updated specs:
- pandas
- python
The following packages will be downloaded:
package | build
---------------------------|-----------------
pandas-1.1.5 | py36h2be6da3_0 10.0 MB conda-forge
python-dateutil-2.8.1 | py_0 220 KB conda-forge
pytz-2021.1 | pyhd8ed1ab_0 239 KB conda-forge
six-1.15.0 | pyh9f0ad1d_0 14 KB conda-forge
------------------------------------------------------------
Total: 10.5 MB
The following NEW packages will be INSTALLED:
pandas conda-forge/osx-64::pandas-1.1.5-py36h2be6da3_0
python-dateutil conda-forge/noarch::python-dateutil-2.8.1-py_0
pytz conda-forge/noarch::pytz-2021.1-pyhd8ed1ab_0
six conda-forge/noarch::six-1.15.0-pyh9f0ad1d_0
Downloading and Extracting Packages
python-dateutil-2.8. | 220 KB | ########## | 100%
pytz-2021.1 | 239 KB | ########## | 100%
six-1.15.0 | 14 KB | ########## | 100%
pandas-1.1.5 | 10.0 MB | ########## | 100%
Preparing transaction: ...working... done
Verifying transaction: ...working... done
Executing transaction: ...working... done
安裝到指定的 Conda Environment:
> conda_install(envname = "r-reticulate", packages = "plotnine")
Collecting package metadata (current_repodata.json): ...working... done
Solving environment: ...working... done
## Package Plan ##
environment location: /usr/local/anaconda3/envs/r-reticulate
added / updated specs:
- plotnine
- python
The following packages will be downloaded:
package | build
---------------------------|-----------------
cycler-0.10.0 | py_2 9 KB conda-forge
descartes-1.1.0 | py_4 7 KB conda-forge
freetype-2.10.4 | h4cff582_1 890 KB conda-forge
jpeg-9d | hbcb3906_0 250 KB conda-forge
kiwisolver-1.3.1 | py36h615c93b_1 59 KB conda-forge
lcms2-2.12 | h577c468_0 404 KB conda-forge
libpng-1.6.37 | h7cec526_2 313 KB conda-forge
libtiff-4.2.0 | h355d032_0 639 KB conda-forge
libwebp-base-1.2.0 | h0d85af4_2 700 KB conda-forge
lz4-c-1.9.3 | h046ec9c_0 155 KB conda-forge
matplotlib-base-3.3.4 | py36h4ea959b_0 6.8 MB conda-forge
mizani-0.7.2 | pyhd8ed1ab_0 53 KB conda-forge
olefile-0.46 | pyh9f0ad1d_1 32 KB conda-forge
palettable-3.3.0 | py_0 86 KB conda-forge
patsy-0.5.1 | py_0 187 KB conda-forge
pillow-8.1.2 | py36h154fef6_0 642 KB conda-forge
plotnine-0.7.1 | py_0 3.6 MB conda-forge
pyparsing-2.4.7 | pyh9f0ad1d_0 60 KB conda-forge
scipy-1.5.3 | py36h04de62b_0 17.1 MB conda-forge
statsmodels-0.12.2 | py36hf14bfbd_0 10.6 MB conda-forge
tornado-6.1 | py36h20b66c6_1 643 KB conda-forge
zstd-1.4.9 | h582d3a0_0 460 KB conda-forge
------------------------------------------------------------
Total: 43.5 MB
The following NEW packages will be INSTALLED:
cycler conda-forge/noarch::cycler-0.10.0-py_2
descartes conda-forge/noarch::descartes-1.1.0-py_4
freetype conda-forge/osx-64::freetype-2.10.4-h4cff582_1
jpeg conda-forge/osx-64::jpeg-9d-hbcb3906_0
kiwisolver conda-forge/osx-64::kiwisolver-1.3.1-py36h615c93b_1
lcms2 conda-forge/osx-64::lcms2-2.12-h577c468_0
libpng conda-forge/osx-64::libpng-1.6.37-h7cec526_2
libtiff conda-forge/osx-64::libtiff-4.2.0-h355d032_0
libwebp-base conda-forge/osx-64::libwebp-base-1.2.0-h0d85af4_2
lz4-c conda-forge/osx-64::lz4-c-1.9.3-h046ec9c_0
matplotlib-base conda-forge/osx-64::matplotlib-base-3.3.4-py36h4ea959b_0
mizani conda-forge/noarch::mizani-0.7.2-pyhd8ed1ab_0
olefile conda-forge/noarch::olefile-0.46-pyh9f0ad1d_1
palettable conda-forge/noarch::palettable-3.3.0-py_0
patsy conda-forge/noarch::patsy-0.5.1-py_0
pillow conda-forge/osx-64::pillow-8.1.2-py36h154fef6_0
plotnine conda-forge/noarch::plotnine-0.7.1-py_0
pyparsing conda-forge/noarch::pyparsing-2.4.7-pyh9f0ad1d_0
scipy conda-forge/osx-64::scipy-1.5.3-py36h04de62b_0
statsmodels conda-forge/osx-64::statsmodels-0.12.2-py36hf14bfbd_0
tornado conda-forge/osx-64::tornado-6.1-py36h20b66c6_1
zstd conda-forge/osx-64::zstd-1.4.9-h582d3a0_0
Downloading and Extracting Packages
plotnine-0.7.1 | 3.6 MB | ########## | 100%
pillow-8.1.2 | 642 KB | ########## | 100%
patsy-0.5.1 | 187 KB | ########## | 100%
libpng-1.6.37 | 313 KB | ########## | 100%
freetype-2.10.4 | 890 KB | ########## | 100%
cycler-0.10.0 | 9 KB | ########## | 100%
libwebp-base-1.2.0 | 700 KB | ########## | 100%
kiwisolver-1.3.1 | 59 KB | ########## | 100%
pyparsing-2.4.7 | 60 KB | ########## | 100%
lcms2-2.12 | 404 KB | ########## | 100%
palettable-3.3.0 | 86 KB | ########## | 100%
scipy-1.5.3 | 17.1 MB | ########## | 100%
lz4-c-1.9.3 | 155 KB | ########## | 100%
mizani-0.7.2 | 53 KB | ########## | 100%
olefile-0.46 | 32 KB | ########## | 100%
libtiff-4.2.0 | 639 KB | ########## | 100%
tornado-6.1 | 643 KB | ########## | 100%
matplotlib-base-3.3. | 6.8 MB | ########## | 100%
statsmodels-0.12.2 | 10.6 MB | ########## | 100%
zstd-1.4.9 | 460 KB | ########## | 100%
descartes-1.1.0 | 7 KB | ########## | 100%
jpeg-9d | 250 KB | ########## | 100%
Preparing transaction: ...working... done
Verifying transaction: ...working... done
Executing transaction: ...working... done
重建 r-reticulate Conda Environment
因為 reticulate Package 預設建立的 Conda Environment 使用的是 3.6 版本的 Python,所以可以考慮砍掉重建,但是記得要有 NumPy Package,不然 reticulate Package:
% conda env remove --name r-reticulate
Remove all packages in environment /usr/local/anaconda3/envs/r-reticulate:
% conda create --name r-reticulate python=3.8 conda pip numpy
Collecting package metadata (current_repodata.json): done
Solving environment: done
## Package Plan ##
environment location: /usr/local/anaconda3/envs/r-reticulate
added / updated specs:
- conda
- pip
- python=3.8
The following packages will be downloaded:
package | build
---------------------------|-----------------
ruamel_yaml-0.15.100 | py38h9ed2024_0 247 KB
sqlite-3.35.2 | hce871da_0 1.1 MB
------------------------------------------------------------
Total: 1.4 MB
The following NEW packages will be INSTALLED:
brotlipy pkgs/main/osx-64::brotlipy-0.7.0-py38h9ed2024_1003
ca-certificates pkgs/main/osx-64::ca-certificates-2021.1.19-hecd8cb5_1
certifi pkgs/main/osx-64::certifi-2020.12.5-py38hecd8cb5_0
cffi pkgs/main/osx-64::cffi-1.14.5-py38h2125817_0
chardet pkgs/main/osx-64::chardet-4.0.0-py38hecd8cb5_1003
conda pkgs/main/osx-64::conda-4.9.2-py38hecd8cb5_0
conda-package-han~ pkgs/main/osx-64::conda-package-handling-1.7.2-py38h22f3db7_0
cryptography pkgs/main/osx-64::cryptography-3.4.6-py38h2fd3fbb_0
idna pkgs/main/noarch::idna-2.10-pyhd3eb1b0_0
libcxx pkgs/main/osx-64::libcxx-10.0.0-1
libffi pkgs/main/osx-64::libffi-3.3-hb1e8313_2
ncurses pkgs/main/osx-64::ncurses-6.2-h0a44026_1
openssl pkgs/main/osx-64::openssl-1.1.1j-h9ed2024_0
pip pkgs/main/osx-64::pip-21.0.1-py38hecd8cb5_0
pycosat pkgs/main/osx-64::pycosat-0.6.3-py38h1de35cc_1
pycparser pkgs/main/noarch::pycparser-2.20-py_2
pyopenssl pkgs/main/noarch::pyopenssl-20.0.1-pyhd3eb1b0_1
pysocks pkgs/main/osx-64::pysocks-1.7.1-py38_1
python pkgs/main/osx-64::python-3.8.8-h88f2d9e_4
readline pkgs/main/osx-64::readline-8.1-h9ed2024_0
requests pkgs/main/noarch::requests-2.25.1-pyhd3eb1b0_0
ruamel_yaml pkgs/main/osx-64::ruamel_yaml-0.15.100-py38h9ed2024_0
setuptools pkgs/main/osx-64::setuptools-52.0.0-py38hecd8cb5_0
six pkgs/main/osx-64::six-1.15.0-py38hecd8cb5_0
sqlite pkgs/main/osx-64::sqlite-3.35.2-hce871da_0
tk pkgs/main/osx-64::tk-8.6.10-hb0a8c7a_0
tqdm pkgs/main/noarch::tqdm-4.59.0-pyhd3eb1b0_1
urllib3 pkgs/main/noarch::urllib3-1.26.4-pyhd3eb1b0_0
wheel pkgs/main/noarch::wheel-0.36.2-pyhd3eb1b0_0
xz pkgs/main/osx-64::xz-5.2.5-h1de35cc_0
yaml pkgs/main/osx-64::yaml-0.2.5-haf1e3a3_0
zlib pkgs/main/osx-64::zlib-1.2.11-h1de35cc_3
Proceed ([y]/n)?
Downloading and Extracting Packages
sqlite-3.35.2 | 1.1 MB | ################################################################################################################## | 100%
ruamel_yaml-0.15.100 | 247 KB | ################################################################################################################## | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
# $ conda activate r-reticulate
#
# To deactivate an active environment, use
#
# $ conda deactivate
Metadata
Metadata
Assignees
Labels
No labels