Title: | Reliably Return the Source and Call Location of a Command |
---|---|
Description: | Robust and reliable functions to return informative outputs to console with the run or source location of a command. This can be from the 'RScript'/R terminal commands or 'RStudio' console, source editor, 'Rmarkdown' document and a Shiny application. |
Authors: | Jonathan Sidi [aut, cre] , Colin Fay [ctb] , Kirill Müller [aut] |
Maintainer: | Jonathan Sidi <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.2.0 |
Built: | 2024-11-11 03:14:11 UTC |
Source: | https://github.com/yonicd/whereami |
cat function for whereami output
cat_where( where, type = c("rule", "boxx", "bullet", "line", "print"), color = c("none", "black", "blue", "cyan", "green", "magenta", "red", "white", "yellow", "grey", "silver"), ... )
cat_where( where, type = c("rule", "boxx", "bullet", "line", "print"), color = c("none", "black", "blue", "cyan", "green", "magenta", "red", "white", "yellow", "grey", "silver"), ... )
where |
whereami output |
type |
|
color |
|
... |
further arguments passed to |
character
Colin Fay
cat_where(whereami()) cat_where(whereami(), type = "bullet", bullet = "tick") cat_where(whereami(), color = "green")
cat_where(whereami()) cat_where(whereami(), type = "bullet", bullet = "tick") cat_where(whereami(), color = "green")
Interact with internal whereami counter
counter_state(item = NULL, tag = NULL) counter_reset(item = NULL, tag = NULL) counter_names() counter_tags() counter_get()
counter_state(item = NULL, tag = NULL) counter_reset(item = NULL, tag = NULL) counter_names() counter_tags() counter_get()
item |
character, name of the counter to access, Default: NULL |
tag |
character, tag of the counter to acces, if it was set at initial whereami call. This can be used instead of the item name, Default: NULL |
counter_names()
returns names of the active stored counters.
counter_state()
returns current hit count for item, if NULL then all counters
are returned.
counter_reset()
will remove counter of item, if item is NULL then all counters
are reset.
counter_get()
returns a table with all the counters. These can then
be plotted with the with the plot method.
A json log of the counter is written to file.path(tempdir(),'whereami.json') by default The path can be set using set_whereami_log
Jonathan Sidi
Other counter:
set_whereami_log()
if(interactive()){ txt <- " whereami::cat_where(whereami::whereami(tag = 'tag1')) " tf <- tempfile(fileext = '.R') cat(txt,file = tf) source(tf) counter_state() counter_state(tag = 'tag1') counter_names() counter_tags() counters <- counter_get() counters plot(counter_get()) # read the json log jsonlite::read_json( file.path(tempdir(),'whereami.json'), simplifyVector = TRUE) # clear all counters counter_reset() #verify that there are no active counters counter_state() # cleanup unlink(tf) }
if(interactive()){ txt <- " whereami::cat_where(whereami::whereami(tag = 'tag1')) " tf <- tempfile(fileext = '.R') cat(txt,file = tf) source(tf) counter_state() counter_state(tag = 'tag1') counter_names() counter_tags() counters <- counter_get() counters plot(counter_get()) # read the json log jsonlite::read_json( file.path(tempdir(),'whereami.json'), simplifyVector = TRUE) # clear all counters counter_reset() #verify that there are no active counters counter_state() # cleanup unlink(tf) }
Set the path to write the counter log as a json
set_whereami_log(path = tempdir())
set_whereami_log(path = tempdir())
path |
character, path to write to, Default: tempdir() |
Other counter:
counter_state()
R does not store nor export the path of the currently running script. This is an attempt to circumvent this limitation by applying heuristics (such as call stack and argument inspection) that work in many cases. CAVEAT: Use this function only if your workflow does not permit other solution: if a script needs to know its location, it should be set outside the context of the script if possible.
thisfile() thisfile_source() thisfile_r() thisfile_rscript() thisfile_knit()
thisfile() thisfile_source() thisfile_r() thisfile_rscript() thisfile_knit()
This functions currently work only if the script was source
d,
processed with knitr
,
or run with Rscript
or using the --file
parameter to the
R
executable. For code run with Rscript
, the exact value
of the parameter passed to Rscript
is returned.
The path of the currently running script, NULL if it cannot be determined.
These functions were migrated from r-lib/rprojroot.
Kirill Müller, Hadley Wickham, Michael R. Head
https://stackoverflow.com/q/1815606/946850
base::source()
, utils::Rscript()
, base::getwd()
if( !interactive() ) thisfile() # using in terminal ( with pipe() ) # open a temp file tf <- tempfile(fileext = '.R') # call to write to temp file f <- 'cat(whereami::thisfile(), "\n", sep = "")' # write to the file cat(f,file = tf) # create an R call to terminal fcmd <- sprintf('"%s" --slave --vanilla --no-save -f %s',R.home('bin/R'),tf) # run the call p <- pipe(fcmd) # read the output readLines(p) # cleanup close(p) unlink(tf)
if( !interactive() ) thisfile() # using in terminal ( with pipe() ) # open a temp file tf <- tempfile(fileext = '.R') # call to write to temp file f <- 'cat(whereami::thisfile(), "\n", sep = "")' # write to the file cat(f,file = tf) # create an R call to terminal fcmd <- sprintf('"%s" --slave --vanilla --no-save -f %s',R.home('bin/R'),tf) # run the call p <- pipe(fcmd) # read the output readLines(p) # cleanup close(p) unlink(tf)
Returns the file that a command is run from.
If traceback is available then the line that it was run from is also returned.
whereami(path_expand = FALSE, tag = NULL)
whereami(path_expand = FALSE, tag = NULL)
path_expand |
logical, expand relational path, Default: FALSE |
tag |
character, optional tag for the call, Default: NULL |
character
Jonathan Sidi
whereami() whereami(path_expand = TRUE)
whereami() whereami(path_expand = TRUE)