-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslow_print_v2.R
48 lines (44 loc) · 1.79 KB
/
slow_print_v2.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#' Slowly Print Text
#'
#' This function prints the characters of a given text string one by one,
#' with a specified delay between each character. The delay can be either fixed or random.
#'
#' @title Slowly Print Text
#' @description Prints the characters of the input text string one by one,
#' with a specified delay between each character. If the random parameter
#' is set to TRUE, the delay will be a random value between 0.0001 and 0.3 seconds.
#' Otherwise, the delay will be the value specified by the delay parameter.
#' @param text A string representing the text to be printed. Must be a non-NA string.
#' @param random A logical value indicating whether the delay between characters should be random.
#' Default is FALSE.
#' @param delay A numeric value representing the fixed delay between characters in seconds.
#' Default is 0.125. Must be a non-negative number.
#' @importFrom assertthat is.string noNA is.number
#' @importFrom stats runif
#' @return Invisible NULL. The function prints the text to the console.
#' @export slow_print_v2
#' @author Satoshi Kume
#' @examples
#' \dontrun{
#' slow_print_v2("Hello, World!")
#' slow_print_v2("Hello, World!", random = TRUE)
#' slow_print_v2("Hello, World!", delay = 0.1)
#' }
slow_print_v2 <- function(text, random = FALSE, delay = 0.125) {
assertthat::is.string(text)
assertthat::noNA(text)
assertthat::is.number(delay)
stopifnot(delay >= 0)
if (random) {
for (i in seq_along(strsplit(text, "")[[1]])) {
cat(strsplit(text, "")[[1]][i])
Sys.sleep(stats::runif(1, min = 0.0001, max = 0.4))
}
} else {
for (i in seq_along(strsplit(text, "")[[1]])) {
cat(strsplit(text, "")[[1]][i])
Sys.sleep(delay*stats::runif(1, min = 0.9, max = 1.1))
}
}
cat("\n") # Move to the next line after printing the text
}