Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R CMD BATCH - output in terminal

Just learning R and I thought it would be great to use it in batch mode in the unix terminal instead of writing in the R terminal.

So I decided to write test.r

    x <- 2
    print(x)

then in terminal I did

    R CMD BATCH test.r

it runs, But outputs a test.r.Rout file. I can get it to output to say a text file by running R CMD BATCH test.r out.txt.

Question is, is it possible to print the output to the terminal?

like image 793
Blakedallen Avatar asked Jan 21 '13 04:01

Blakedallen


People also ask

What is batch mode in r?

R provides a way to run a large set of commands in sequence and save the results to a file. This is called batch mode. One way to run R in batch mode is from the system command line (not the R console). By running R from the system command line, it's possible to run a set of commands without starting R.

What does R mean in CMD?

R is an interpreted programming language. This means that R will interpret each line of code as it is entered and, if it is valid, R will execute it, returning the result in the command console.


2 Answers

Sebastian-C posted:

    Rscript test.r

This worked in the terminal and produced the desired output

Thanks Sebastian-C

like image 82
Blakedallen Avatar answered Sep 22 '22 08:09

Blakedallen


A somewhat late reaction, but the other day I was looking for the answer to exactly the question Blakedallen posted. Here's my solution:

mkfifo fifo   # create a named pipe
cat fifo &    # show everything to the terminal 
              # (or do 'cat fifo' in another terminal)
R CMD BATCH input # 'input' contains your R commands

Works like a charm :-)

The 'fifo' named pipe can be used again for any future R CMD BATCH commands you may want to execute. After the 'R CMD BATCH' command the 'cat' command also ends.

like image 21
Frank B. Brokken Avatar answered Sep 20 '22 08:09

Frank B. Brokken