2017-09-16 77 views
1

我想为先知,在Jupyter笔记本中使用R魔法运行一个教程。下面的代码:安装一个R库,在一个anaconda Jupyter笔记本上使用R魔法

%%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) 

返回此:

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

然后,在我的IPython的笔记本我运行此:

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

它返回这样的:

--- 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,并且先知在R Studio中运行无w/o问题。这告诉我,我有另一个R内核在某处运行,链接到Anaconda的环境或其他一些配置错误。

有没有办法解决这个问题,所以我可以用R Studio中的内核运行R或强制当前的R内核安装prophet?

如何知道R魔术师在这款Jupyter笔记本中使用的R内核的位置? (我的Jupyter笔记本显示了6个内核,当我真的有3个时,我重复了我的两次)。

谢谢

+0

除非您正在处理[Prophet](http://www.prophet-web.com/)软件,否则请不要使用[prophet]标记。 – YowE3K

回答

1

你可能有2个版本R的当您安装从巨蟒[R内核,它安装自己的版本,无论你在什么RStudio。这是你应该做的。从Jupyter笔记本电脑上,运行在电池下面:

%load_ext rpy2.ipython 

然后

%%R 
.libPaths() 

它应该返回是这样的:

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

现在去RStudio并运行相同的线:

.libPaths() 

它pr可以返回这样的东西:

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

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

最好的解决方案是让RStudio使用Conda使用的相同版本。要做到这一点,在这两个版本之间切换的方式有很多,但最好的方法是使用一个简单的实用程序Rswitch,您可以从here下载。

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

enter image description here

同样,我的建议是切换到R的版本康达使用,并从RStudio,安装你的包,以避免从Jupyter笔记本电脑,它可以显示错误,如

rpy2.rinterface.NULL

你指示。希望这可以工作。

0

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

我如何知道R魔术师在这款Jupyter笔记本中使用的R内核的位置?

在Jupyter,做到:

%run -m rpy2.situation 
+0

谢谢。该命令什么都不返回。 –

+0

然后如果没有打印任何东西,那么你的python环境或jupyter安装可能会有一些问题。该命令使用“print()”https://bitbucket.org/rpy2/rpy2/src/46f9092c262ab98a4ff5250d95cec84ace0782b2/rpy/situation.py?at=default#situation.py-110 – lgautier