简体   繁体   中英

Customize existing function in R

I want to change a condition within the function psych::polychoric in R. Specifically, I want to increase the limit of different realizations of aa variable from 8 to 10 on line 77 of the code. I can manually increase the limit by calling

trace(polychoric, edit=TRUE)

Since the script is meant for reproduction purposes for a paper of mine, I want to make handling as smooth as possible by avoiding manual editing. Is there a way to edit the function by a piece code, eg by replacing if (nvalues > 8) by if (nvalues > 10) in the code by another function?

Any suggestions would be much appreciated.

find the location in the function that you want to change

as.list(body(psych::polychoric))

Change the function

trace(psych::polychoric, quote(nvalues > 10), at=11)

Check to see that you changed what you want to change

trace(psych::polychoric, edit=TRUE)

Set the function back to original

untrace(psych::polychoric)

-----

Seems like fix may be easier for you to implement for this task

fix(polychoric)

opens a pane that you can change the code in - change and hit save.

This will make the function local to your global environment you can check this by looking at the original function trace(polychoric, edit = T) will show nvalues > 10 , and trace(psych::polychoric, edit = T) will show nvalues > 8 . The next time you reload psych you will be using the original function. Bit of a manual hack - but hopefully works for this one off situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM