Dockerfile中的多行Rscript [英] Multi-Line Rscript in Dockerfile

查看:78
本文介绍了Dockerfile中的多行Rscript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R构建一个docker映像,并且我希望能够以一种简洁,易于阅读的方式跨越多行来突破我的软件包安装步骤,但是bash似乎并不喜欢由于不知道结尾)的位置,所以采用这种方法.

I am trying to build a docker image with R, and I'd like to be able to break out my package install steps in a clean, easy to read, manner across multiple lines, but bash does not seem to like the approach due to not knowing where the ending ) is.

有没有办法使这行长代码分成多行?

Is there a way to make this long line of code split across multiple lines?

Rscript -e 'devtools::install_cran(c("tidytext","janitor","corrr","officer","devtools","pacman"))'

也许是这样的:

Rscript -e 'devtools::install_cran(c("tidytext","janitor",
                              "corrr","officer","devtools","pacman"))'

这可能与Rscript有关吗?我尝试在每行的末尾使用\,但仍然无法正常工作.

Is this possible to do with Rscript? I have tried using a \ at the end of each line, and it still does not work.

我知道install2.r可以逐行列出软件包,但我希望有一个软件包向量,如果可能的话,可以传递给 devtools :: install_cran .我已经看到其他人只是通过Rscript调用它们来简单地引用他们的R脚本,但是我想在Dockerfile中看到我的所有安装步骤,而不是在容器中复制和运行外部R脚本.感谢您的帮助.

I understand install2.r can list out the packages line by line, but I would like to have a vector of packages to pass to devtools::install_cran if possible. I have seen others simply refer to their R script by simply calling it via Rscript, but I would like to see all my installation steps inside my Dockerfile, and not copy and run an external R script in my container. Thanks for your help.

Rscript test.R

推荐答案

BASH会将换行符解释为命令的结尾.

BASH will interpret a newline as the end of the command.

在BASH(我假设您正在使用)中,反斜杠后跟换行符被解释为该行的延续.除非它在单引号内!

In BASH (which I'm assuming you're using), a backslash followed by a newline is interpreted as a continuation of the line. Except when it is inside single quotes!

所以...

Rscript -e 'devtools::install_cran(c("tidytext","janitor",
                          "corrr","officer","devtools","pacman"))'

将被解释为两个命令...

will be interpreted as two commands...

Rscript -e 'devtools::install_cran(c("tidytext","janitor",

"corrr","officer","devtools","pacman"))'

两者都不是正确的格式.

Neither of which are well formed.

此外,BASH中的单引号字符串将无法处理转义.他们只是假设您的文字是文字.因此,您无法在BASH中的单引号字符串内继续一行.

Additionally, single quoted strings in BASH will not handle escapes. They simply assume your text is literal. So you cannot continue a line within single quoted strings in BASH.

最重要的是,如果要在BASH中用引号引起来的字符串内继续,则必须使用双引号引起来的字符串.您的选择如下:

The bottom line is that if you want continuation within a quoted string in BASH, you must use double quoted strings. Your options are as follows:

Rscript -e "devtools::install_cran(c('tidytext','janitor', \  
                'corrr','officer','devtools','pacman'))"

在BASH中使用双引号,在R或...中使用单引号.

using double quotes in BASH and single quotes in R or...

Rscript -e "devtools::install_cran(c(\"tidytext\",\"janitor\", \    
                \"corrr\",\"officer\",\"devtools\",\"pacman\"))"

在两者中都使用双引号.

using double quotes in both.

这篇关于Dockerfile中的多行Rscript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆