tally_counter: Count the number of remaining interactions

View source: R/tally.R

tally_counterR Documentation

Count the number of remaining interactions

Description

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.

Usage

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.

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.

Value

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

See Also

Other tally counter methods: click()

Examples

suppressPackageStartupMessages({
  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)

gcfrench/store documentation built on May 17, 2024, 5:52 p.m.