I am working with the earlywarnings package, and would like to edit one of the functions written in the qda_ews function. I could do fix(...) but the function I would like to edit is for some reason not listed when I use fix.
The function is called generic_RShiny. Here is the link to the github (https://github.com/earlywarningtoolbox/earlywarnings-R/blob/master/earlywarnings/R/qda_ews.R).
How can I access the entire qda_ews.R code to make the changes I need?
once the library is loaded, use
trace(name_of_function, edit = T)
but beware that the function will be modified permanently (until of course you reinstall the package)
Related
For a package I am using, I would like to fix part of the code. I have downloaded the "package source" from CRAN and have narrowed down where the problem is. I would like to edit the problem function in RStudio. I've tried using trace, but I am unable to call the desired function. The function I want to call is a helper function (not in the documentation) to the main function (which is in the documentation). Is there a way to edit the functions not in the documentation and used in the implementation?
If it helps, the package I am using is called RecordLinkage. I would like to change the function .toFF which is called by RLBigDataDedup and is in the file RLBigData-classes.r.
Now that I've established the problem by looking at the source code, I now want to edit it in RStudio so that I can run it when using the package... The best I can do is looking at RLBigDataDedup in RStudio, but I can't find the .toFF that is used within it.
Thats not working because the hidden function .toFF is not known in the global env because it is hidden. If you want to edit/debug hidden function you have to specify the where argument in trace() with the corresponding function. In your case it would be RLBigDataLinkage.
Regarding to the docs of trace, it is stated:
For “hidden” functions such as S3 methods in a namespace, where = *
typically needs to be specified as well
So for your answer this will work:
trace(".toFF", edit=T, where = RLBigDataLinkage)
try debug(.toFF) or debug(function_to_calls_.toFF) This will open the debug tool when the function is called.
I am working on a project and I need to modify some funciton of existing package. Some of these functions are not exported and I only can acess them via packagename:::function in R. Are these function cannot be modified or must be not used or we do not allowded to modify them since there are not exported by the author? any help please?
Note that: I need to build my own function based on some existing function from the package. The existing functions are very helful and for my project I need to modify them to what I need. Then I will use these functions in my project only for my use. I will not modify the package itself. Hope it is clear.
Just take the source code, paste it in your R script (and give it another name), modify the code as you want and run the function. Then it will be saved in your environment and you can run it as a normal function. Hope this helps!
So I have code for a custom function in R, e.g. simply:
f <- function(x) return(x)
What I want to do is be able to call this function f as if it came with default R. That is, I don't want to have to source() the file containing the code to be able to use the function. Is there a way to accomplish this?
What you are looking to do is documented extensively under ?Startup. Basically, you need to customize your Rprofile.site or .Rprofile to include the functions that you want available on startup. A simple guide can be found at the Quick-R site.
One thing to note: if you are commonly sharing code with others, you do need to remember what you've changed in your startup options and be sure that the relevant functions are also shared.
I am using an R script that someone sent me. This is not a big package but just one function. To use it, I source the file.
However,whenever I restart R, I must type in source("directory") again to use the function.
Is there any way I can avoid this and set that function permanently?
I think that you just want to add source("directory") to your .Rprofile so that the function gets loaded at startup.
See this SO question for some handy things you can do with .Rprofile.
I am looking for a way to embed the fix() function within a script. Basically, here's what I'm currently doing:
I load a certain package. For example, library(PerformanceAnalytics)
I call the fix() function to edit a couple functions within the loaded package. Example, fix(VaR).
Then, using R's built-in editor, I copy-paste my function over the one originally loaded from the package.
Finally, I source in my .R script which calls the above functions I fixed and performs the computations I need.
Essentially, I'd like to streamline Step 3 above. Rather than having to manually type fix(function) and copy-paste over the original functions within the loaded package, I'd rather just have it done within a script I source.
Is there anyway to accomplish this?
FYI, I have reached out to the package's creator and loading a re-compiled version of the package with my modified code is out of the question.
Maybe source your functions and then use assignInNamespace?
EDIT #1:
The above won't work because assignInNamespace doesn't alter objects that have been exported. Instead,
put your functions in a file (foo.R)
load the package
then source(foo.R) or
sys.source(foo.R, envir=attach(NULL, name="myenv"))
Your functions will be higher up on the search list if you load them after the package, so R will find them before getting to the package's functions with the same name.
EDIT #2:
I didn't realize VaR called unexported functions in the namespace. That's why EDIT #1 doesn't work. To get it to work, you would need to explicitly reference all unexported PerformanceAnalytics functions used in VaR (e.g. change VaR.Gaussian to PerformanceAnalytics:::VaR.Gaussian).
See this post on R-devel for a couple other approaches. I couldn't quickly get Prof. Ripley's solution to work (I get the same error as in EDIT #1) and I didn't try Gabor's solution.
You can edit the body directly, discussed here:
What ways are there to edit a function in R?
You can download the packages source from CRAN. Edit the function (it will be found in PackageName/R), then install this package into R and just use it that way.
You can even change the package name in the DESCRIPTION file ... call it "PerformanceAnalytics2", then in R you just library(PerformanceAnalytics2) and use it as you would the original package.