R output control

I have this code:

x <- rnorm(10)
print(x)

returns as result:

[1] -0.67604293 -0.49002147  1.50190943  0.48438935 -0.17091949  0.39868189
[7] -0.57922386 -0.08172699 -0.82327067  0.07005629

I believe that R has a character limit per line or something else, and therefore splits the result into two lines. I am creating a service based on the output of R.

What should I do to get it on one line?

+3
source share
3 answers

The better it is to say R to output things in a way that is convenient for your wrapper; check string functions, such as paste(), sprintf()and output the result to the output via cat(); for example, placing numbers in a column might look like this:

x<-rnorm(10)
cat(paste(x,collapse="\n"),"\n")

which only outputs:

0.889105851072202
0.86920550247321
0.817785758768382
-0.0194490361401052
1.13386492568134
0.0786139738004322
0.7431631392675
0.93881227070957
0.534225167458455
1.08265812080696
+5
source
+6

Thanks to everyone for their answers. I want to post a new way to do this, which was the best for me, and maybe someone will need this link. When I create a web service, I will use phpSerialize to control the output so that I can unserialize in PHP.

library(phpSerialize)
x<-rnorm(10)
x = phpSerialize(x)
cat(x)
+2
source

Source: https://habr.com/ru/post/1777906/


All Articles