Useful mainly in a script file to time, then log, each step (or process) in the file.

timer(
  process,
  notes = NULL,
  file = NULL,
  name = NULL,
  log = NULL,
  time_df = NULL,
  write_log = grepl("end", process)
)

Arguments

process

Character. Name of the process being timed.

notes

Character. Any notes associated with the process.

file

Character. Name of the (script) file.

name

Character. Might be the same as process, or an element over which process is being run. e.g. name could be a species name where the same function is being run over many different species.

log

Character. Path to write the log file.

time_df

Object name or null for the initiation of a timer.

write_log

Write the log after this process. Default is only where process contains 'end'.

Value

A dataframe (time_df), possibly with an additional row for the current process. If write_log then log file is written.

Details

Timer can be re-started by including 'start' in a process.

Examples

time_df <- timer("start script", file = "file 01", name = "example", log = "example.log")
Sys.sleep(1)
time_df <- timer("process 01", time_df = time_df)
Sys.sleep(1)
time_df
#> # A tibble: 2 × 7
#>   name    file    process      time                notes elapsed       log      
#>   <chr>   <chr>   <chr>        <dttm>              <chr> <time>        <chr>    
#> 1 example file 01 start script 2024-09-09 15:47:59 NA    00'00.000000" example.…
#> 2 example file 01 process 01   2024-09-09 15:48:00 NA    00'01.043116" example.…
time_df <- timer("end script", time_df = time_df) # log.log written
Sys.sleep(1)
time_df <- timer("start script", file = "file 02", time_df = time_df)
Sys.sleep(1)
time_df <- timer("process 01", time_df = time_df, write_log = TRUE) # log.log written
time_df <- timer("end script", time_df = time_df) # log.log written

# clean up
rm(time_df)
unlink("example.log")