py_get_item | R Documentation |
Access an item from a Python object, similar to how x[key]
might be
used in Python code to access an item indexed by key
on an object x
. The
object's __getitem__()
__setitem__()
or __delitem__()
method will be
called.
py_get_item(x, key, silent = FALSE)
py_set_item(x, key, value)
py_del_item(x, key)
## S3 method for class 'python.builtin.object'
x[...]
## S3 replacement method for class 'python.builtin.object'
x[...] <- value
x |
A Python object. |
key , ... |
The key used for item lookup. |
silent |
Boolean; when |
value |
The item value to set. Assigning |
For py_get_item()
and [
, the return value from the
x.__getitem__()
method. For py_set_item()
, py_del_item()
and [<-
,
the mutate object x
is returned.
The py_get_item()
always returns an unconverted python object, while
[
will automatically attempt to convert the object if x
was created
with convert = TRUE
.
## Not run:
## get/set/del item from Python dict
x <- r_to_py(list(abc = "xyz"))
#' # R expression | Python expression
# -------------------- | -----------------
x["abc"] # x["abc"]
x["abc"] <- "123" # x["abc"] = "123"
x["abc"] <- NULL # del x["abc"]
x["abc"] <- py_none() # x["abc"] = None
## get item from Python list
x <- r_to_py(list("a", "b", "c"))
x[0]
## slice a NumPy array
x <- np_array(array(1:64, c(4, 4, 4)))
# R expression | Python expression
# ------------ | -----------------
x[0] # x[0]
x[, 0] # x[:, 0]
x[, , 0] # x[:, :, 0]
x[NA:2] # x[:2]
x[`:2`] # x[:2]
x[2:NA] # x[2:]
x[`2:`] # x[2:]
x[NA:NA:2] # x[::2]
x[`::2`] # x[::2]
x[1:3:2] # x[1:3:2]
x[`1:3:2`] # x[1:3:2]
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.