The object r is alsocallable , and the string passed in a call is evaluated as R code.
An R object has a string representation that can be used directly into R code to be evaluated.
In R, data are mostly represented by vectors, even when looking like scalars. When looking closely at the R object pi used previously, we can observe that this is in fact a vector of length 1.
Creating R vectors can be achieved simply.
The easiest way to create such objects is to do it through R functions.
Calling R functions is disappointingly similar to calling Python functions.
By default, calling R functions return R objects.
Linear models
Creating an R vector or matrix, and filling its cells using Python code
This module should be the right pick for casual and general use. Its aim is to abstract some of the details and provide an intuitive interface to both Python and R programmers.
The instance can be seen as the entry point to an embedded R process. The elements that would be accessible from an equivalent R environment are accessible as attributes of the instance.
When safety matters most, we recommend using__getitem__()to get a given R object.
Storing the object in a python variable will protect it from garbage collection, even if deleted from the objects visible to an R user.
Just like it is the case with RPy-1.x, on-the-fly evaluation of R code contained in a string can be performed by calling the r instance.
The astute reader will quickly realize that R objects named by python variables can be plugged into code through their R representation.
R environments can be described to the Python user as an hybrid of a dictionary and a scope.
The first of all environments is called the Global Environment, that can also be referred to as the R workspace.
Assigning a value to a symbol in an environment has been made as simple as assigning a value to a key in a Python dictionary.
An environment is also iter-able, returning all the symbols (keys) it contains.
R functions exposed by rpy2's high-level interface can be used:
This is all looking fine and simple until R arguments with names such as na.rm are encountered. By default, this is addressed by having a translation of ‘.’ (dot) in the R argument name into a ‘_’ in the Python argument name.
In Python one can write:
R is capable of introspection, and can return the arguments accepted by a function through the function formals().
The method Function.rcall() is an alternative way to call an underlying R function.
For tasks such as modelling and plotting, an R formula can be a terse, yet readable, way of expressing what is wanted.
The classrobjects.Formulais representing an R formula.
Other options are:
This is achieved by the R functions library() and require() (attaching the namespace of the package to the R search path).
Beside functions and environments, most of the objects an R user is interacting with are vector-like. For example, this means that any scalar is in fact a vector of length one.
The class Vector has a constructor:
Creating vectors can be achieved either from R or from Python.
When the vectors are created from R, one should not worry much as they will be exposed as they should by rpy2.robjects.
When one wants to create a vector from Python, either the class Vector or the convenience classesIntVector, FloatVector, BoolVector, StrVectorcan be used.
Extracting, Python-style
The python__getitem__()method behaves like a Python user would expect it for a vector (and indexing starts at zero).
Extracting, R-style
Access to R-style extracting/subsetting is granted though the two delegators rx and rx2, representing the R functions [ and [[ respectively.
Assigning, Python-style
Since vectors are exposed as Python mutable sequences, the assignment works as for regular Python lists.
推荐阅读
- 游戏仿真模拟器封面,游戏仿真模拟器封面怎么设置
- 茂名应急管理局公众号关注,陕西应急管理局公众号
- 作文网络游戏300字,网络游戏作文200字
- 电视全面屏什么,电视全面屏的好处
- mysql多表怎么关联 mysql怎么多表联查
- jquery表格tree,jquery表格筛选
- mysql如何查询某条记录是第多少条,mysql查询每个用户的第一条记录
- 视频号投流价格怎么设置,视频号 投票
- java小闹钟代码 java闹钟小程序