【问题标题】:NotImplementedError: Conversion 'py2rpy' not defined for objects of type '<class 'str'>'NotImplementedError:转换 \'py2rpy\' 未为 \'<class \'str\'>\' 类型的对象定义
【发布时间】:2022-10-26 01:28:21
【问题描述】:

我正在 Python 中构建一个Plotly Dash,我需要使用 rpy2 包来导入 Python 不支持的一些函数,所以当我在破折号中调用 (%R-i df) 时出现此错误,

奇怪的是,R 代码在破折号之外可以正常工作,但它不能完全实现 R 的任何代码。

我尝试了下面链接的所有解决方案,但它不起作用。有任何想法吗?

https://github.com/theislab/anndata2ri/issues/50 https://rpy2.github.io/doc/latest/html/pandas.html

【问题讨论】:

  • 如果您包含代码会更有帮助。共享 Python 和 R 对象,如数据框、数组、矩阵和绘图,往往效果很好。共享功能,没那么多。在 R 中使用 R 中的函数,并将修改后的数据带入 Python。另一种可能性是在没有 Dash 的情况下呈现您正在寻找的内容。我没有遇到过 Dash 提供的任何我无法使用 R 和 Javascript 实现的东西。我还没有遇到对 Python 这样做的需要。但是,如果你可以在 R 中做到这一点,我有 99.99999% 的把握你可以在 Python 中做到这一点。

标签: python r plotly-dash rpy2


【解决方案1】:

您需要在 Dash/Plotly 绘图函数中添加 r 上下文 - 带有回调更新的函数。像这样的东西。

@app.callback(
    Output('graph-to-update', 'figure'),
    Input('input-slide', 'value')
)
def my_graph_func(var):

    with localconverter(ro.default_converter + pandas2ri.converter):
        result = robject(var)
    
    fig = create_myfigure(result)

    return result

rpy2 文档解释了您的问题:https://rpy2.github.io/doc/latest/html/pandas.html

BW

【讨论】:

    猜你喜欢
    • 2022-11-09
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多