Skip to content

Commit

Permalink
refactored creating temp file (or finding existing files) to a function
Browse files Browse the repository at this point in the history
  • Loading branch information
Torbjørn Lindahl committed Sep 9, 2024
1 parent f7db914 commit f5b002f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 53 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
export(local_here)
export(with_here)
importFrom(fs,dir_exists)
importFrom(fs,dir_ls)
importFrom(fs,file_touch)
importFrom(fs,is_file)
importFrom(fs,path_rel)
importFrom(here,here)
importFrom(here,i_am)
Expand Down
93 changes: 40 additions & 53 deletions R/here.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,47 +31,27 @@ with_here <- function(new_here, expr, chdir=FALSE, verbose=FALSE ) {

current_here <- here()

current_here_contents <- dir_ls(path = current_here, all = TRUE)
i <- is_file(current_here_contents)

# find or make a suitable file to get us back when we're done
if(length(current_here_contents) >= 1) {
# fish out the first file in here()
tf_current <- current_here_contents[i][1]
} else {
# create a temporary file
tf_current <- local_tempfile(tmpdir=current_here, pattern=".here")
file_touch(tf_current)
}
# create a temporary file to get us back when we're done
tf_current <- file_beacon(current_here)

# opt to suppress i_am's "here() starts at"
f <- function(x) {
m <- capture.output(x, type="message")
if(!verbose) {
m <- grep("^here\\(\\) starts at", m, value=TRUE, invert=TRUE)
}
if(length(m))
message(m)
}

# make sure it goes back aftrwards (this will trigger after the
# above local_dir defer has changed the working dir back
defer(f(local({
defer(suppress_here_message(local({
setwd(current_here)
i_am(path_rel(tf_current, current_here))
})))
}), verbose=verbose))

# create another remporary file to get us where we want to go
tf_temp <- local_tempfile(tmpdir=new_here, pattern=".here")
file_touch(tf_temp)
tf_temp <- file_beacon(new_here)

local({

# change to this directory and setup here()
local_dir(new_here)

# redirect here temporarily
f(i_am(path_rel(tf_temp, new_here)))
suppress_here_message(i_am(path_rel(tf_temp, new_here)), verbose=verbose)

})

Expand Down Expand Up @@ -123,47 +103,26 @@ local_here <- function(new_here, chdir=FALSE, verbose=FALSE, .local_envir = pare

current_here <- here()

current_here_contents <- dir_ls(path = current_here, all = TRUE)
i <- is_file(current_here_contents)

# find or make a suitable file to get us back when we're done
if(length(current_here_contents) >= 1) {
# fish out the first file in here()
tf_current <- current_here_contents[i][1]
} else {
# create a temporary file
tf_current <- local_tempfile(tmpdir=current_here, pattern=".here")
file_touch(tf_current)
}

# opt to suppress i_am's "here() starts at"
f <- function(x) {
m <- capture.output(x, type="message")
if(!verbose) {
m <- grep("^here\\(\\) starts at", m, value=TRUE, invert=TRUE)
}
if(length(m))
message(m)
}
# create a temporary file to get us back when we're done
tf_current <- file_beacon(current_here)

# make sure it goes back aftrwards (this will trigger after the
# above local_dir defer has changed the working dir back
defer(f(local({
defer(suppress_here_message(local({
local_dir(current_here)
i_am(path_rel(tf_current, current_here))
})), envir=.local_envir)
}), verbose=verbose), envir=.local_envir)

# create another remporary file to get us where we want to go
tf_temp <- local_tempfile(tmpdir=new_here, pattern=".here")
file_touch(tf_temp)
tf_temp <- file_beacon(new_here)

local({

# change to this directory and setup here()
local_dir(new_here)

# redirect here temporarily
f(i_am(path_rel(tf_temp, new_here)))
suppress_here_message(i_am(path_rel(tf_temp, new_here)), verbose=verbose)

})

Expand All @@ -173,3 +132,31 @@ local_here <- function(new_here, chdir=FALSE, verbose=FALSE, .local_envir = pare
invisible(current_here)

}

##' @importFrom fs dir_ls is_file
##' @importFrom withr local_tempfile
file_beacon <- function(where) {

stopifnot(dir_exists(where))
existing_paths <- Filter(is_file, dir_ls(where, all=TRUE))

if(length(existing_paths))
return(existing_paths[1])

# create a temporary local to the parent environment
f <- local_tempfile(tmpdir=where, .local_envir=parent.frame(), pattern=".here")
file_touch(f)
return(f)

}


##' @importFrom utils capture.output
suppress_here_message <- function(x, verbose=TRUE) {
m <- capture.output(x, type="message")
if(!verbose) {
m <- grep("^here\\(\\) starts at", m, value=TRUE, invert=TRUE)
}
if(length(m))
message(m)
}

0 comments on commit f5b002f

Please sign in to comment.