The main goal of the tally_counter is to provide a quick and easy way to monitor progress whilst iterating through a data frame, applying a function to each row at a time. This can be useful when the time taken for this step is sufficiently long enough to run the script in the background, coming back to the console at regular intervals to check the progress. To help with monitoring this progress, the number of rows remaining can be displayed in the console so that you can see at a glance how far the script has progressed and how far there is left to go.

tally_counter(data, ...)

Arguments

data

The data frame to be used in the iteration.

type

Which type of counter to use, either adding (add) or subtracting (default = subtract) counts.

Value

The data frame is returned invisibly so that the function can be used in a piped workflow.

Details

The counter is initiated by passing the data frame into this tally_counter function. This can be done within a pipeline containing the iteration step, for example in conjunction with the tidyverse suite of packages. It is used in conjunction with the click function which updates the counter, either decreasing or increasing the count by one. This up-to-date count can then be displayed in the console.

On initiation the counter is set to the count to the number of rows to be iterated through, counting downwards to zero during the iteration step. This behaviour can be changed through the type argument so that instead of counting downwards the counter counts upwards from zero to the total number of iterations.

The number of digits displayed by the counter is set to four to mimic the appearance of a real tally counter. However this number of digits is not fixed and will increase to accommodate increasing number of iterations, so for example above 9,999 iterations the number of digits increases to five, above 99,999 iterations six digits will be displayed and so on.

See also

Other tally counter methods: click()

Examples

suppressPackageStartupMessages({ library(store) suppressWarnings({ library(palmerpenguins) library(dplyr) library(stringr) library(purrr) }) }) penguin_stats <- function(...) { # get row data <- list(...) # get penguins stats species <- purrr::pluck(data, "species") island <- purrr::pluck(data, "island") body_mass_g <- purrr::pluck(data, "body_mass_g") sex <- purrr::pluck(data, "sex") year <- purrr::pluck(data, "year") # print penguin stats message(stringr::str_glue("{click()} : The body mass for the {sex} {species} penguin recorded in {year} on {island} island is {body_mass_g} grams")) } penguin_stats_slow <- slowly(penguin_stats, rate_delay(0.1)) penguins %>% slice_sample(n = 10) %>% arrange(desc(body_mass_g)) %>% tally_counter(type = "add") %>% pwalk(penguin_stats_slow)
#> 0000 tally counter
#> 0001 : The body mass for the male Gentoo penguin recorded in 2007 on Biscoe island is 5700 grams
#> 0002 : The body mass for the male Gentoo penguin recorded in 2009 on Biscoe island is 5400 grams
#> 0003 : The body mass for the male Gentoo penguin recorded in 2007 on Biscoe island is 5050 grams
#> 0004 : The body mass for the female Gentoo penguin recorded in 2009 on Biscoe island is 4625 grams
#> 0005 : The body mass for the male Adelie penguin recorded in 2007 on Dream island is 4400 grams
#> 0006 : The body mass for the male Adelie penguin recorded in 2009 on Biscoe island is 4275 grams
#> 0007 : The body mass for the NA Adelie penguin recorded in 2007 on Torgersen island is 4250 grams
#> 0008 : The body mass for the male Adelie penguin recorded in 2007 on Dream island is 4150 grams
#> 0009 : The body mass for the male Adelie penguin recorded in 2009 on Dream island is 3900 grams
#> 0010 : The body mass for the female Adelie penguin recorded in 2007 on Torgersen island is 3625 grams