如何创建“锁定”在R中测试事物的环境

发布于 2025-01-24 09:08:40 字数 611 浏览 5 评论 0原文

我知道这是在存在/不作为编码问题的边缘,但我仍然尝试运气。

当创建自己的R软件包时,我想在某个“模拟”用户环境(已安装的包装等方面的环境,而不是在R的Reg Global环境中)进行测试在安装一些默认软件包的位置,例如Tidyverse,然后我将在该环境中测试我的软件包。但是,当关闭项目并再次开放时,我希望将其恢复为我定义为默认值的任何内容,这意味着:

  • 我安装的其他软件包不应再
  • 进行Rprofile或Renviron文件的更改

。需要这样做是,在我的软件包中,我从Google中检索一个API键,然后将其存储在.renviron文件中。但是,一旦将其存储在那里,我将无法测试“首次使用”。我可以进入.renviron文件,删除API键并再次测试,但这很乏味。与已安装的软件包相同。

我以为renv软件包正是为此目的,但事实并非如此。 IE RENV只能跟踪我在项目中所做的任何设置,但它不允许我使用定义的某个默认环境。

我想念STH吗?在renv中(我看到它具有还原函数,但是只有在我将项目与git存储库一起使用时才有效)?定义这种固定环境的好方法是什么(除了一直到OS中的VM或售货亭设置之外)是什么?

I know this it at the edge of being/not being a coding question, but I still try my luck.

When creating my own R package, I want to test it in a certain "simulated" user environment (environment in the sense of packages installed etc., not in the R sense of e.g. global environment), i.e. I want to create an RStudio project where I install a few default packages, e.g. the tidyverse and then I will test my package in that environment. However, when closing the project and opening again, I want it to be restored to whatever I defined as the default, which means:

  • additional packages I installed should not be there anymore
  • changes to the Rprofile or Renviron file should be reverted

One example why I need to do this is that in my package I retrieve an API key from Google and store it in the .Renviron file. However, once it is stored there, I can't test "first time usage" anymore. I could go into the .Renviron file, delete the API key and test again, but that's very tedious. Same with installed packages.

I thought that the renv package is there for exactly this purpose, but that doesn't seem to be the case. I.e. renv just keeps track of whatever settings I do in my project, but it doesn't let me use a certain default environment I defined.

Am I missing sth. in renv (I saw that it has a revert function, but it only works if I'm using my project with a git repository)? What would be a good way to define such a fixed environment (except going all the way down to a VM or kiosk setup in the OS)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

庆幸我还是我 2025-01-31 09:08:40

由于您想隔离OS,因此需要创建虚拟机,这很容易重置。您可以使用Vagrant自动化此功能。如果将OS定义为EG仅仅是Linux发行版,则还可以使用不同的Docker容器来测试软件包。

对于后一种情况,请基于摇滚乐手并使用rstudio package manager要安装先决条件,例如特定日期的整理。您的包装文件位于目录中的Your_package可以复制到Docker映像:

FROM rocker/r-ver:4.1.0
RUN R -e "install.packages('tidyverse', repos='https://packagemanager.rstudio.com/all/__linux__/focal/2022-04-25+Y3JhbiwyOjQ1MjYyMTU7QjA3OUM2Q0I')"
COPY your_package your_package
CMD Rscript your_package/test.R

然后,您可以在隔离的环境中构建并运行此图像来测试您的软件包,在隔离环境中,只有Linux内核从主机重新使用。

Since you want to isolate the OS, you need to create virtual machines, which can be easily reset. You can use vagrant to automatize this. If you define OS e.g. as just Linux distribution, you can also use different docker containers for testing your package.

For the latter case, create a Dockerfile based on rocker and use RStudio package manager to install prerequisites, e.g. tidyverse of a particular date. Your package files located in directory your_package can be copied to the docker image:

FROM rocker/r-ver:4.1.0
RUN R -e "install.packages('tidyverse', repos='https://packagemanager.rstudio.com/all/__linux__/focal/2022-04-25+Y3JhbiwyOjQ1MjYyMTU7QjA3OUM2Q0I')"
COPY your_package your_package
CMD Rscript your_package/test.R

Then you can build and run this image to test your package in an isolated environment, where only the Linux kernel is reused from the host.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文