Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to write on a previous line?

Tags:

r

I'm using R and I want to write over some text that has been put onto the console using cat. It's easy to do if the text is on the current line by using the backspace character (\b). Example:

> cat("A cat says ruff\b\b\b\bmeow")
A cat says meow> 

However if there is a line break I don't know how to go back to the previous line. Using cat with a backspace character doesn't seem to undo a line break.

> cat("A cat says ruff\n\b\b\b\b\bmeow")
A cat says ruff
meow> 

Alternatively if there is package that allows you allocate a section of the console and just modify the contents inside of there that would work for me as well. I've never used ncurses in linux directly but my understanding is that I want to have some functionality that is similar to what ncurses provides. Thanks for your time!

Edit: I'll add that I don't necessarily just need to modify just a certain section of the console - if the only solution is to allocate the entire console that would be fine for what I'm trying to do.

Edit 2: A solution for a tty console has been provided. I'm now interested in if it is possible to do this using RGui in Windows.

like image 672
Dason Avatar asked Dec 23 '11 18:12

Dason


1 Answers

You simply can't do that. In the special case of a tty output you can use escape sequences as shown above, but that is merely a side-effect of that particular (rare) case (and the behavior is actually undefined as far as R is concerned). None of the regular R GUIs supports editing other than with the pre-defined sequences documented in R (essentially just \b and \r) which won't go above the last line.

I should add that there are plenty of packages providing widgets (to create your own special output window) if that's what you want.

like image 193
Simon Urbanek Avatar answered Nov 10 '22 04:11

Simon Urbanek