如何传递 Rscript -e 多行字符串? [英] How to pass Rscript -e a multiline string?

查看:43
本文介绍了如何传递 Rscript -e 多行字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在多行中为 Rscript -e 提供代码?

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

这在 vanilla R 中是可能的

This is possible in vanilla R

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

但是使用 Rscript 我得到两个不同的东西,具体取决于 R 版本.

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?

推荐答案

更新:我认为问题在于间距和引号.这有效(在 Windows 上):

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

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

在 Mac 上,您必须转义转义字符:

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)"

这篇关于如何传递 Rscript -e 多行字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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