I'd like to write a function that presents to the user a status message that shows something like the time, the percent complete, and the current status of a process. I can handle assembling the message, but I'd like to do something other than just print to the console and have it scroll up, one message after the other. I'd really like the message to change without scrolling like message()
and without any graphics. Is this possible with R?
Asked
Active
Viewed 1.7k times
31
![](../../users/profiles/428790.webp)
Joris Meys
- 98,937
- 27
- 203
- 258
![](../../users/profiles/37751.webp)
JD Long
- 55,115
- 51
- 188
- 278
-
12"This is R. There is no if. Only how." --Simon Blomberg – Joshua Ulrich Jan 21 '11 at 03:21
4 Answers
27
How about something like this?
for(i in 1:10) {
Sys.sleep(0.2)
# Dirk says using cat() like this is naughty ;-)
#cat(i,"\r")
# So you can use message() like this, thanks to Sharpie's
# comment to use appendLF=FALSE.
message(i,"\r",appendLF=FALSE)
flush.console()
}
![](../../users/profiles/271616.webp)
Joshua Ulrich
- 163,034
- 29
- 321
- 400
-
6Don't use `cat()`, use `message()` (which can be suppressed) as eg discussed on a R-bloggers post today arguing gripes with `cat()`. – Dirk Eddelbuettel Jan 21 '11 at 03:40
-
2@Dirk: It would have to be `message(..., appendLF = FALSE)`---otherwise scrolling messages would appear on the console which the OP specifically wanted to avoid. – Sharpie Jan 21 '11 at 03:44
-
@Dirk replacing `cat()` with `message()` adds a newline with each update. Do you have to do something extra to prevent the newline? FWIW, `txtProgressBar` uses `cat()`. – Joshua Ulrich Jan 21 '11 at 03:45
-
2Hmpf. Thanks guys. Guess not in that case *but as a general features it is lovely* to be able to suppress line noise, e.g. in scripts via littler or Rscript. – Dirk Eddelbuettel Jan 21 '11 at 03:50
-
@Dirk: Sorry, I had to update my comment after I RTFM. Totally possible to substitute `message()` for `cat()` if the proper arguments are provided. – Sharpie Jan 21 '11 at 03:55
-
1Glad someone read the post. Shameless self promotion: http://4dpiecharts.com/2011/01/20/bad-kitty/ – Richie Cotton Jan 21 '11 at 11:28
19
The utils
package contains the txtProgressBar
and functions for updating it which can be used to show the percent complete of a process.
See the up1
, up2
and up3
functions that are created during a call to txtProgressBar
for examples of how updates are handled without scrolling the console.
![](../../users/profiles/135870.webp)
Sharpie
- 15,975
- 4
- 41
- 45
-
2That's fantastic! You can also create and set your own update function via: `tpb – Joshua Ulrich Jan 21 '11 at 04:04
-
@Joshua: Good observation! When I was pondering creating a custom update function, I was envisioning having to copy and re-write `txtProgressBar()` in order to implement it. – Sharpie Jan 21 '11 at 04:07
-
There's also `plyr::progress_text`. It's a simple wrapper for `txtProgressBar`, though... – aL3xa Jan 21 '11 at 14:59
15
Here's some bling bling. From ?tcltk::tkProgressBar
.
pb <- tkProgressBar("test progress bar", "Some information in %",
0, 100, 50)
Sys.sleep(0.5)
u <- c(0, sort(runif(20, 0 ,100)), 100)
for(i in u) {
Sys.sleep(0.1)
info <- sprintf("%d%% done", round(i))
setTkProgressBar(pb, i, sprintf("test (%s)", info), info)
}
Sys.sleep(5)
close(pb)
![](../../users/profiles/322912.webp)
Roman Luštrik
- 64,404
- 24
- 143
- 187
-
1that's really cool to have for completeness, but my current need is for a text only solution. – JD Long Jan 21 '11 at 12:06
-
2I didn't read your question to the end when tcltk popped up. It was an impulse answer, for which I apologize. Perhaps you from the future will need it some day. – Roman Luštrik Jan 21 '11 at 18:03
-
8
There may be more elegant ways to do this, but this could do it:
test.message <- function() {
for (i in 1:9){
cat(i)
Sys.sleep(1)
cat("\b")
}
}
If you're automatically generating your message, you'll need to calculate how many \b characters to output to back up the correct amount, but that's pretty straightforward.
![](../../users/profiles/523044.webp)
Tyler
- 9,474
- 1
- 31
- 54