Skip to contents

This requires the reticulate package to be installed.

Usage

# S3 method for iteror
r_to_py(x, convert = FALSE, ...)

# S3 method for python.builtin.object
iteror(obj, ...)

Arguments

x

An iterable object.

convert

does nothing.

...

Passed along to iteror(x, ...).

obj

A Python object (as viewed by package reticulate.)

Value

r_to_py(it) returns a Python iterator.

Method iteror.python.builtin.object returns an iteror.

Examples

pit <- reticulate::r_to_py(iseq(2, 11, 5))
reticulate::iter_next(pit, NULL)
#> [1] 2
reticulate::iter_next(pit, NULL)
#> [1] 7
reticulate::iter_next(pit, NULL)
#> NULL

# create an R iterator and ask Python to sum it
triangulars <- icount() |> i_accum() |> i_limit(10)
builtins <- reticulate::import_builtins()
builtins$sum(triangulars) # r_to_py is called automatically
#> [1] 220

# create a generator in Python and sum it in R
pit <- reticulate::py_eval("(n for n in range(1, 25) if n % 3 == 0)")
sum(iteror(pit))
#> [1] 108