This function unpacks elements of a Python tuple (or any sequence-like
object) into R variables, similar to tuple unpacking in Python
(a, b = (1, 2)
).
Usage
py_tuple_unpack(vars, value, envir = parent.frame(), quote_vars = TRUE)
Arguments
- vars
A symbol or a call like
c(a, b, c)
representing the variables to assign into.- value
A Python object (tuple, list, etc.) or an R vector/list to unpack.
- envir
Environment. Environment in which to assign the unpacked values. Defaults to the calling environment.
- quote_vars
Boolean. If
TRUE
,vars
is captured unevaluated (recommended when writingc(a, b, c)
directly).
Details
If vars
is a vector of names created with c(...)
, each element of
value
is assigned to the corresponding variable in the calling
environment. Nested unpacking is supported recursively.
If the number of elements in value
exceeds the number of variables
provided, a warning is issued but unpacking proceeds for the available
variables.
Examples
if (FALSE) { # \dontrun{
# Simple unpacking
py_tuple_unpack(c(a, b), list(1, 2))
a # 1
b # 2
# With nested unpacking
py_tuple_unpack(c(a, c(b, d)), list(1, list(2, 3)))
a # 1
b # 2
d # 3
# Works with Python tuples/lists
tup <- reticulate::tuple(list(10, list(20, 30)))
py_tuple_unpack(c(x, c(y, z)), tup)
x # 10
y # 20
z # 30
} # }