최근 Pthon과 R 사이의 상호 운용을 위한 포괄적인 도구 모음인 reticulate 패키지가 발표되었습니다. 이 패키지는 다음 기능을 포함하고 있습니다.

reticulated python

  • R Markdown, Python 스크립트 소싱, Python 모듈 가져 오기, R 세션 내에서 대화식으로 파이썬 사용 등 다양한 방법으로 R에서 파이썬을 호출합니다,
  • R과 Pyrthon 객체 간 변환 (예 : R 및 Pandas 데이터 프레임 간 또는 R 행렬과 NumPy 배열 간 변환).
  • 가상 환경 및 Conda 환경을 포함하여 다른 버전의 Python에 유연하게 결합 할 수 있습니다.

reticulate는 R 세션 내에 Python 세션을 내장하여 원활한 고성능 상호 운용을 지원합니다. 데이터 부넛 작업 일부에 Python을 사용하는 R 개발자거나 두 언어를 사용하는 데이터 과학 팀원 인 경우를 망라하여 워크 플로우를 획기적으로 능률화 할 수 있습니다.

사용하기 위해서는 아래와 같이 설치하시기 바랍니다.

reticulate의 기능에 대한 자세한 내용을 보려면 계속 읽거나 패키지 사용에 대한 자세한 문서는 reticulate 웹사이트를 참조하십시오.

 

Python in R Markdown

reticulate 패키지에는R Markdo을 위한 Python 엔진이 포함되어 있습니다.

  • Run Python chunks in a single Python session embedded within your R session (shared variables/state between Python chunks)
  • Printing of Python output, including graphical output from matplotlib.
  • Access to objects created within Python chunks from R using the  py object (e.g.  py$x would access an  x variable created within Python from R).
  • Access to objects created within R chunks from Python using the  r object (e.g.  r.x would access to  x variable created within R from Python)

R 세션 내에 임베드 된 단일 Python 세션에서 Python 청크 실행 (Python 청크간에 변수 공유 / 상태 공유)

matplotlib의 그래픽 출력을 포함하여 Python 출력을 인쇄합니다.

Py 객체를 사용하여 R에서 Python 청크로 만든 객체에 액세스합니다 (예 : py $ x는 Python에서 R에서 만든 x 변수에 액세스합니다).

r 객체를 사용하여 Python에서 R 청크로 만든 객체에 액세스합니다 (예 : r.x는 Python에서 R 내에서 생성 된 x 변수에 액세스 함)

Built in conversion for many Python object types is provided, including NumPy arrays and Pandas data frames. From example, you can use Pandas to read and manipulate data then easily plot the Pandas data frame using ggplot2:

Note that the reticulate Python engine is enabled by default within R Markdown whenever reticulate is installed.

See the R Markdown Python Engine documentation for additional details.

Importing Python modules

You can use the 

import()

 function to import any Python module and call it from R. For example, this code imports the Python 

os

 module and calls the 

listdir()

 function:

Functions and other data within Python modules and classes can be accessed via the 

$

 operator (analogous to the way you would interact with an R list, environment, or reference class).

Imported Python modules support code completion and inline help:

See Calling Python from R for additional details on interacting with Python objects from within R.

Sourcing Python scripts

You can source any Python script just as you would source an R script using the 

source_python()

 function. For example, if you had the following Python script flights.py:

Then you can source the script and call the 

read_flights()

 function as follows:

See the 

source_python()

 documentation for additional details on sourcing Python code.

Python REPL

If you want to work with Python interactively you can call the 

repl_python()

 function, which provides a Python REPL embedded within your R session. Objects created within the Python REPL can be accessed from R using the 

py

 object exported from reticulate. For example:

Enter 

exit

 within the Python REPL to return to the R prompt.

Note that Python code can also access objects from within the R session using the 

r

 object (e.g. 

r.flights

). See the 

repl_python()

 documentation for additional details on using the embedded Python REPL.

Type conversions

When calling into Python, R data types are automatically converted to their equivalent Python types. When values are returned from Python to R they are converted back to R types. Types are converted as follows:

R Python Examples
Single-element vector Scalar 11LTRUE"foo"
Multi-element vector List c(1.0, 2.0, 3.0)c(1L, 2L, 3L)
List of multiple types Tuple list(1L, TRUE, "foo")
Named list Dict list(a = 1L, b = 2.0)dict(x = x_data)
Matrix/Array NumPy ndarray matrix(c(1,2,3,4), nrow = 2, ncol = 2)
Data Frame Pandas DataFrame data.frame(x = c(1,2,3), y = c("a", "b", "c"))
Function Python function function(x) x + 1
NULL, TRUE, FALSE None, True, False NULLTRUEFALSE

If a Python object of a custom class is returned then an R reference to that object is returned. You can call methods and access properties of the object just as if it was an instance of an R reference class.

Learning more

The reticulate website includes comprehensive documentation on using the package, including the following articles that cover various aspects of using reticulate:

  • Calling Python from R — Describes the various ways to access Python objects from R as well as functions available for more advanced interactions and conversion behavior.
  • R Markdown Python Engine — Provides details on using Python chunks within R Markdown documents, including how call Python code from R chunks and vice-versa.
  • Python Version Configuration — Describes facilities for determining which version of Python is used by reticulate within an R session.
  • Installing Python Packages — Documentation on installing Python packages from PyPI or Conda, and managing package installations using virtualenvs and Conda environments.
  • Using reticulate in an R Package — Guidelines and best practices for using reticulate in an R package.
  • Arrays in R and Python — Advanced discussion of the differences between arrays in R and Python and the implications for conversion and interoperability.

Why reticulate?

From the Wikipedia article on the reticulated python:

The reticulated python is a speicies of python found in Southeast Asia. They are the world’s longest snakes and longest reptiles…The specific name, reticulatus, is Latin meaning “net-like”, or reticulated, and is a reference to the complex colour pattern.

From the Merriam-Webster definition of reticulate:

1: resembling a net or network; especially : having veins, fibers, or lines crossing a reticulate leaf. 2: being or involving evolutionary change dependent on genetic recombination involving diverse interbreeding populations.

The package enables you to reticulate Python code into R, creating a new breed of project that weaves together the two languages.