conda创建`R`或`R相关包`的虚拟环境并被jupyter识别
整体要求:
- 创建的
python
新环境需要配置ipykernel
使其被jupyter
识别- 创建的
R
新环境需要配置ipykernel
和IRkernel
使其被jupyter
识别。IRkernel
通过ipykernel
完成配置,因此需要先装ipykernel
,再装IRkernel
。
在新环境中安装R
或R相关包
并被识别
以安装R
为例
# 创建名为 R 的虚拟环境
$ conda create R
# 激活虚拟环境 R
$ conda activate R
# 在虚拟环境R中安装 R
$ conda install r-base=4.1.3
# 在虚拟环境R中安装 ipykernel
$ conda install -c conda-forge ipykernel
# 在虚拟环境R中安装 IRkernel
$ conda install -c conda-forge r-irkernel
# 在虚拟环境R中启动R
$ R
启动R
后界面如下:
R version 4.0.5 (2021-03-31) -- "Shake and Throw"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-conda-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
[Previously saved workspace restored]
# 配置 IRkernel ,'name'用于命名核心名称,'displayname'用于命名jupyter中展示的核心名称
> IRkernel::installspec(name = "ir_R", displayname = 'R')
从jupyter中删除R kernel
以删除上述ir_R
kernel为例
# 回到初始环境(在该环境中运行的 jupyter-lab)
$ conda activate base
# 列出jupyter中所有的kernel
$ jupyter kernelspec list
# 删除kernel 'ir_R'
$ jupyter kernelspec remove ir_R
参考:
jupyter 识别 python 虚拟环境
jupyter 识别 R 虚拟环境
Jupyter 删除不用的kernel
Tips:
When using R with conda, some people recommend to install all R packages via conda and not let R install packages. This is because R looks for header files (i.e. dev libraries) in canonical locations where these files are typically located when installed by the OS package manager. For more on R with conda, see this answer on the RStudio forum.