Learn R Programming

SnakeCharmR (version 1.0.7.1)

py.get: Get values from Python variables to R

Description

py.get get the value of Python and returns it to the R environment. Objects are serialized as JSON strings on Python with json.dumps, are transferred to R and are converted back to an R value using jsonlite::fromJSON.

Usage

py.get(var.name, json.opt.ret = getOption("SnakeCharmR.json.opt.ret", list()))

Arguments

var.name

a character string containing a valid Python variable name

json.opt.ret

explicit arguments to pass to jsonlite::fromJSON when deserializing the value

Value

an R object containing the variable value after serialization to JSON in the Python environment and deserialization from JSON in the R environment

Examples

Run this code
# NOT RUN {
py.assign("a", 1:4)
py.get("a")
# [1] 1 2 3 4

py.assign("b", list(one = 1, foo = "bar"))
str(py.get("b"))
# List of 2
#  $ foo: chr "bar"
#  $ one: int 1

py.exec("import math")
py.get("math.pi")
# [1] 3.141593

# }
# NOT RUN {
py.rm("notset")
py.get("notset")
# Error in py.get("notset") (from py.get.R#60) : Traceback (most recent call last):
#   File "<string>", line 2, in <module>
# NameError: name 'notset' is not defined
# }

Run the code above in your browser using DataLab