Anaconda安装R语言及虚拟环境下如何添加R和python到jupyter

有时候,会简单调用R语言,anaconda加装R语言。

#20220323更新
conda install r
#命令行输入R,进行R环境
install.packages('IRkernel')
IRkernel::installspec()
#刷新jupyterlab即可看到R。

#20220408更新
区别于上次的操作,再次添加不同的虚拟环境下的R,执行如下:
conda create -n r413
conda install -c conda-forge r-base
install.packages('IRkernel')
IRkernel::installspec(name = 'ir413', displayname = 'R 4.1.3')
# 上面这步会报错,是因为,虚拟环境r413下,直接输入R进入的R环境,并没有指定对应的r版本,此时,我们应该进入虚拟环境后,以输入r的具体路径,替代输入R
conda activate r413
whereis R
[1] R: /usr/bin/R /usr/lib/R /etc/R /usr/local/lib/R /usr/share/R /root/anaconda3/envs/r413/bin/R /usr/share/man/man1/R.1.gz
# 此时,可以看出,此处返回的R路径为/root/anaconda3/envs/r413/bin/R,我们实际需要运行的具体路径为/root/anaconda3/envs/r413/lib/R/bin
# 我们同样可以借助print(system.file("kernelspec", package = "IRkernel"))指令,去查看IRkernel的安装路径
print(system.file("kernelspec", package = "IRkernel"))
[1] "/root/anaconda3/envs/r413/lib/R/library/IRkernel/kernelspec"
.libPaths()
[1] '/root/anaconda3/envs/r413/lib/R/library'
R.home()
[1] '/root/anaconda3/envs/r413/lib/R'

/root/anaconda3/envs/r413/lib/R/bin/R #进入指定路径的R
IRkernel::installspec(name = 'ir413', displayname = 'R 4.1.3')
# 成功

# 补充python添加虚拟环境到jupyter
conda create -n py310 python=3.10.4 ipykernel
conda activate py310
python -m ipykernel install --user --name py310 --display-name "Python 3.10.4"
# 成功

# 查看jupyter已有内核
jupyter kernelspec list

# 删除jupyter已有内核
jupyter kernelspec uninstall ir413
conda install R
conda update r r=3.5.2 #指定版本安装
---
#R环境或RStudio中,输入命令:
install.packages(c('repr', 'IRdisplay', 'evaluate', 'crayon', 'pbdZMQ', 'devtools', 'uuid', 'digest'))
devtools::install_github('IRkernel/IRkernel')
# 只在当前用户下安装
IRkernel::installspec()
# 或者是在系统下安装
IRkernel::installspec(user = FALSE)

conda  install -c r r-essentials #或者直接在conda中安装即可
conda install -c r rstudio

参考链接:
https://cloud.tencent.com/developer/article/1543568
https://www.jianshu.com/p/2e1986296e15
https://www.jianshu.com/p/fcbac8b0f617
https://zhuanlan.zhihu.com/p/416087348
https://www.jianshu.com/p/62701c3d1ff2
https://blog.csdn.net/zpf336/article/details/104480732
https://sharifxu.top/2021/06/30/JupyterNoteWithAnaconda/
https://blog.csdn.net/sdnuwjw/article/details/111940416
http://paisheng.me/2015/08/16/%E5%AE%89%E8%A3%85%E7%AC%94%E8%AE%B0-%E5%9C%A8Jupyter%E4%B8%AD%E6%B7%BB%E5%8A%A0R-Kernel/
https://blog.csdn.net/guoyuzhu_2013/article/details/106604447
https://www.jianshu.com/p/d0ec882702fc
https://www.jianshu.com/p/b9eb874fc8f4
https://blog.csdn.net/rotator00cc/article/details/119144967
https://zhuanlan.zhihu.com/p/392481027

本文链接:

https://ma.ge/archives/273.html
1 + 7 =
快来做第一个评论的人吧~