4

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.

Sangcheol Choi
  • 801
  • 1
  • 12
  • 16

1 Answers1

6

Yes there is. You can use Rscript:

Rscript -e 'print("hello world")'
johannes
  • 12,861
  • 5
  • 37
  • 49