replace_package_fun: replace function in package

replace_package_funR Documentation

replace function in package

Description

This function uses trace to (temporarily) replace a function in a package. The same function can be used to redo this action by using 'start=F

Usage

replace_package_fun(fn1, fn2, ns, envir = globalenv(), start = T)

Arguments

fn1

Character string with name of function in package

fn2

Character string with name of function in the global environment. See details

ns

Character with name of package

envir

Environment in which the new environment wil be set

start

Boolean indicating if the rename should be activated (when TRUE) or redone (when FALSE)

Acknowledgement

The inner function my_edit is an adaptation of the code gotten by using getOption("editor") in an RStudio window. I assume this is part of the RStudio product. Because the function did not work in the blogdown environment, I replaced the call to (RStudio function ?) .rs.deparseFunction with a call to the base function deparse.

Details

The function works by using trace to insert a browser() line in a function and immediately replacing this line, by using a customized editor function, to a call of the replacement function. See blog entry for an example of use. A non-global function fn2 can temporarily copied to the global environment. See examples.

Examples

## Not run: 
# example function
format_WENS2 <- function (x,WENS='NS',dig=3) {
cat('print of  the body of internal function HOQCutil:::internal_demo_function :',sep='\n')
print(internal_demo_function)
NULL
}

replace_package_fun('format_WENS','format_WENS2','HOQCutil',start=T)
HOQCutil:::format_WENS(3.1415,WENS='NS',dig=3)
replace_package_fun('format_WENS','format_WENS2','HOQCutil',start=F)

# when format_WENS2 is not a global function :

assign('HOQC1181_format_WENS2',format_WENS2,envir = globalenv())
replace_package_fun('format_WENS','HOQC1181_format_WENS2','HOQCutil',start=T)
HOQCutil:::format_WENS(3.1415,WENS='NS',dig=3)
replace_package_fun('format_WENS','HOQC1181_format_WENS2','HOQCutil',start=F)
rm(list='HOQC1181_format_WENS2',envir = globalenv() )


## End(Not run)

HanOostdijk/HOQCutil documentation built on July 28, 2023, 5:56 p.m.