Comments (5)
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.
Do you have any suggestions on how to use spinners with an arbitrary function? What I've tried so far:
- your
callr
package with r(fun, show = T)
drawbacks: function evaluated in another session which can be tricky is some cases and user can't set the spinner type or the output message. - the
future
package, but the output is only relayed (https://cran.r-project.org/web/packages/future/vignettes/future-2-output.html, HenrikBengtsson/future#141) - your
processx
package
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.
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.
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.
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)
- "Quiet" progress bars
- Add a new style that supports tooltips
- Fix Unicode 15.1 grapheme algorithm
- Issues with installing R package cli from source HOT 1
- Rstudio progress bar handlers don't work by default
- Handling `...` to create an ideal alert
- cli_vec: vec-trunc not working HOT 2
- Progress bar for `furrr::future_map` and `future.apply::future_lapply` functions HOT 1
- cli update from CRAN HOT 2
- cli::pb_current_name HOT 1
- FR: Add subtle style inline markup
- Add a "default" option to hyperlink detection
- Release cli 3.6.2
- Changing color for bullets
- Bad formatting when passing bare lubdridate dates HOT 4
- `cli_warning` and `cli_inform` don't expose individual bullet points in condition object. HOT 3
- Internal error with `ansi_strwrap`when string contains `\r\n\r` in `cli_abort()` message HOT 1
- Never seeing the first progress message from `cli_progress_along()`
- Suggestion: `cli_abort_if_not()` HOT 3
- More control over indentation for `cli_bullets()`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cli.