Skip to content

Commit

Permalink
Merge pull request #16 from poissonconsulting/f-with-timer
Browse files Browse the repository at this point in the history
  • Loading branch information
joethorley authored Aug 7, 2024
2 parents 181b331 + 39944a4 commit 2a676ef
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 14 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export(tmr_round)
export(tmr_start)
export(tmr_stop)
export(tmr_timer)
export(with_timer)
import(hms)
1 change: 1 addition & 0 deletions R/hmstimer-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
# The following block is used by usethis to automatically manage
# roxygen namespace tags. Modify with care!
## usethis namespace: start
#' @import hms
## usethis namespace: end
NULL
7 changes: 6 additions & 1 deletion R/local-timer.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
#' Local Timer
#'
#' Called for the side effect of providing a message of the time
#' required to execute the rest of the function.
#'
#' @inheritParams params
#' @seealso [with_timer()]
#'
#' @return An invisible copy of the hmstimer object.
#' @export
#'
#' @examples
#' fun <- function() {
#' local_timer()
#' Sys.sleep(0.1)
#' 10
#' }
#' fun()
local_timer <- function(.local_envir = rlang::caller_env()) {
Expand Down
2 changes: 0 additions & 2 deletions R/namespace.R

This file was deleted.

3 changes: 2 additions & 1 deletion R/params.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#' Parameter Descriptions for hmstimer Functions
#'
#' @param .local_envir The environment to use for scoping
#' @param .local_envir The environment to use for scoping.
#' @param code Code to time.
#' @param digits A whole number of the number of decimal places.
#' @param seconds A non-negative numeric scalar of the initial number of seconds.
#' @param start A flag indicating whether to start the timer.
Expand Down
25 changes: 25 additions & 0 deletions R/with-timer.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#' With Timer
#'
#' @inheritParams params
#'
#' @return The result of executing the code.
#' @seealso [local_timer()]
#' @export
#'
#' @examples
#' fun <- function() {
#' Sys.sleep(0.1)
#' 10
#' }
#' with_timer(fun())
#'
#' with_timer({
#' for(i in 1:2) {
#' Sys.sleep(0.1)
#' }
#' 20
#' })
with_timer <- function(code) {
local_timer()
force(code)
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ reference:
desc: Miscellaneous functions
contents:
- local_timer
- with_timer
13 changes: 8 additions & 5 deletions man/local_timer.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/params.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions man/with_timer.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions tests/testthat/test-local-timer.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ test_that("local_timer", {
fun <- function(x) {
tmr <- local_timer()
Sys.sleep(0.1)
NULL
10
}
expect_message(fun(), "^00:00:00\\.\\d{3,3}\\s$")
expect_message(expect_identical(fun(), 10), "^00:00:00\\.\\d{3,3}\\s$")
})

test_that("test_local_timer()", {
test_local_timer <- function() {
local_timer()
Sys.sleep(0.1)
NULL
20
}

expect_message(test_local_timer(), "^00:00:00\\.\\d{3,3}\\s$")
expect_message(expect_identical(test_local_timer(), 20), "^00:00:00\\.\\d{3,3}\\s$")
})
17 changes: 17 additions & 0 deletions tests/testthat/test-with-timer.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
test_that("with_timer", {
fun <- function() {
Sys.sleep(0.1)
10
}
expect_message(expect_identical(with_timer(fun()), 10), "^00:00:00\\.\\d{3,3}\\s$")
})

test_that("with_timer", {
expect_message(expect_identical(with_timer({
for(i in 1:2) {
Sys.sleep(0.1)
}
20
}
), 20), "^00:00:00\\.\\d{3,3}\\s$")
})

0 comments on commit 2a676ef

Please sign in to comment.