Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Imports:
rsvg,
jsonlite,
rlang,
glue,
grDevices,
svglite
Suggests:
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import(rsvg)
importFrom(ggplot2,ggsave)
importFrom(ggplot2,set_last_plot)
importFrom(gifski,gifski)
importFrom(glue,glue)
importFrom(grDevices,dev.copy)
importFrom(grDevices,dev.cur)
importFrom(grDevices,dev.list)
Expand Down
32 changes: 25 additions & 7 deletions R/gg_record.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
#' @param device Device to use. Can either be a device function (e.g. png()), or
#' one of "png", "pdf", "jpeg", "bmp", "tiff", "emf", "svg", "eps", "ps".
#' @param device_ext file extension to use for images created. Does not usually need to be populated manually.
#' @return Used initialize recording, nothing returned
#' @param filename_pattern Pattern to use when naming the interim files. Must use one of the
#' reserved camcorder iterators: {timestamp},{i}
#' @inheritParams ggplot2::ggsave
#'
#'
#' @return Used initialize recording, nothing returned
#'
#' @importFrom ggplot2 ggsave
#'
#' @examples
Expand Down Expand Up @@ -40,7 +44,8 @@ gg_record <- function(dir = NULL,
dpi = 300,
limitsize = TRUE,
device_ext = NULL,
bg = NULL
bg = NULL,
filename_pattern = "{timestamp}"
){

if (is.null(dir)) {
Expand All @@ -67,22 +72,35 @@ gg_record <- function(dir = NULL,
device_ext <- derive_ext(device)
}

filename_pattern <- verify_filename_pattern(filename_pattern)

units <- match.arg(units)

if (!dir.exists(dir)) {
dir.create(dir, recursive = TRUE)
} else{
if (length(list.files(dir, pattern = paste0("[.]", device_ext, "$"))) > 1) {
device_files <-
list.files(dir,
pattern = paste0(
"^",
cleanup_filename_pattern_to_regex(filename_pattern),
"[.]",
device_ext,
"$"
))
if (length(device_files) > 1) {
warning(
"Writing to a folder that already exists. gg_playback may use more files than intended!"
)
}
}

GG_RECORDING_ENV$recording_dir <- dir
GG_RECORDING_ENV$device <- device
GG_RECORDING_ENV$device_ext <- device_ext
GG_RECORDING_ENV$is_temp_dir <- is_temp_dir

GG_RECORDING_ENV$recording_dir <- dir
GG_RECORDING_ENV$device <- device
GG_RECORDING_ENV$device_ext <- device_ext
GG_RECORDING_ENV$is_temp_dir <- is_temp_dir
GG_RECORDING_ENV$filename_pattern <- filename_pattern

GG_RECORDING_ENV$image_width <- width
GG_RECORDING_ENV$image_height <- height
Expand Down
7 changes: 5 additions & 2 deletions R/preview.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ preview_film <- function(){
wait = FALSE)

} else{


image_viewer_html <-
file.path(tempdir(), "preview.html")

Expand All @@ -37,11 +39,12 @@ get_file_records <- function(full_path = FALSE){

file_preview_ext <- paste0("[.]", GG_RECORDING_ENV$device_ext, "$")

file_preview_format <- "\\d{4}_\\d{2}_\\d{2}_\\d{2}_\\d{2}_\\d{2}[.]\\d+"
filename_pattern <- GG_RECORDING_ENV$filename_pattern
filename_pattern <- cleanup_filename_pattern_to_regex(filename_pattern)

list.files(
path = GG_RECORDING_ENV$recording_dir,
pattern = paste0("^",file_preview_format,file_preview_ext),
pattern = paste0("^",filename_pattern,file_preview_ext),
full.names = full_path
)

Expand Down
22 changes: 4 additions & 18 deletions R/recording.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@
#' @param ... allow for traditionally pass arguments to printing that are ignored
#'
#' @importFrom ggplot2 set_last_plot
#' @importFrom glue glue
#'
#' @noRd
#'
record_ggplot <- function(x, ...) {

plot_file <-
file.path(GG_RECORDING_ENV$recording_dir, paste0(
format(Sys.time(), "%Y_%m_%d_%H_%M_%OS6"),
".",
GG_RECORDING_ENV$device_ext
))
plot_file <- camcorder_plot_file_path()

suppressMessages({
ggsave(
Expand All @@ -44,12 +40,7 @@ record_ggplot <- function(x, ...) {
#' @importFrom utils capture.output
record_patchwork <- function(x,...) {

plot_file <-
file.path(GG_RECORDING_ENV$recording_dir, paste0(
format(Sys.time(), "%Y_%m_%d_%H_%M_%OS6"),
".",
GG_RECORDING_ENV$device_ext
))
plot_file <- camcorder_plot_file_path()

registerS3method(
genname = "print",
Expand Down Expand Up @@ -118,12 +109,7 @@ record_patchwork <- function(x,...) {
#' @export
record_polaroid <- function(){

plot_file <-
file.path(GG_RECORDING_ENV$recording_dir, paste0(
format(Sys.time(), "%Y_%m_%d_%H_%M_%OS6"),
".",
GG_RECORDING_ENV$device_ext
))
plot_file <- camcorder_plot_file_path()

suppressMessages({

Expand Down
58 changes: 58 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,61 @@ derive_ext <- function(x){
}
}
}

camcorder_plot_file_path <- function(){

timestamp <- format(Sys.time(), "%Y_%m_%d_%H_%M_%OS6")
i <- camcorder_plot_count()

file.path(
GG_RECORDING_ENV$recording_dir,
paste0(
glue(GG_RECORDING_ENV$filename_pattern),
".",
GG_RECORDING_ENV$device_ext
))
}


camcorder_reserved_patterns <- list(
list(pattern = "{timestamp}", regex = "\\d{4}_\\d{2}_\\d{2}_\\d{2}_\\d{2}_\\d{2}[.]\\d+"),
list(pattern = "{i}", regex = "\\d+")
)

verify_filename_pattern <- function(x){
has_pattern <- FALSE
for(reserved_pattern in camcorder_reserved_patterns){
has_pattern <- grepl(reserved_pattern$pattern, x, fixed = TRUE)
if(has_pattern){
break
}
}

if(!has_pattern){
stop(paste0(
"filename_pattern must have one of the camcorder reserved filename",
" patterns to ensure uniqueness:\n",
paste0("\t`",sapply(camcorder_reserved_patterns, `[[`, "pattern"),"`", collapse = "\n")
)
)
}

x
}

camcorder_plot_count <- function(){
i <- mget("plot_count", GG_RECORDING_ENV, ifnotfound = 0)[[1]] + 1
set_plot_count(i)
i
}

cleanup_filename_pattern_to_regex <- function(filename_pattern){
for(reserved_pattern in camcorder_reserved_patterns){
filename_pattern <- gsub(reserved_pattern$pattern,reserved_pattern$regex, filename_pattern, fixed=TRUE)
}
filename_pattern
}

set_plot_count <- function(i){
GG_RECORDING_ENV$plot_count <- i
}
6 changes: 5 additions & 1 deletion man/Recording.Rd

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