Unexpected text wrapping behaviour of the RStudio console


I'm wondering if anyone knows why the RStudio console is having issues with properly wrapping long strings of text of various characters.

Let me show by example: using the cli::console_width() function, we can get the "exact" width in characters of the console. Actually, you have to add 3 if you're not counting the default > that's displayed.

My console has as width of 89 (89+3) and I'm generating one long string of 5x89 characters and using cat to display it in the console. Then I do the same thing, but I paste 5 strings of 89 together with a linebreak (\n) and then use cat

#Console width = 89

sample(c("*", "."), (cli::console_width()+3)*5, 
replace = T) %>% paste0(collapse = "") %>% cat("\n")

sapply(1:5, function(x){
  sample(c("*", "."), (cli::console_width()+3), 
       replace = T) %>% paste0(collapse = "")}) %>% 
  paste(collapse = "\n") %>% cat()

OUTPUT in console

Note how in the first instance, the wrapping is not correct and is generating incomplete lines with overflow into a 6th line. The number of characters in both is exactly the same, but the wrapping goes wrong.

I have tested this in other consoles (not RStudio) and this problem never occurs. Interestingly, if the characters are all the same, the wrapping issue is not present:

The characters are in a monospace font (you can see that they are aligned even when different) so this should not be the explanation...

Any ideas?


if you run the code you provide here, and resize the console with your mouse left and right, you will see that Rstudio console is dynamically wrapping your text if needed. I.e. I don't think its an R issue so much as an R Studio issue.

p.s. can open RGui, and try the same there. It doesn't attempt to dynamically wrap the console view, in contrast


Yes that's indeed what I mean. It's not an R issue but RStudio, as all other consoles do the wrapping correctly. I'm ignoring the R GUI for this discussion indeed because it doesn't even attempt to wrap :stuck_out_tongue:

It's just weird this issue is not happening when I use any other terminal window running an R script that produces text that's longer and needs to be wrapped, hence my post here...


Raise it ?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.