From b578cf1b5b441dc96af0e1be3c1047e6a8234671 Mon Sep 17 00:00:00 2001 From: Ian Lyttle Date: Tue, 2 Jan 2024 16:45:07 -0600 Subject: [PATCH] use withr::local_tempfile() in place of tempfile() --- R/boxr_read.R | 5 +---- R/boxr_save_load.R | 9 +-------- R/boxr_upload_download.R | 5 +---- tests/testthat/test_05_read_write.R | 8 ++++---- 4 files changed, 7 insertions(+), 20 deletions(-) diff --git a/R/boxr_read.R b/R/boxr_read.R index feff538c..78d01fb2 100644 --- a/R/boxr_read.R +++ b/R/boxr_read.R @@ -54,7 +54,7 @@ box_read <- function(file_id, type = NULL, version_id = NULL, ...) { checkAuth() - temp_file <- tempfile() + temp_file <- withr::local_tempfile() # Make the request req <- boxGet(file_id, local_file = temp_file, version_id = version_id, @@ -106,9 +106,6 @@ box_read <- function(file_id, type = NULL, version_id = NULL, cont <- unclass(cont) } - # Delete the tempfile - unlink(temp_file, force = TRUE) - message( "Remote file '", new_name, "' read into memory as an object of class ", paste(class(cont), collapse = ", "), diff --git a/R/boxr_save_load.R b/R/boxr_save_load.R index 555b9aba..55861f16 100644 --- a/R/boxr_save_load.R +++ b/R/boxr_save_load.R @@ -31,14 +31,7 @@ box_save <- function(..., dir_id = box_getwd(), file_name = ".RData", description = NULL) { - # TODO: fs - #temp_file <- withr::local_tempfile(pattern = file_name) - temp_file <- normalizePath(file.path(tempdir(), file_name), mustWork = FALSE) - - # clean up after ourselves - # TODO: withr 2.3.0 may have a cleaner way to do this: local_tempfile() - # - see https://github.com/r-lib/usethis/issues/1217 - on.exit(fs::file_delete(temp_file)) + temp_file <- withr::local_tempfile(pattern = file_name) save(..., file = temp_file) diff --git a/R/boxr_upload_download.R b/R/boxr_upload_download.R index f9a7de7a..07a64a37 100644 --- a/R/boxr_upload_download.R +++ b/R/boxr_upload_download.R @@ -83,7 +83,7 @@ box_dl <- function(file_id, local_dir = getwd(), overwrite = FALSE, stop("File already exists locally, and overwrite = FALSE") # Get a temp file - temp_file <- tempfile() + temp_file <- withr::local_tempfile() # Download to a tempfile with boxGet req <- boxGet(file_id = file_id, version_id = version_id, @@ -127,9 +127,6 @@ box_dl <- function(file_id, local_dir = getwd(), overwrite = FALSE, stop("Problem writing file to ", new_file, ".\n Check that directory is writable.") - # Remove the tempfile to free up space - file.remove(temp_file) - return(new_file) } diff --git a/tests/testthat/test_05_read_write.R b/tests/testthat/test_05_read_write.R index 92d8068c..44be226f 100644 --- a/tests/testthat/test_05_read_write.R +++ b/tests/testthat/test_05_read_write.R @@ -8,7 +8,7 @@ test_that("You can source a remote R script", { skip_if_no_token() # Write a little R script - tf <- paste(tempfile(), ".R") + tf <- withr::local_tempfile(fileext = ".R") writeLines("test_vector <- 1:10\n", tf) # Upload it, so that you can 'source it' back down @@ -26,7 +26,7 @@ test_that("You can write/read a remote .csv file", { skip_if_no_token() # Write a little .csv file - tf <- paste0(tempfile(), ".csv") + tf <- withr::local_tempfile(fileext = ".csv") # Note: It looks like although httr says it uses read.csv for the .csv files, # it doesn't obey the usual R behaviour of treating strings as factors by # default. So to get two objects that match, you'll need to make sure they're @@ -58,7 +58,7 @@ test_that("You can write/read a remote .tsv file", { skip_if_no_token() # Write a little .tsv file - tf <- paste0(tempfile(), ".tsv") + tf <- withr::local_tempfile(fileext = ".tsv") df <- data.frame(a = letters[1:5], b = 1:5, c = rnorm(5), stringsAsFactors = FALSE) @@ -88,7 +88,7 @@ test_that("You can write/read a remote .json file", { skip_if_no_token() # Write a little .json file - tf <- paste0(tempfile(), ".json") + tf <- withr::local_tempfile(fileext = ".json") df <- data.frame(a = letters[1:5], b = 1:5, c = rnorm(5), stringsAsFactors = FALSE) l <- list(a = 1:10, b = matrix(1, 3, 3), c = df)