How to pass Rscript -e a multiline string?

戏子无情 提交于 2019-12-21 07:00:08

问题


Is there a way to provide the code to Rscript -e in multiple lines?

This is possible in vanilla R

R --vanilla <<code
a <- "hello\n"
cat(a)
code

But using Rscript I get two different things depending on the R version.

# R 3.0.2 gives two ignores
Rscript -e '
quote> a <- 3+3
quote> cat(a, "\n")
quote> '
# ARGUMENT 'cat(a,~+~"' __ignored__
# ARGUMENT '")' __ignored__

Rscript -e 'a <- 3+3;cat(a, "\n")'
# ARGUMENT '")' __ignored__

# R 2.15.3 gives an ignore for the multiline, but it works with semicolons
Rscript -e '
quote> a <- 3+3
quote> cat(a, "\n")
quote> '
# ARGUMENT 'cat(a,~+~"\n")' __ignored__

Rscript -e 'a <- 3+3;cat(a, "\n")'
6

I'm clearly using the wrong syntax. What is the proper way to do this?


回答1:


Update: I think the problem was spacing and quotes. This worked (on windows):

Rscript -e "a <- 3+3; cat(a,'\n')"
6

On Mac, you have to escape the escape character:

Rscript -e 'a <- 3+3; cat(a,"\\n")'

You can also put each expression separately.

Rscript -e "a <- 3+3" -e "cat(a)"


来源:https://stackoverflow.com/questions/21733137/how-to-pass-rscript-e-a-multiline-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!