profile_script: profile_script

View source: R/utils_profiling.R

profile_scriptR Documentation

profile_script

Description

This function will add profiling code to a script wherever the following flags are found in the first non-spacing characters on the line:

  • #<p Opening comment flag where profile_open will be inserted.

  • #>p Closing comment flag where profile_close will 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 using glue such that internal variables can be used for file naming conventions. In addition the function arguments, the following variable names can be used:

  • .lo Short for line open, the line number of the opening profile code.

  • .lc Short 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). If profile_script were called on a script called script.R with 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 all profvis related files will be put. Default: "profvis". Profile-ready script copies (if new_scripts = TRUE) and Rprof files/directories will be placed here. Set to NULL to use the working directory and overwrite existing files.

remove

(logical/character) to indicate whether to remove profiling code. Default: FALSE. Change to TRUE to remove profile code but leave flags. Change to "f"/"flags" to remove code and flags. Remove modifies .file in place.

new_script

(logical) to indicate whether to write new profile-ready script copies to dir_profvis. Default: TRUE, if FALSE the character vector output of the profile-ready script lines from the function can be written to a file manually.


yogat3ch/UU documentation built on May 31, 2024, 10:14 p.m.