Reputation: 61933
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.
Upvotes: 5
Views: 3854
Reputation: 13932
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.
Upvotes: 5
Reputation: 11473
try cat("hello world\033[A")
or cat("hello world\033[nA")
where n is the number of rows you want to go up. These are the vt100 sequences for cursor up.
http://ascii-table.com/ansi-escape-sequences-vt-100.php
e.g. here is what happened on my screen
> hello again> > > cat("hello world\033[3Ahello again") hello world
You are correct thought that something like ncurses would be preferable. It is designed to be a higher level termio which is in turned designed to be a higher level than outputting control sequences like this. I don't know if R has any packages for them, though.
Maybe you can describe why you want to do this? There might be totally different options you have not considered.
Upvotes: 4