Rython

Embed python in R

Environment

Ubuntu with python 2.7

Please install the following ubuntu packages:

Usage

library(Rython)
py("python script")

Type Mapping

To pass object from R to python, use the following functions:

library(xtable)
type.mapping <- matrix(dimnames=list(NULL, c("function name", "R type", "python type")), byrow=TRUE, ncol=3, c(
  "pydict", "list", "dictionary",
  "pylong", "integer", "list of long",
  "pyfloat", "numeric", "list of float",
  "pybool", "logical", "list of bool",
  "pystr", "character", "list of str"
  ))
print(xtable(type.mapping), type="html")

API

Demo

cql 1.4

The following script query the Cassandra with python-cql 1.4 and pass the value back to R

library(Rython)
py("import cql")
py("con = cql.connect(host, port, key_space, cql_version='3.0.0')")
py("cursor = con.cursor()")
py("cursor.execute('select * from column_family limit 1')")
py("result = cursor.fetchall()")
py("print result")
py("result1 = result[0]")
result1 <- pyobj(obj_name="result1", module_name="")
pywrap(result1)

Demo for R package with python

Put your python script under inst/python and install it.

#'@export
.onLoad <- function(libname, pkgname) {
  import_pkg_module(pkgname, "foo.py")
}

NEWS



wush978/Rython documentation built on May 4, 2019, 12:02 p.m.