pySet: assigns R objects to Python

Description Usage Arguments Details Examples

View source: R/PySet.R

Description

The function pySet allows to assign R objects to the Python namespace, the conversion from R to Python is done automatically.

Usage

1
pySet(key, value, namespace = "__main__")

Arguments

key

a string specifying the name of the Python object.

value

a R object which is assigned to Python.

namespace

a string specifying where the key should be located. If the namespace is set to "__main__" the key will be set to the global namespace. But it is also possible to set attributes of objects e.g. the attribute name of the object 'os'.

Details

More information about the type conversion can be found in the README file or at http://pythoninr.bitbucket.org/.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
pySet("x", 3)
pySet("M", diag(1,3))
pyImport("os")
pySet("name", "Hello os!", namespace="os")
## In some situations it can be beneficial to convert R lists or vectors
## to Python tuple instead of lists. One way to accomplish this is to change
## the class of the vector to "tuple".
y <- c(1, 2, 3)
class(y) <- "tuple"
pySet("y", y)
## pySet can also be used to change values of objects or dictionaries.
asTuple <- function(x) {
 class(x) <- "tuple"
 return(x)
}
pyExec("d = dict()")
pySet("myTuple", asTuple(1:10), namespace="d")
pySet("myList", as.list(1:5), namespace="d")

PythonInR documentation built on May 2, 2019, 5:17 p.m.