短版
将renv、venv和jupyterlab与IRkernel一起使用的简单而优雅的方法是什么?特别是,如何从不在根目录的jupyter笔记本中自动激活renv?
长版
我采用的是"polyglot“数据科学风格,这意味着同时使用python和R。现在venv很棒,renv很棒,jupyterlab也很棒,所以我想找出什么是将它们结合在一起的好方法。
我几乎有它,所以可能几个提示就足以完成这个设置。我就在这里。
系统
从一个干净的操作系统开始,并安装系统级的要求:r+ renv和Python + venv。例如,在Ubuntu上,它近似于这样:
# R
sudo apt install r-base
sudo R -e "install.packages('renv')"
# Python
sudo apt install python3.8
sudo apt install python3.8-venv项目
现在使用两个文件创建一个基本的项目jupyrenv:
jupyrenv/
├── DESCRIPTION
└── requirements.txtDESCRIPTION包含R依赖项:
Suggests:
IRkernel,
fortunesrequirements.txt包含python依赖项:
jupyterlab创建虚拟环境并安装依赖关系(order,R必须遵循python):
# Python
python3.8 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
# R
R -e "renv::init(bare=TRUE)"
R -e "renv::install()"
R -e "IRkernel::installspec()"到目前为止非常整洁!
木星
从命令行启动jupyter并高兴,它起作用了!
jupyter-lab

有什么不喜欢的?
不幸的是,如果我创建一个文件夹(比如notebooks)并在那里启动一个R笔记本,它就不能工作:(
[I 2022-02-23 19:07:24.628 ServerApp] Creating new directory in
[I 2022-02-23 19:07:31.159 ServerApp] Creating new notebook in /notebooks
[I 2022-02-23 19:07:31.416 ServerApp] Kernel started: 0aa2c276-18dc-4511-b308-e78234fa71d4
Error in loadNamespace(name) : there is no package called ‘IRkernel’
Calls: :: ... loadNamespace -> withRestarts -> withOneRestart -> doWithOneRestart
Execution halted试图修复
renv似乎不是从子文件夹中使用的,所以我们需要提示R进程才能使用它。我尝试添加一个额外的.Rprofile文件-- notebooks子文件夹:
jupyrenv/
├── DESCRIPTION
├── requirements.txt
├── renv
├── venv
├── notebooks
│ ├── .Rprofile
│ └── Untitled.ipynb
├── .Rprofile
└── Untitled.ipynb内容如下:
.Rprofile
source("../renv/activate.R")这是可行的,但并不是真的。首先,当尝试在notebooks目录中创建一个R笔记本时,它会创建一个新的renv
[I 2022-02-23 19:22:28.986 ServerApp] Creating new notebook in /notebooks
[I 2022-02-23 19:22:29.298 ServerApp] Kernel started: b40a88b3-b0bb-4839-af45-85811ec3073c
# Bootstrapping renv 0.15.2 --------------------------------------------------
* Downloading renv 0.15.2 ... OK (downloaded source)
* Installing renv 0.15.2 ... Done!
* Successfully installed and loaded renv 0.15.2.然后,jupyter的实例可以工作,我可以使用它,但是如果重新启动,它就会停止工作,并返回到丢失的IRkernel错误:
[I 2022-02-23 19:24:58.912 ServerApp] Kernel started: 822d9372-47fd-43f5-8ac7-77895ef124dc
Error in loadNamespace(name) : there is no package called ‘IRkernel’
Calls: :: ... loadNamespace -> withRestarts -> withOneRestart -> doWithOneRestart我遗漏了什么?
发布于 2022-02-24 20:06:08
我把这个问题作为问题在renv github中打开,维护人员好心地提供了一个解决办法。notebooks/.Rprofile的内容应如下:
owd <- setwd(".."); source("renv/activate.R"); setwd(owd)它混合在一起!
https://stackoverflow.com/questions/71242328
复制相似问题