为Jupyter notebook配置R kernel过程及踩坑记录
Posted Data+Science+Insight
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为Jupyter notebook配置R kernel过程及踩坑记录相关的知识,希望对你有一定的参考价值。
为Jupyter notebook配置R kernel过程及踩坑记录
注意:本文为作者安装过程及折腾的过程,内容比较冗杂,如果读者想直接创建一个属于自己的子环境则参考:
如下文章:
anaconda下配置R子环境并配置jupyter notebook的R Kernel
stackExchange+Anconda R version - How to upgrade to 4.0 and later
我们想用jupyter notebook运行R代码,但是发现我们的kernel只有python3可以选择
可是我们就想用R进行数据科学任务的实施。
# windows10系统已经安装了R软件、RStudio
# 打开R原生环境
# 注意不是RStudio
# 输入如下命令
install.packages(c('repr', 'IRdisplay', 'evaluate', 'crayon', 'pbdZMQ', 'devtools', 'uuid', 'digest'))
#笔者的机器配置上海无法下载,最后配置兰州成功:
我们可以手动去设置镜像地址:
#安装 IRkernel包:
devtools::install_github('IRkernel/IRkernel')
发生如下错误:
错误: Failed to install 'IRkernel' from GitHub:
Git does not seem to be installed on your system.
提示是因为我们的系统中没有git服务所以没有安装,好了那么我就安装一个git好了:
具体参考笔者文章:
git安装+错误:Failed to install 'IRkernel' from GitHub: Git does not seem to be installed on your system
# 注意,各位在刚装完git之后运行
devtools::install_github('IRkernel/IRkernel')
可能会依据报错,可以把R console关掉重新打开再试;
# 可能会因为国内网络连接github的速度或者墙的问题,偶尔也会出去,大家耐心多运行几次;
不过后来发现CRAN已经添加了这部分包,所以如下一条语句就可以了:
install.packages('IRkernel')
# 输入如下命令:
# 通过intallspec()函数,使Jupyter能找到刚刚安装的R核
IRkernel::installspec(user = FALSE)
接着出现如下错误:
Error in IRkernel::installspec(user = FALSE) :
jupyter-client has to be installed but “jupyter kernelspec --version” exited with code 127.
此外: Warning message:
In system2("jupyter", c("kernelspec", "--version"), FALSE, FALSE) :
'"jupyter"' not found
# 我们继续解决
# 实际上笔者的jupyter notebook及其服务是一直启动着的,只不过是通过anaconda启动的;
(base) C:\\Users\\user>
(base) C:\\Users\\user>jupyter notebook list
Currently running servers:
http://localhost:8888/?token=184900cdd13d72d3d046dc9238c3425083a14341e606e2da :: E:\\
# 有网友建议使用 conda install -c r r-irkernel 之后再试;
之后重新启动jupyter notebook,如论通过anaconda启动还是windows启动项启动都获得了我们期望的结果:
R kernel已经成功注册到了jupyter notebook的核列表中;
# 实际运行是发现还是有问题:
# 实际所有的包均已安装;
Error in library(dplyr): there is no package called 'dplyr'
Traceback:
1. library(dplyr)
继续上stackoverflow查找问题的根源:
最后成功;
- If Anaconda is installed and the Jupyter-Notebook with it (should be the standard install), open up the Anaconda prompt, not the Windows command prompt or the Anaconda Navigator
- Look up the executable of R (not Rgui or Rstudio), it should be somewhere like C:\\Program Files\\R\\R-3.5.1\\bin and remember the path typing cd C:\\Program Files\\R\\R-3.5.1\\bin and start R by typing R
- typing IRkernel::installspec()
- Now you can start an R kernel within Jupyter-Notebook
# 非常遗憾还是不行,继续按照网络的其他方案找到本地目录:
D:\\anaconda\\Scripts
然后加入PATH
setwd('
D:\\anaconda\\Scripts')
IRkernel::installspec(user = FALSE)
接着参考stackoverflow+github
想起自己先前执行了这个命令:conda install -c r r-irkernel
所以可能就压根和我们在windows启动目录里面的环境不是一个东西,所以找不到包,试着在jupyter中执行install.packages()命令,OK:
为了证明:
conda uninstall -c r r-irkernel
uninstall之后,jupyter环境依旧如故;
依旧是使用anaconda prompt进入的R环境可以设置:IRkernel::installspec(user = FALSE)
而从windows原生的cmd进入之后执行:IRkernel::installspec(user = FALSE)
依旧会报如下错误:
> IRkernel::installspec(user = FALSE)
Error in IRkernel::installspec(user = FALSE) :
jupyter-client has to be installed but "jupyter kernelspec --version" exited with code 1.
> IRkernel::installspec(user = FALSE)
发现jupyter中能用的这个R的版本为3.6.1
而Windows中手动安装的版本为4.0.5
现在倒回来执行:conda install -c r r-irkernel
(base) C:\\Users\\user>jupyter kernelspec list
Available kernels:
ir C:\\Users\\user\\AppData\\Roaming\\jupyter\\kernels\\ir
python3 D:\\anaconda\\share\\jupyter\\kernels\\python3
如果是新创建一个子环境参考如下内容:
stackExchange+Anconda R version - How to upgrade to 4.0 and later
如果是新旧有环境升级:
conda install -c conda-forge r-base==4.1.0
参考:R报错:Failed to install ‘IRkernel‘ from GitHub: Git does not seem to be installed on your system.
参考:如何在Jupyter Notebook里添加R核的详细步骤
参考:jupyter notebook
参考:https://github.com/IRkernel/IRkernel
参考:Jupyter Notebook/Lab中添加R Kernel的详细步骤
参考:在Jupyter Notebook/Lab中添加R Kernel
参考:jupyter-client has to be installed but “jupyter kernelspec --version” exited with code 127
参考:Error in IRkernel::installspec() : jupyter-client has to be installed but “jupyter kernelspec --version” exited with code 127. In addition: Warning message: running command '"jupyter" kernelspec --version' had status 127 #510
以上是关于为Jupyter notebook配置R kernel过程及踩坑记录的主要内容,如果未能解决你的问题,请参考以下文章
anaconda下配置R子环境并配置jupyter notebook的R Kernel
好消息! 不用再羡慕Python有jupyter 我R也有Notebook了附演示视频