Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

suppress line/index numbers in R output

Tags:

r

console

rstudio

Can I systematically suppress the index of the first element in the line of the output in R's output in the console?

I am looking for an option to prettify the output, without having to type anything extra. I imagine that if such a feat is possible, it would be set up as an option in the .renviron file (or similar). An RStudio-specific answer would be acceptable. Apologies if I have overlooked something obvious in the settings (I would have expected that option to be in Preferences --> Code --> Display.

Currently the R console and RStudio consoles display:

1+1
[1] 2

I would like to see:

1+1
2

I know I can get the above with cat(1+1), but what I'm looking for is a systematic change in the display style. Something like the typical Python output (open a terminal, type Python followed by 1+1. I want that)

Edit: Another example. In RStudio, if I define x=1:5, it appears as int [1:5] 1 2 3 4 5 in the environment: that's informative and I don't mind it. But in the R console, it looks like [1] 1 2 3 4 5, which I do not find informative, especially when there are multiple lines.

I have personally got used to these numbers, as I imagine everyone has, but that doesn't make them right: (1) they serve no purpose: if you widen the console, the lines get wider and the line numbers change (if they marked the 80-character width, ok, maybe they would serve a purpose), (2) when I copy-paste output into lecture notes, these line numbers interfere with clarity and confuse the novice.

I have not found an answer to this question, which is surprising, so please let me know if I have missed it. The following question is related but not a duplicate https://stackoverflow.com/questions/3271939. Is there a duplicate I have missed?

Edit As pointed out by Adiel Loinger in the comments section, these are not "line numbers", as I had called them, but "the index of the first element of the line being printed in the console". Thanks for the correction. I have tried to edit my question accordingly.

like image 599
PatrickT Avatar asked Nov 05 '17 06:11

PatrickT


1 Answers

I believe the only way to do that is to modify the sources. R is open source, so that's not impossible, but it's not easy.

It's easier to change the print format for particular classes of objects. For example, if you don't like the way lm objects print, you can create your own print.lm method to do it yourself:

print.lm <- function (x, ...) 
{
    cat("My new version!")
}

Then

> lm(rnorm(10) ~ I(1:10))
My new version!

This doesn't work for things like 1+1, because for efficiency reasons, R always uses the internal version of the print method for auto-printing.

By the way, the printed indices do serve a purpose: if you print a long vector and wonder what the index is for some particular element, you only need to count from the start of the line, not from the start of the vector, to find it.

like image 134
user2554330 Avatar answered Oct 05 '22 12:10

user2554330