简体   繁体   English

在正在运行的 docker 容器上一一执行许多 R 命令

[英]Executing many R commands one by one on a running docker container

I have a running container with RStudio image and my own preinstalled package.我有一个正在运行的容器,其中包含 RStudio 映像和我自己预装的 package。 My task is to call different functions from the package one by one - an output from one function is the input to other functions.我的任务是从 package 一个一个地调用不同的函数 - 一个 output 来自一个 function 是其他函数的输入。 I don't want to do it this way: fun1(x) %>% fun2(x) %>%... %>% funN(x) .我不想这样做: fun1(x) %>% fun2(x) %>%... %>% funN(x) I want it to work like this:我希望它像这样工作:

out1 <- fun1(x)
out2 <- fun2(out1)
...
funN(outN-1)

The reason I'd it like to be done this way is that I'd like to call these functions as separate docker exec commands against the running container.我希望这样做的原因是我想将这些函数称为针对正在运行的容器的单独docker exec命令。 I'm planning to convert these commands into separate Jenkins jobs.我打算将这些命令转换为单独的 Jenkins 作业。

I tried docker exec... commands but failed on the second step.我尝试docker exec...命令,但在第二步失败。 In the first step, the fun1 function creates an R object out1 .第一步, fun1 function 创建一个 R object out1 But the out1 is not available for the second docker exec call, ie this does not work:但是out1不适用于第二个docker exec调用,即这不起作用:

docker run --name=my_container -d my_image (ok)
docker exec -it my_container Rscript main.R (ok)
docker exec -it my_container R "-e my_package::fun2(out1)" (not ok - object out1 not found)

where main.R is something like this:其中main.R是这样的:

library(my_package)
out1 <- my_package::fun1()

In other words, the next docker exec is a continuation of the previous one - all the objects living in the R workspace after the previous step is finished should be available for functions in the next step.换句话说,下一个docker exec是上一个 exec 的延续 - 上一步完成后 R 工作区中的所有对象都应该可用于下一步中的函数。 Is it possible?可能吗? I'd like to avoid putting all the functions (steps) into one R script.我想避免将所有功能(步骤)放入一个 R 脚本中。 Any help would be very appreciated.任何帮助将不胜感激。

You can try this:你可以试试这个:

    docker run -it rocker/rstudio-stable:3.5.1 echo 'set.seed(123); 
    out1 <- base::sample(1:100, 3);
    print(out1);
    out2 <- out1 * 3;
    print(out2)'  | R --no-save 

Output: Output:

> set.seed(123);
                 >     out1 <- base::sample(1:100, 3);
                                                      >     print(out1);
                                                                        [1] 29 79 41
                                                                                    >     out2 <- out1 * 3;
                                                                                                           >     print(out2)
                                                                                                                            [1]  87 237 123
                                                                                                                                           >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM