Sangcheol Choi
Sangcheol Choi

Reputation: 841

Create and execute an R script just like perl -e to script R in a shell such as bash?

I tend to use perl -e option to add Perl scripts in a shell script such as bash without creating a Perl script file.

perl -e '
while (<>) {
  print; 
}' < file

Is there a similar way of doing this for R? I want to write R scripts in a bash script but without creating an R script file.

One way is to use

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

Unlike perl -e, when using $, one has to prepend a backslash when to refer to a column of a table. In perl -e, we do not need to use \$ to refer to a perl variable.

Upvotes: 4

Views: 780

Answers (1)

johannes
johannes

Reputation: 14433

Yes there is. You can use Rscript:

Rscript -e 'print("hello world")'

Upvotes: 6

Related Questions