Git Product home page Git Product logo

Comments (5)

gaborcsardi avatar gaborcsardi commented on May 30, 2024 2

There is now a make_spinner() helper function (#65) that you can use to create a spinner, or you can write your own helper function based on that.

from cli.

samssann avatar samssann commented on May 30, 2024 1

Do you have any suggestions on how to use spinners with an arbitrary function? What I've tried so far:

spin <- function() {
  name = "test"
  sp <- cli::get_spinner("line")
  interval <- sp$interval/1000
  frames <- sp$frames
  cycles <- max(round(2.5/((length(frames) - 1) * interval)), 1)
  while(T) {
    for (i in 1:(length(frames) * cycles) - 1) {
      cat("\r", name, cli:::rpad(unclass(frames[i %% length(frames) + 1]), width = 10), sep = " ")
      Sys.sleep(interval)
    } 
  }
}
x <- r_process$new(r_process_options(func = spin))
processx::conn_set_stdout(x$get_output_connection())
Sys.sleep(10) # <your_long_running_function_here>
x$kill()

But I can't get the output attached to the current r session.

The ideal case would be if there would be a wrapper function called e.g. spinr and could use it by just inserting the function as an argument
randomFunction() %>% spinr('line')
and it would spin as long as function is running. The processx way (described above) could be valid if the output can be redirected to the current R session.

from cli.

gaborcsardi avatar gaborcsardi commented on May 30, 2024

You can just cat paste0("\r", spinner$frames[i]) to the screen, while the function is running. It is not good to do it too often, because cat has a cost.

You can also use the progress package with a custom token.

Yeah, an example would be nice, would you like to submit one with the progress package?

from cli.

gaborcsardi avatar gaborcsardi commented on May 30, 2024

and it would spin as long as function is running

that does not work, because R is single threaded. Currently the spinners are only useful if you write your own event loop, unfortunately.

from cli.

samssann avatar samssann commented on May 30, 2024

Here is one approach with future package (modified from HenrikBengtsson/future#141 (comment))

library(future)
library(cli)
plan(multisession)

spinr <- function(fun, name, spinner) {
  expr <- substitute(fun)
  # spinner configuration
  spinner <- match.arg(spinner, choices = list_spinners())
  sp <- get_spinner("line")
  interval <- sp$interval/1000
  frames <- sp$frames
  cycles <- max(round(2.5/((length(frames) - 1) * interval)), 1)
  # parallel execution
  future_fun <- future(expr, substitute = F)
  # spinner loop
  while (!resolved(future_fun)) {
    for (i in 1:(length(frames) * cycles) - 1) {
      cat("\r", name, cli:::rpad(unclass(frames[i %% length(frames) + 1]), width = 10), sep = " ")
      Sys.sleep(interval)
    } 
  }
  tryCatch(
    {
      out <- value(future_fun)
      cat("\r", name, cli::symbol$tick, sep = " ")
      cat("\n", file = stderr())
      out
    },
    error = function(e) {
      cat("\r", name, cli::symbol$cross, sep = " ")
      cat("\n", file = stderr())
      stop(e$message)
    }
  )
}

funOK <- function(x) {
  Sys.sleep(3)
  return(x)
}

funFAIL <- function(x) {
  Sys.sleep(3)
  stop("asd")
}

spinr(funOK("test"), "testing", "line")
spinr(funFAIL("test"), "testing", "line")

testing ✔         
[1] "test"

testing ✖         
Error in value[[3L]](cond) : asd

from cli.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.