diff --git a/NEWS.md b/NEWS.md index a7589f3..a12718a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,7 @@ * Tests use https://demo.dataverse.org/dataverse/dataverse-client-r/. (#40) * Fixes most get_file errors by removing query argument (#33 @kuriwaki) * Fix getting multiple files by id in `get_file()` (#47 @adam3smith) +* Temporary files created by `get_file()` are automatically deleted. # CHANGES TO dataverse 0.2.1 diff --git a/R/get_dataframe.R b/R/get_dataframe.R index 434d636..31d14bc 100644 --- a/R/get_dataframe.R +++ b/R/get_dataframe.R @@ -140,11 +140,15 @@ get_dataframe_by_doi <- function(filedoi, #' #' @keywords internal get_dataframe_internal <- function(raw, filename, .f) { - tmp <- tempfile(filename) - writeBin(raw, tmp) - - do.call(.f, list(tmp)) - - # TODO: unlink/delete tmp file in a try/catch/finally block. + tryCatch( + { + tmp <- tempfile(filename) + writeBin(raw, tmp) + do.call(.f, list(tmp)) + }, + finally = { + if (file.exists(tmp)) unlink(tmp) + } + ) }