python in RStudio
R에서 miniconda설치 후(https://onesixx.com/reticulate/),
R스크립트에서 관련 Library 불러오고, 특정 conda 가상환경및 python 위치를 지정하여 준비한다.
이후, OOO.py 로 파일을 만들면, RStudio에서 python코드를 실행할 수 있다.
myENV='sixxDL' library(keras) library(reticulate) # install_keras(method="conda", tensorflow="gpu") ### create new myENV by cloning # ~$➜ conda create --clone r-reticulate --name sixxDL Sys.setenv(RETICULATE_PYTHON_ENV=myENV) Sys.setenv(RETICULATE_PYTHON=file.path("~/Library/r-miniconda/envs",myENV,"bin/python")) use_condaenv(condaenv=myENV, required=T) if (Sys.info()["sysname"]=="Darwin"){ \tuse_backend(backend=c("plaidml")) } else { \tuse_backend(backend="tensorflow") } keras::k_backend() #system("plaidbench keras mobilenet") #py_config() # py_install("pandas") # py_install("matplotlib") # py_install("scikit-learn") #.rs.restartR() # cmd+shift+F10
#repl_python() from pandas import read_csv from matplotlib import pyplot # load dataset dataset = read_csv('pollution.csv', header=0, index_col=0) values = dataset.values # specify columns to plot groups = [0, 1, 2, 3, 5, 6, 7] i = 1 # plot each column pyplot.figure() for group in groups: \tpyplot.subplot(len(groups), 1, i) \tpyplot.plot(values[:, group]) \tpyplot.title(dataset.columns[group], y=0.5, loc='right') \ti += 1 pyplot.show()
> reticulate::repl_python() Python 3.6.10 (/home/onesixx/.local/share/r-miniconda/envs/sixxDL/bin/python) Reticulate 1.16 REPL -- A Python interpreter in R. >>> from pandas import read_csv >>> from matplotlib import pyplot >>> dataset = read_csv('pollution.csv', header=0, index_col=0) >>> dataset No pollution dew temp ... wnd_spd snow rain daytime 1 25 129 -16 -4.0 ... 1.79 0 0 2010-01-02 00:00:00 2 26 148 -15 -4.0 ... 2.68 0 0 2010-01-02 01:00:00 3 27 159 -11 -5.0 ... 3.57 0 0 2010-01-02 02:00:00 4 28 181 -7 -5.0 ... 5.36 1 0 2010-01-02 03:00:00 5 29 138 -7 -5.0 ... 6.25 2 0 2010-01-02 04:00:00 ... ... ... ... ... ... ... ... ... ... 43796 43820 8 -23 -2.0 ... 231.97 0 0 2014-12-31 19:00:00 43797 43821 10 -22 -3.0 ... 237.78 0 0 2014-12-31 20:00:00 43798 43822 10 -22 -3.0 ... 242.70 0 0 2014-12-31 21:00:00 43799 43823 8 -22 -4.0 ... 246.72 0 0 2014-12-31 22:00:00 43800 43824 12 -21 -3.0 ... 249.85 0 0 2014-12-31 23:00:00
특정 가상환경에 추가적인 패키지를 설치하려고 할때,
RStudio내에 Terminal이 안되는 경우가 있으며, 기존의 사용하던 Terminal을 이용하면 문제없이 설치된다.
(base) onesixx@cf1d8cf6e830:/usr/lib/R/etc$ conda activate sixxDL (sixxDL) onesixx@cf1d8cf6e830:/usr/lib/R/etc$ conda install pandas (sixxDL) onesixx@cf1d8cf6e830:/usr/lib/R/etc$ conda install matplotlib (sixxDL) onesixx@cf1d8cf6e830:/usr/lib/R/etc$ conda install scikit-learn
(base) onesixx@cf1d8cf6e830:/usr/lib/R/etc$ conda info active environment : base active env location : /home/onesixx/.local/share/r-miniconda shell level : 1 user config file : /home/onesixx/.condarc populated config files : conda version : 4.8.3 conda-build version : not installed python version : 3.8.3.final.0 virtual packages : __cuda=10.1 __glibc=2.27 base environment : /home/onesixx/.local/share/r-miniconda (writable) channel URLs : https://repo.anaconda.com/pkgs/main/linux-64 https://repo.anaconda.com/pkgs/main/noarch https://repo.anaconda.com/pkgs/r/linux-64 https://repo.anaconda.com/pkgs/r/noarch package cache : /home/onesixx/.local/share/r-miniconda/pkgs /home/onesixx/.conda/pkgs envs directories : /home/onesixx/.local/share/r-miniconda/envs /home/onesixx/.conda/envs platform : linux-64 user-agent : conda/4.8.3 requests/2.24.0 CPython/3.8.3 Linux/3.10.0-1062.18.1.el7.x86_64 ubuntu/18.04.4 glibc/2.27 UID:GID : 1001:1001 netrc file : None offline mode : False (base) onesixx@cf1d8cf6e830:/usr/lib/R/etc$ conda env list # conda environments: # base * /home/onesixx/.local/share/r-miniconda r-reticulate /home/onesixx/.local/share/r-miniconda/envs/r-reticulate sixxDL /home/onesixx/.local/share/r-miniconda/envs/sixxDL (base) onesixx@cf1d8cf6e830:/usr/lib/R/etc$ conda activate sixxDL (sixxDL) onesixx@cf1d8cf6e830:/usr/lib/R/etc$ conda list # packages in environment at /home/onesixx/.local/share/r-miniconda/envs/sixxDL: # # Name Version Build Channel _libgcc_mutex 0.1 conda_forge conda-forge _openmp_mutex 4.5 1_gnu conda-forge absl-py 0.9.0 pypi_0 pypi astunparse 1.6.3 pypi_0 pypi ca-certificates 2020.6.24 0 cachetools 4.1.1 pypi_0 pypi certifi 2020.6.20 py36_0 chardet 3.0.4 pypi_0 pypi gast 0.3.3 pypi_0 pypi google-auth 1.20.0 pypi_0 pypi google-auth-oauthlib 0.4.1 pypi_0 pypi google-pasta 0.2.0 pypi_0 pypi grpcio 1.31.0 pypi_0 pypi h5py 2.10.0 pypi_0 pypi idna 2.10 pypi_0 pypi importlib-metadata 1.7.0 pypi_0 pypi keras 2.4.3 pypi_0 pypi keras-preprocessing 1.1.2 pypi_0 pypi ld_impl_linux-64 2.34 hc38a660_9 conda-forge libffi 3.2.1 he1b5a44_1007 conda-forge libgcc-ng 9.3.0 h24d8f2e_14 conda-forge libgomp 9.3.0 h24d8f2e_14 conda-forge libstdcxx-ng 9.3.0 hdf63c60_14 conda-forge markdown 3.2.2 pypi_0 pypi ncurses 6.2 he1b5a44_1 conda-forge numpy 1.18.5 pypi_0 pypi oauthlib 3.1.0 pypi_0 pypi openssl 1.1.1g h7b6447c_0 opt-einsum 3.3.0 pypi_0 pypi pillow 7.2.0 pypi_0 pypi pip 20.2.1 py_0 conda-forge protobuf 3.12.4 pypi_0 pypi pyasn1 0.4.8 pypi_0 pypi pyasn1-modules 0.2.8 pypi_0 pypi python 3.6.10 hcf32534_1 pyyaml 3.12 pypi_0 pypi readline 8.0 he28a2e2_2 conda-forge requests 2.24.0 pypi_0 pypi requests-oauthlib 1.3.0 pypi_0 pypi rsa 4.6 pypi_0 pypi scipy 1.4.1 pypi_0 pypi setuptools 49.2.0 py36_0 six 1.15.0 pypi_0 pypi sqlite 3.32.3 hcee41ef_1 conda-forge tensorboard 2.3.0 pypi_0 pypi tensorboard-plugin-wit 1.7.0 pypi_0 pypi tensorflow 2.3.0 pypi_0 pypi tensorflow-estimator 2.3.0 pypi_0 pypi tensorflow-gpu 2.2.0 pypi_0 pypi tensorflow-hub 0.8.0 pypi_0 pypi termcolor 1.1.0 pypi_0 pypi tk 8.6.10 hed695b0_0 conda-forge urllib3 1.25.10 pypi_0 pypi werkzeug 1.0.1 pypi_0 pypi wheel 0.34.2 py_1 conda-forge wrapt 1.12.1 pypi_0 pypi xz 5.2.5 h516909a_1 conda-forge zipp 3.1.0 pypi_0 pypi zlib 1.2.11 h516909a_1006 conda-forge (sixxDL) onesixx@cf1d8cf6e830:/usr/lib/R/etc$ conda install pandas (sixxDL) onesixx@cf1d8cf6e830:/usr/lib/R/etc$ conda install matplotlib