profile_script
profile_script.RdThis function will add profiling code to a script wherever the following flags are found in the first non-spacing characters on the line:
#<pOpening comment flag whereprofile_openwill be inserted.#>pClosing comment flag whereprofile_closewill be inserted.
Usage
profile_script(
.file,
profile_open = utils::Rprof(fs::path(dir_profvis, ext(basename(.file), strip = TRUE),
glue::glue("{.lo}-{.lc}_{file_timestamp()}"), ext = "Rprof"), interval = 0.02,
line.profiling = TRUE, memory.profiling = TRUE),
profile_close = utils::Rprof(NULL),
dir_profvis = "profvis",
remove = FALSE,
new_script = TRUE,
new_filename = paste0(ext(basename(.file), strip = TRUE), "_prof", ext(.file))
)Arguments
- .file
path to script
- profile_open
(expression)The code that will be added preceding the opening comment flag. Consider usinggluesuch that internal variables can be used for file naming conventions. In addition the function arguments, the following variable names can be used:.loShort for line open, the line number of the opening profile code..lcShort for line close, the line number of the closing profile code.
Default:
utils::Rprof(fs::path(dir_profvis, basename(.x), glue::glue('{.lo}-{.lc}'), ext = "Rprof"), interval = .01, line.profiling = TRUE, gc.profiling = TRUE, memory.profiling = TRUE). Ifprofile_scriptwere called on a script calledscript.Rwith opening flag at line 20 and closing flag at line 30, then line 20 would be changed as follows:utils::Rprof("profvis/script.R/20-30.Rprof", interval = 0.01, line.profiling = TRUE, gc.profiling = TRUE, memory.profiling = TRUE) #<p.- profile_close
(expression)The code that will be added preceding the closing comment flag. Default:utils::Rprof(NULL)- dir_profvis
(character)The directory in which allprofvisrelated files will be put. Default:"profvis". Profile-ready script copies (ifnew_scripts = TRUE) and Rprof files/directories will be placed here. Set toNULLto use the working directory and overwrite existing files.- remove
(logical/character)to indicate whether to remove profiling code. Default: FALSE. Change toTRUEto remove profile code but leave flags. Change to"f"/"flags"to remove code and flags. Remove modifies.filein place.- new_script
(logical)to indicate whether to write new profile-ready script copies todir_profvis. Default: TRUE, ifFALSEthe character vector output of the profile-ready script lines from the function can be written to a file manually.