问题的起因在于我并不是我们组服务器的管理员以及师兄不定期更新base中的R版本,这些都给我造成了很多bug。之前对这些bug视而不见,因为总能够通过一些弯弯绕绕的方法解决,但最近寝室网太差,耐心确实是少了点的我决定还是解决一下吧,孩子真的心累了~~~

- 我的需求是:想要在不同的R版本下运行jupyter notebook
- 我之前的操作是:通过在不同环境中切换打开jupyter notebook才能运行该环境中的R
- 经验总结:
首先,确认base中安装的R版本,并新建虚拟环境以及在新的环境中下载R
# 新建虚拟环境
conda create -n r4.1 r=4.1.0
# 激活虚拟环境
conda activate r4.1
# 退出虚拟环境
conda deactivate r4.1
接着,需要确保在base环境和虚拟环境中都下载了jupyter notebook
conda install jupyter notebook
# jupyter notebook的配置只在base环境中进行,以确保所有环境都能运行jupyter notebook
jupyter notebook --generate-config # 生成配置文件
python # 打开python
from notebook.auth import passwd
passwd() # 生成密钥
vim .jupyter/jupyter_notebook_config.py # 修改配置文件
c.NotebookApp.ip='*'
c.NotebookApp.passwd=u'刚才生成的密钥'
c.NotebookApp.open_browser=False
c.NotebookApp.port=自己设置一个端口,不要设置8888
c.NotebookApp.allow_remote_access=True
然后,在base环境和虚拟环境中为jupyter notebook添加Rkernel
install.packages("IRkernel")
IRkernel::installspec()
# 为了区分base环境和虚拟环境当中的Rkernel以及防止相同名称的覆盖,需要给一个环境的Rkernel更改名称
IRkernel::installspec(name="ir_base",displayname="R_base")
最后,通过运行命令行以及打开jupyter notebook查看是否配置正确
# 查看环境当中的所有kernel
jupyter kernelspec list
# 删除某个kernel
jupyter kernelspec remove kernelname
# 运行并网页打开jupyter notebook
jupyter notebook

查看环境当中的kernel

配置好的Rkernel