在不同的conda环境中安装R内核到Jupyter Notebook

5
我有一个包含所有jupyter notebook包的conda环境(称为jupyter_env)。在另一个conda环境中,我已经安装了R,包括r-irkernel(假设该环境名为R_env)。
对于Python内核,我可以轻松地将特定环境中的Python内核(例如pyth27)添加到不同环境中的jupyter安装中:(pyth27) > python -m ipykernel install --prefix=/path/to/jupyter/env --name "python27"
是否有类似于R内核的解决方案?到目前为止,我只能使用同一环境(R_env)中的jupyter安装运行R内核。
一种解决方法可能是使用nb-conda_kernels软件包。然而,我不确定它是否会始终添加所有可用内核以及是否可以指定应搜索哪些环境。
我的问题类似于https://github.com/jupyter/jupyter/issues/397。只是我不想使用基本环境来启动jupyter,而是要使用专用环境。
1个回答

0
https://github.com/IRkernel/IRkernel所述,r-ikernel包提供了类似于python -m ipykernel install的机制,可在R中运行:
R> IRkernel::installspec() 

如果要从Bash运行此程序,可以执行以下命令:

(R_env)> Rscript -e "IRkernel::installspec()"

现在是棘手的部分,由于Jupyter和R处于不同的环境中:根据https://github.com/IRkernel/IRkernel/issues/499IRkernel::installspec()需要jupyter-kernelspec命令。我已经测试了两种方法来提供它(在发出上述命令之前完成):

  • jupyter-kernelspec是Jupyter的一部分,因此在jupyter_env的文件树中,将其路径添加到PATH中(我发现最好添加到末尾,以免在Rscript调用期间干扰其他路径查找)

    (R_env)> export PATH="$PATH:</path/to/conda>/envs/jupyter_env/bin"
    
  • jupyter-kernelspec包含在jupyter_client conda包中,因此您可以执行以下操作

    (R_env)> conda install jupyter_client
    

    注意:这会安装许多依赖项,包括Python。

我选择了第一种方法,以保持R_env不受Python包的影响。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接