在 anaconda Jupyter notebook 中安装 R 库以使用 R magic

installing an R library to work with R magic in an anaconda Jupyter notebook

我正在尝试 运行 先知教程,它在 Jupyter 笔记本中使用 R magic。以下代码:

%%R
library(prophet)
df <- read.csv('../examples/example_wp_peyton_manning.csv')
df$y <- log(df$y)
m <- prophet(df)
future <- make_future_dataframe(m, periods=366)

Returns这个:

Error in library(prophet) : there is no package called ‘prophet’

然后,在我的 iPython 笔记本中,我 运行 这个:

from rpy2.robjects.packages import importr
utils = importr('utils')
utils.install_packages('prophet')

哪个returns这个:

--- Please select a CRAN mirror for use in this session ---
Secure CRAN mirrors 

 1: 0-Cloud [https]                 2: Australia (Canberra) [https] 
 3: Australia (Melbourne) [https]   4: Australia (Perth) [https]    
 5: Austria [https]                 6: Belgium (Ghent) [https]      
 7: Brazil (RJ) [https]             8: Brazil (SP 1) [https]        
 9: Bulgaria [https]               10: Chile 1 [https]              
11: China (Lanzhou) [https]        12: Colombia (Cali) [https]      
13: Czech Republic [https]         14: Denmark [https]              
15: France (Lyon 1) [https]        16: France (Lyon 2) [https]      
17: France (Marseille) [https]     18: France (Montpellier) [https] 
19: France (Paris 2) [https]       20: Germany (Münster) [https]    
21: Iceland [https]                22: Indonesia (Jakarta) [https]  
23: Ireland [https]                24: Italy (Padua) [https]        
25: Japan (Tokyo) [https]          26: Malaysia [https]             
27: Mexico (Mexico City) [https]   28: Norway [https]               
29: Philippines [https]            30: Russia (Moscow) [https]      
31: Spain (A Coruña) [https]       32: Spain (Madrid) [https]       
33: Sweden [https]                 34: Switzerland [https]          
35: UK (Bristol) [https]           36: UK (Cambridge) [https]       
37: UK (London 1) [https]          38: USA (CA 1) [https]           
39: USA (KS) [https]               40: USA (MI 1) [https]           
41: USA (TN) [https]               42: USA (TX 1) [https]           
43: USA (TX 2) [https]             44: (other mirrors)         

出现一个输入框,我所做的任何选择都会导致:

rpy2.rinterface.NULL

我有 RStudio,Prophet 在 R Studio 中遇到 运行ning w/o 问题。这告诉我我在某处有另一个 R 内核 运行ning,链接到 Anaconda 中的环境,或者其他一些配置错误。

有什么方法可以解决此问题,以便我可以 运行 R 使用我在 R Studio 中的内核或强制当前的 R 内核安装先知?

我怎么知道这个 Jupyter notebook 中 R magic 使用的 R 内核的位置?

我正在使用 mac,我可能有一些交叉链接的文件等。(我的 Jupyter notebook 显示 6 个内核,而我实际上有 3 个..它重复了我所拥有的两次) .

谢谢

问题中有很多问题。回答其中之一:

How do I know the location of the R kernel used by R magic in this Jupyter notebook?

在 Jupyter 中,执行:

%run -m rpy2.situation 

你可能有 2 个版本的 R。当你从 Anaconda 安装 R 内核时,它会安装它自己的版本,不管你在 RStudio 中有什么。这是你应该做的。在 Jupyter notebook 中,运行 单元格中的以下内容:

%load_ext rpy2.ipython

然后

%%R
.libPaths()

应该return像这样:

[1] "/Users/user/anaconda/lib/R/library"

现在转到 RStudio 和 运行 同一行:

.libPaths()

可能return是这样的:

[1] "/Users/user/Library/R/3.2/library"                      
[2] "/Library/Frameworks/R.framework/Versions/3.2/Resources/library"

在这个例子中,你可以看到一个R在anaconda中,另一个是独立的R。你的RStudio中的那个,你正确加载Prophet的那个是独立的。

最好的解决方案是让 RStudio 使用与 Conda 相同的版本。为此,有很多方法可以在两个版本之间切换,但最好的方法是使用一个名为 Rswitch 的简单实用程序,您可以从 here.

下载该实用程序

RSwitch 检测您计算机中的所有 R 版本,并允许您的 RStudio 在您拥有的不同版本的 R 之间切换。

同样,我的建议是切换到 Conda 使用的 R 版本,并从 RStudio 安装您的包以避免从 Jupyter notebook 进行安装,这可能会显示

等错误

rpy2.rinterface.NULL

你指出的。希望这有效。