首页 > 解决方案 > 将 r 包导入 jupyter 笔记本(使用 rpy2)的最佳方法是什么?

问题描述

我正在尝试使用 rpy2 将 r 包(标题为“afex”)导入到 jupyter notebook 中,以便于使用 python。但是,我收到“选择:”语句后跟空白行。我尝试在空白行中输入各种镜像链接,但是没有任何帮助。我很想听听是否有人可能遇到过这种情况并且可以提供帮助。

请参阅下面的代码:

#Importing key libraries.

import numpy as np
import pandas as pd
import seaborn as sns
import scipy.stats as stats
import matplotlib.lines as mlines
from matplotlib import pyplot as plt
from statsmodels.formula.api import ols

# Download rpy2 package
# Note: The r-modules require Rpy2 to be installed, which should first be done using 
# "conda install -c conda-forge rpy2"
# using Jupyter lab within the conda navigator (terminal within there).
%load_ext rpy2.ipython
%load_ext rmagic

# Import the necessary objects from rpy2 and acrivate conversion for dataframe columns
from rpy2.robjects import *
pandas2ri.activate()

# Import the r library for anovas
#import afex
%R install.packages("afex")

这是问题(我在上述命令之后得到的输出): 选择:

还附上了问题截图的链接。问题截图

标签: rpython-3.xjupyter-notebookrpy2

解决方案


默认情况下,R 会以交互方式要求用户选择一个应该从哪里下载包的镜像,并且由于某种原因,镜像列表不会显示在您的系统上(使用 Windows ? rpy2 不支持)。

请参阅文档以选择镜像并从 Python 安装:

https://rpy2.github.io/doc/v2.9.x/html/robjects_rpackages.html#installing-removing-r-packages


推荐阅读