1

I am trying to create an (ascii) progress wheel that runs while any random function runs. For example, the sf packages st_intersection() runs painfully slow and I would like an indicator showing that R is still working.

It should look something like this:

while (T) {
  for(c in c("-", "\\", "|", "/")){
    Sys.sleep(0.1)
    cat("\r", c) 
  }
}

I can't figure out how to make this run while the "target"-function is progressing. Also putting the system to sleep is not exactly helpful for runtimes. My first idea was to let the wheel run while R is busy but I couldn't figure out how to do it (although it is possible as is evident by the "stop" button used to interrupt a running script).

I found a few answers for Shiny packages but nothing (easily) adaptable to base R.

camille
  • 16,432
  • 18
  • 38
  • 60
D.J
  • 1,180
  • 1
  • 8
  • 17
  • You could use the [progress](https://github.com/r-lib/progress) package directly if a bar would do rather than a wheel. If not, I would check out the [source code](https://github.com/r-lib/progress/blob/main/R/progress.R) for their progress bar to amend to your use case. – caldwellst Nov 29 '21 at 09:42
  • 2
    @caldwellst I don't think "progress" package would work. As the st_interaction is not looping, it is a single function that is taking too long to run. – zx8754 Nov 29 '21 at 09:44
  • 2
    sf has an open feature request issue at GitHub: https://github.com/r-spatial/sf/issues/1564 – zx8754 Nov 29 '21 at 09:45
  • @caldwellst as zx8754 says, there is (at the surface level) no iteration happening - which makes it difficult to track the exact progress. if possible, i'd definitely prefer a progress bar. as far as i could tell there is no progress bar implemented as of yet. furthermore i'd like to be able to use the wheel for any function rather than just `sf` (although right now it is the one i work with most) – D.J Nov 29 '21 at 09:52
  • 1
    Yeah, good point @zx8754. Running through the source code of `st_intersection()`, I am not sure what all would be possible, as it is eventually based on the use of C++ code depending on if its [spherical](https://github.com/r-spatial/s2/blob/main/R/RcppExports.R) or [geographic](https://github.com/r-spatial/sf/blob/5dde39c8261dc6b202e6cde9d41ad3bf0f46aa3a/R/RcppExports.R) coordinates. – caldwellst Nov 29 '21 at 09:56
  • 2
    If your goal is just an indication that "R is still working", how about just monitoring the cpu usage of the R process? How you do this depends on your platform. All OS's have some kind of system monitor app that can do this. – dww Nov 29 '21 at 13:06
  • @dww you know i thought about this but i often have multiple r sessions as well as some gis-software running and can not always be sure what change in cpu use (or ram use - which was my 2nd idea) corresponds to which process. – D.J Nov 29 '21 at 13:23
  • Also, neither cpu/ram usage reliably reduce if r gets stuck. – D.J Nov 29 '21 at 13:38

0 Answers0