Created
December 1, 2015 19:27
-
-
Save artemklevtsov/ac55347e5ded0cf9769f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
timer <- R6::R6Class( | |
classname = "timer", | |
public = list( | |
initialize = function(started.at = Sys.time()) { | |
private$started.at <- started.at | |
}, | |
elapsed = function() { | |
if (!private$suspended) | |
secs <- as.double(difftime(Sys.time(), private$started.at, units = "secs")) | |
else | |
secs <- as.double(difftime(private$suspended.at[length(private$suspended.at)], private$started.at, units = "secs")) | |
secs - private$suspend_time() | |
}, | |
suspend = function() { | |
if (!private$suspended) { | |
private$suspended.at <- .POSIXct(c(private$suspended.at, Sys.time())) | |
private$suspended <- TRUE | |
} | |
}, | |
resume = function() { | |
if (private$suspended) { | |
private$resumed.at <- .POSIXct(c(private$resumed.at, Sys.time())) | |
private$suspended <- FALSE | |
} | |
}, | |
status = function() { | |
ifelse(private$suspended, "inactive", "active") | |
}, | |
watch = function(interval = 0.01) { | |
while (TRUE) { | |
cat("\r", self$elapsed(), sep = "") | |
Sys.sleep(interval) | |
} | |
}, | |
reset = function() { | |
private$started.at <- Sys.time() | |
private$suspended <- FALSE | |
private$suspended.at <- NULL | |
private$resumed.at <- NULL | |
}, | |
print = function() { | |
cat("Timer:\n") | |
cat(sprintf(" Started at: %s\n", format(private$started.at))) | |
cat(sprintf(" Status: %s\n", self$status())) | |
cat(sprintf(ngettext(private$suspend_time(), | |
" Suspend time: %1.3f sec\n", | |
" Suspend time: %1.3f secs\n"), private$suspend_time())) | |
cat(sprintf(" Elapsed time: %s\n", private$pretty_time())) | |
invisible(self) | |
} | |
), | |
private = list( | |
started.at = NULL, | |
suspended = FALSE, | |
suspended.at = NULL, | |
resumed.at = NULL, | |
suspend_time = function() { | |
if (is.null(private$resumed.at)) | |
res <- 0 | |
if (!is.null(private$resumed.at)) { | |
if (!private$suspended) | |
res <- sum(difftime(private$resumed.at, private$suspended.at, units = "secs")) | |
else | |
res <- sum(difftime(private$resumed.at, private$suspended.at[-length(private$suspended.at)], units = "secs")) | |
} | |
as.double(res) | |
}, | |
pretty_time = function() { | |
secs <- self$elapsed() | |
mins <- secs %/% 60 | |
hrs <- mins %/% 60 | |
days <- hrs %/% 24 | |
mins = mins - hrs * 60 | |
hrs = hrs - 24 * days | |
if (secs < 60L) | |
res <- sprintf(ngettext(secs, "%1.3f sec", "%1.3f secs"), secs) | |
else | |
res <- sprintf("%02.0f:%02.0f:%02.0f", hrs, mins, secs %% 60) | |
if (days >= 1L) | |
res <- sprintf("%d %s %s", as.integer(days), ngettext(as.integer(days), "day", "days"), res) | |
return(res) | |
} | |
) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
timerApp <- shiny::shinyApp( | |
ui = list( | |
titlePanel("Название приложения"), | |
sidebarLayout( | |
sidebarPanel( "sidebar panel"), | |
mainPanel("main panel") | |
) | |
), | |
server = function(input, output, session) { | |
timer <- timer$new() | |
shiny::observeEvent(input$suspend, { | |
timer$suspend() | |
}) | |
shiny::observeEvent(input$resume, { | |
timer$resume() | |
}) | |
shiny::observeEvent(input$reset, { | |
timer$reset() | |
}) | |
output$timer <- shiny::renderPrint({ | |
shiny::invalidateLater(1000, session) | |
timer$print() | |
}) | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment