Title: | Run Once and Save Result |
---|---|
Description: | Package 'runonce' helps automating the saving of long-running code to help running the same code multiple times. If you run some long-running code once, it saves the result in a file on disk. Then, if the result already exists, i.e. if the code has already been run and its output has already been saved, it just reads the result from the stored file instead of running the code again. |
Authors: | Florian Privé [aut, cre] |
Maintainer: | Florian Privé <[email protected]> |
License: | GPL-3 |
Version: | 0.2.3 |
Built: | 2024-11-21 04:34:59 UTC |
Source: | https://github.com/privefl/runonce |
Download file if does not exist yet.
download_file( url, dir = tempdir(), fname = url_basename(url), overwrite = FALSE, mode = "wb", ... )
download_file( url, dir = tempdir(), fname = url_basename(url), overwrite = FALSE, mode = "wb", ... )
url |
URL of file to be downloaded. |
dir |
Directory where to download the file. |
fname |
Base name of the downloaded file ( |
overwrite |
Whether to overwrite? Default is |
mode |
See parameter of |
... |
Arguments passed on to
|
Path to the downloaded (or existing) file.
download_file("https://github.com/privefl.png") download_file("https://github.com/privefl.png") download_file("https://github.com/privefl.png", overwrite = TRUE)
download_file("https://github.com/privefl.png") download_file("https://github.com/privefl.png") download_file("https://github.com/privefl.png", overwrite = TRUE)
Cache the result of code
in an RDS file.
save_run(code, file, timing = TRUE)
save_run(code, file, timing = TRUE)
code |
Code to run. Do not forget to wrap it with |
file |
File path where the result is stored. Should have extension |
timing |
Whether to print timing of running code? Default is |
The evaluation of code
the first time, the content of file
otherwise.
# Prepare some temporary file tmp <- tempfile(fileext = ".rds") # Run once because result does not exist yet save_run({ Sys.sleep(2) 1 }, file = tmp) # Skip run because the result already exists # (but still output how long it took the first time) save_run({ Sys.sleep(2) 1 }, file = tmp)
# Prepare some temporary file tmp <- tempfile(fileext = ".rds") # Run once because result does not exist yet save_run({ Sys.sleep(2) 1 }, file = tmp) # Skip run because the result already exists # (but still output how long it took the first time) save_run({ Sys.sleep(2) 1 }, file = tmp)
Skip code if all conditions are fulfilled. The code should not return anything.
skip_run_if(code, cond = NULL, files = NULL, timing = TRUE)
skip_run_if(code, cond = NULL, files = NULL, timing = TRUE)
code |
Code to run. Do not forget to wrap it with |
cond |
Condition to be fulfilled to skip running |
files |
Character vector of file path(s). Default is |
timing |
Whether to print timing of running code? Default is |
NULL
, invisibly.
# Prepare some temporary file tmp <- tempfile(fileext = ".txt") # Run once because file does not exist yet skip_run_if({ Sys.sleep(2) write.table(iris, tmp) }, cond = file.exists(tmp)) # Skip run because `cond` is `TRUE` skip_run_if({ Sys.sleep(2) write.table(iris, tmp) }, cond = file.exists(tmp)) # Skip run because file exists skip_run_if({ Sys.sleep(2) write.table(iris, tmp) }, files = tmp)
# Prepare some temporary file tmp <- tempfile(fileext = ".txt") # Run once because file does not exist yet skip_run_if({ Sys.sleep(2) write.table(iris, tmp) }, cond = file.exists(tmp)) # Skip run because `cond` is `TRUE` skip_run_if({ Sys.sleep(2) write.table(iris, tmp) }, cond = file.exists(tmp)) # Skip run because file exists skip_run_if({ Sys.sleep(2) write.table(iris, tmp) }, files = tmp)