Skip to content

Commit

Permalink
hgd_watch changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nx10 committed Feb 22, 2022
1 parent 65f7083 commit 87f3b98
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions R/httpgd.R
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,14 @@ hgd_inline <- function(code,
s
}

files_snapshot <- function(files) {
data.frame(file=files, mtime=file.info(files)[,"mtime"])
}

files_changed <- function(snap1, snap2) {
snap1$file[snap1$mtime != snap2$mtime]
}

#' Watch for changes in code files and refresh plots automatically.
#'
#' This function may be used to rapidly develop visualizations
Expand Down Expand Up @@ -770,7 +778,7 @@ hgd_inline <- function(code,
#' )
#'
#' }
hgd_watch <- function(watch = ".",
hgd_watch <- function(watch = list.files(pattern="\\.R$", ignore.case = T),
on_change = function(changed_files) {
print(changed_files)
},
Expand All @@ -789,22 +797,22 @@ hgd_watch <- function(watch = ".",
if (!is.null(on_start)) {
on_start()
}
files_previous <- fileSnapshot(path = watch)
files_previous <- files_snapshot(watch)
tryCatch(
{
on_change(c())
},
error = on_error
)
while (TRUE) {
files_current <- fileSnapshot(path = watch)
changes <- changedFiles(files_previous, files_current)
if (sum(changes$changes) > 0) {
files_current <- files_snapshot(watch)
changes <- files_changed(files_previous, files_current)
if (length(changes) > 0) {
hgd_clear()
files_previous <- files_current
tryCatch(
{
on_change(changes$changed)
on_change(changes)
},
error = on_error
)
Expand Down

0 comments on commit 87f3b98

Please sign in to comment.