0

I sometimes do (nested) for loops that take a while, so I like putting a message with the index. Normally, I do this:

for (i in 1:1000) {
  cat(paste0(i, " "))
}

But I've tried any of these:

for (i in 1:1000) {
  print(i)
}
for (i in 1:1000) {
  cat(paste(i, " ")) # or "\n", doesn't matter
}
for (i in 1:1000) {
  message(i)
}

However, this clearly produces text continuously (in this example, the messages would go by very quickly, but as I said, I often do nested for loops, which take considerably longer). However, I'd like to do a dynamic message, with a single number on my console which keeps changing (the index). I suppose something like this is possible because when you install a package (or when using gganimate), there is that line that keeps changing to = signs, indicating progress (that's next-level stuff, but should have the same idea behind it).

I don't know how to do this. My guess is there's something of the same context as \n, like \s (I know that doesn't exist, but it's just do give an idea), or maybe there is a third function (or something related to message(), which I don't fully understand) other than print(), cat() and message().

Érico Patto
  • 1,015
  • 4
  • 18
  • Possible duplicate: [R - text progress bar in for loop](https://stackoverflow.com/q/26919787/903061). The [progress package](https://github.com/r-lib/progress) can help. – Gregor Thomas Nov 23 '20 at 15:14

1 Answers1

1

If it is OK to clear the console each time, you can:

for (i in 1:1000) {
    for(j in 1:1000){
        for(k in 1:1000){
            cat(paste('\014',i, j, k))
        }
    }
}
Eric Krantz
  • 1,854
  • 15
  • 25