rstudio / rstudio/reticulate

Can't call some python callables

Open
#1,024 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
1.8k
Forks
349
Avg merge
5h 13m
Merged PRs (30d)
4

Description

Hi,

Should R functions converted to python still be callable from R?

library(reticulate)
fn <- r_to_py(function(x) x)
try(fn(1)) # should this succeed?
#> Error in fn(1) : could not find function "fn"

Calling py_to_r enables calling again, but doesn’t change the class or printed output.

fn
#> <function make_python_function.<locals>.python_function at 0x7f243344aef0>
class(fn)
#> [1] "python.builtin.function" "python.builtin.object"
fn <- py_to_r(fn)
fn
#> <function make_python_function.<locals>.python_function at 0x7f243344aef0>
class(fn)
#> [1] "python.builtin.function" "python.builtin.object"
fn(1)
#> 1.0

(Changing the value of convert seems to make no difference.)

This comment and the extensive use of as.environment suggests that maybe the intent is for all python callables to be callable from R and this is a bug?

It would be very convenient if all python callables are always directly callable from R without needing to go though py_call.

Created on 2021-07-02 by the reprex package (v2.0.0)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with R/python.R around the referenced line 220 and trace how r_to_py creates Python callables, including the use of as.environment. Reproduce the issue's fn(1) example and compare it with py_to_r and py_call; done means a callable returned by r_to_py can be called directly from R as described.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, r
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.