pyDict: Create a virtual Python dictionary

Description Usage Arguments Details Examples

View source: R/PyObjectDict.R

Description

The function pyDict creates a virtual Python object of type PythonInR_Dict.

Usage

1
pyDict(key, value, regFinalizer = TRUE)

Arguments

key

a character string giving the name of the Python object.

value

if a value is provided, a new Python dictionary is created based on the value. Therefore allowed values of value are named lists and names vectors.

regFinalizer

a logical indicating if a finalizer should be be registered, the default value is TRUE.

Details

If no value is provided a virtual Python dict for an existing Python object is created. If the value is NULL, an empty virtual Python object for an empty dict is created. If the value is a named vector or named list, a new Python object based on the vector or list is created.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
if ( pyIsConnected() ){
pyExec('myPyDict = {"a":1, "b":2, "c":3}')
## create a virtual Python dictionary for an existing dictionary
myDict <- pyDict("myPyDict")
myDict["a"]
myDict["a"] <- "set the key"
myDict
## allowed keys are
myDict['string'] <- 1
myDict[3L] <- "long"
myDict[5] <- "float"
myDict[c("t", "u", "p", "l", "e")] <- "tuple"
myDict
## NOTE: Python does not make a difference between a float key 3 and a long key 3L
myDict[3] <- "float"
myDict
## create a new Python dict and virtual dict
myNewDict <- pyDict('myNewDict', list(p=2, y=9, r=1))
myNewDict
}

PythonInR documentation built on July 1, 2020, 6:05 p.m.