Korean, Edit

Running Python in R (Based on Windows)

Recommended Article : 【RStudio】 R Studio Index **


1. Overview

2. step 1. Installing R

3. step 2. Installing Python and Setting up the Environment

4. step 3. Activating Python Environment in R

5. step 4. Calling Python



1. Overview

Significance 1. Combining pre-written R and Python code within R

Significance 2. Using Python packages in R

① TensorFlow, Keras, etc., were created for implementing artificial neural networks in Python

② Since 2017, TensorFlow and Keras versions that can run in R have been released, but there have been issues with complicated environment setup


install.packages("tensorflow")
library(tensorflow)
install_tensorflow()


③ Therefore, it is recommended to execute TensorFlow in Python by calling Python in R, rather than directly executing it in R



2. step 1. Installing R

1-1. Installing CRAN : Essential

① Visit https://cran.r-project.org/

② Download R for Windows

③ Install R for the first time

④ Click the top link

1-2. RStudio : Optional

① Installing CRAN is a prerequisite for using RStudio, which is just a UI tool to facilitate development

② RStudio is not essential but offers advantages like

○ Viewing contents of directories

○ Viewing declared variables and contents of objects

○ Freely adjusting the size of output plots and checking variable values intermittently

③ Installation available at https://www.rstudio.com/products/rstudio/

1-3. Installing anaconda (kernel : R) : Optional


activate [MY_ENVIRONMENT]
conda install -c conda-forge r-essentials=4.0 # the latest version



3. step 2. Setting up Python Environment



4. step 3. Activating Python Environment in R

3-1. Checking Python Environment Setup


conda activate [MY_ENV]
conda info --envs


image


① Anaconda supports downloading separate packages for each environment to manage different package versions

② Each environment is stored in C:/USERS/anaconda3/envs

3-2. Using Python Environment in RStudio or anaconda (kernel : R)

① Command


install.packages("reticulate")
library(reticulate)
use_condaenv(condaenv = 'lipo', required = TRUE)
py_config()


image


② Error message : If the environment name is entered incorrectly, the following error message is displayed


Error in use_condaenv(condaenv = "lipo_", required = TRUE) :
  Unable to locate conda environment 'lipo_'.



5. step 4. Calling Python


system("python PYTHON_CODE.py")


⑴ If 1 is output, it is normal, and if 2 is output, an error has occurred



Posted : 2021.06.20 01:40

Updated : 2022.10.15 15:01

results matching ""

    No results matching ""