Reputation: 21377
If I put a data frame to the console directly, it looks nice:
> head(datasets::mtcars, 4)
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
How do I get the same nice output, but through message
?
> message(head(datasets::mtcars, 4))
c(21, 21, 22.8, 21.4)c(6, 6, 4, 6)c(160, 160, 108, 258)c(110, 110, 93, 110)c(3.9, 3.9, 3.85, 3.08)c(2.62, 2.875, 2.32, 3.215)c(16.46, 17.02, 18.61, 19.44)c(0, 0, 1, 1)c(1, 1, 1, 0)c(4, 4, 4, 3)c(4, 4, 1, 1)
This question looks similar, but didn't help me.
Upvotes: 2
Views: 316
Reputation: 886948
We can use paste
to create a vector of values and pass it on to message
message(do.call(paste, c(head(datasets::mtcars, 4), collapse="\n")))
-output
# 21 6 160 110 3.9 2.62 16.46 0 1 4 4
#21 6 160 110 3.9 2.875 17.02 0 1 4 4
#22.8 4 108 93 3.85 2.32 18.61 1 1 4 1
#21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Inorder to get the row names attributes, we could use capture.output
message(paste(capture.output(head(datasets::mtcars, 4)), collapse="\n"))
-output
# mpg cyl disp hp drat wt qsec vs am gear carb
#Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Upvotes: 3