Here is a simple GNU R script print a single line:
#!/usr/bin/Rscript print("hello R")
where or execution output is:
$ ./script.R [1] "hello R"
The line numbers printed are actually row names for a given matrix. One way to avoid printing a line numbers is to use cat()
function:
#!/usr/bin/Rscript cat("hello R")
where the execution output is:
$ ./script.R hello R
However, cat()
function has its limitations and you may soon run into a trouble:
$ cat ./script.R #!/usr/bin/Rscript dataf = data.frame(col1=c(1,2,3),col2=c(4,5,6)) cat(dataf)
print()
has no trouble handling the above data, however, cat()
result in error:
$ ./script.R Error in cat(list(...), file, sep, fill, labels, append) : argument 1 (type 'list') cannot be handled by 'cat' Execution halted
The output of the next example script will produce multiple line numbers:
$ cat script.R
#!/usr/bin/Rscript
args <- commandArgs(TRUE)
commandArgs()
Next, we supply multiple arguments to generate multiple line output:
$ ./script.R 1 2 3 4 5 6 7 8 9 0 [1] "/usr/lib64/R/bin/exec/R" "--slave" [3] "--no-restore" "--file=./script.R" [5] "--args" "1" [7] "2" "3" [9] "4" "5" [11] "6" "7" [13] "8" "9" [15] "0"
The easiest solution to omit the above line numbers is to pipe the STDOUT to awk
command and remove the first column:
$ ./script.R 1 2 3 4 5 6 7 8 9 0 | awk '!($1="")' "/usr/lib64/R/bin/exec/R" "--slave" "--no-restore" "--file=./script.R" "--args" "1" "2" "3" "4" "5" "6" "7" "8" "9" "0"
Alternatively if the first space at the beginning of each line becomes a trouble we can pipe it to sed
command for removal:
$ ./script.R 1 2 3 4 5 6 7 8 9 0 | awk '!($1="")' | sed 's/ //' "/usr/lib64/R/bin/exec/R" "--slave" "--no-restore" "--file=./script.R" "--args" "1" "2" "3" "4" "5" "6" "7" "8" "9" "0"