Modifying an R package function for current R session; assignInNamespace not behaving like fixInNamespace?
Since fixInNamespace
calls assignInNamespace
you should be able to get it to work, the problem is probably that the environment is not the same and possibly some other attributes. If you change those to match then I would expect it to work better, possibly using code like:
tmpfun <- get("chartSeries.chob", envir = asNamespace("quantmod"))
environment(chartSeries.chob2) <- environment(tmpfun)
attributes(chartSeries.chob2) <- attributes(tmpfun) # don't know if this is really needed
assignInNamespace("chartSeries.chob", chartseries.chob2, ns="quantmod")
Another option for some changes would be to use the trace
function. This would make temporary changes and would be fine for inserting code, but I don't know if it would be reasonable for deleting commands or modifying in place (specifying an editor that changed the code rather than letting you change it might make this possible).
changing functions and unlocking bindings in R
If you want, you can use the godmode
package from Github:
# save original version
orig <- graphics::boxplot.default
# devtools::install_github("miraisolutions/godmode")
godmode:::assignAnywhere("boxplot.default", boxplot.default_new)
# switch back
godmode:::assignAnywhere("boxplot.default", orig)
boxplot.default_new
should then be your re-write of boxplot.default
, possibly including your function myboxplot.stats
(maybe even rename it) and the call to it.
stop() in package function doesn't end debug mode
I just did exactly as you instructed and I see the expected result:
pckgname::write(1)
[1] 1
Error in pckgname::write(1) : Text cannot be numeric.
I'm not sure what's wrong when you try it, but would recommend starting a fresh package in case something is not working correctly. You could even try reinstalling R and RStudio, as it worked first time for me and I can't think of why it wouldn't for anyone else.
For reference, here's exactly what I did:
I started the package from RStudio -> File -> New Project -> New Directory -> New Package
The only file I changed was
hello.R
- I added the code in your question exactly as-is.I built the package with RStudio -> Build -> Build from source
And then installed it with
install.packages("../pckgname_0.1.0.tar.gz", repos = NULL, type="source")
And loaded with
library(pckgname)
Modify package function
I finally found a solution that should work in all situations!
environment(customGenomePlot) <- asNamespace('snapCGH')
assignInNamespace("genomePlot", customGenomePlot, ns = "snapCGH")
The call to environment()
assures that the function will be able to call other hidden functions from the package.
The call to assignInNamespace()
assures that other functions from the package will call your updated version of the function.
It is possible that in certain situation, you need only one of these, but in general you need both. I struggled to find this general solution, found many other which are not working in some cases, like this (need opposite order), or this (misses the second part), or this (throws the error "cannot add bindings to a locked environment").
how to fix/edit an invisible function in R interaction session?
There are functions assignInNamespace
and fixInNamespace
that allow doing what you say. There is also the edit
argument to the trace
function which will let you edit a function in place. Using trace
has the advantage of making it easy to untrace
and remove the changes that you made.
Set current subchart
I think you must have a typo somewhere, in code you've not shown, as it works for me:
library(quantmod)
x=xts(runif(10),Sys.Date()+1:10)
z=1/x
chart_Series(x)
add_TA(x, type = "l",col = "green", lwd = 2)
curon = 2
add_TA(z, type = "l",col = "blue", lwd = 2, on=curon)
(By the way, this is what people mean by a "fully reproducible minimal example"; something you can copy and paste into a fresh R session. Unless it matters for your question, the data can be random.)
UPDATE: Having reproduced the problem when using a function, I did find a workaround (for what I think is a quantmod bug). If you name your variable on
instead of curon
then it works:
library(quantmod)
test=function(){
x=xts(runif(10),Sys.Date()+1:10)
z=1/x
chart_Series(x)
add_TA(x, type = "l",col = "green", lwd = 2)
on=2;add_TA(z, type = "l",col = "blue", lwd = 2, on=on)
}
Hooking up a function to window resize operation in R
This document talks about the display list and what happens when graphics are resized. I don't see a way to redo the calculations with base graphics, though there may be a hook that can be set. If you use grid graphics instead of base graphics then the linked document has some suggestions that may trigger the recalculations you want.
R mlogit package: use LAPACK instead of LINPACK
Thanks to Julius' comment and this post on namespaces in R, I figured out the answer. I added the following code right after my library statements:
source("mymFormula.R")
tmpfun <- get("model.matrix.mFormula", envir = asNamespace("mlogit"))
environment(mymFormula) <- environment(tmpfun)
attributes(mymFormula) <- attributes(tmpfun) # don't know if this is really needed
assignInNamespace("model.matrix.mFormula", mymFormula, ns="mlogit")
mymFormula.R
is an R script where I copy/pasted the contents of mlogit:::model.matrix.mFormula
and added mymFormula <-
before the function
invocation at the top of the file.
I viewed the contents of mlogit:::model.matrix.mFormula
by typing trace(mlogit:::model.matrix.mFormula, edit=TRUE)
in RStudio. (Thanks to this answer for help on how to do that.)
Related Topics
How to Apply Function Over Each Matrix Element's Indices
Ggplot2 Legend to Bottom and Horizontal
R: Multiple Linear Regression Model and Prediction Model
Xgboost in R: How Does Xgb.Cv Pass the Optimal Parameters into Xgb.Train
Convert Data Frame into Vector
Increase Legend Font Size Ggplot2
Shiny Doesn't Show Me the Entire Selectinput When I Have Choices > 1000
R: Replace All Values in a Dataframe Lower Than a Threshold with Na
Warning: Non-Integer #Successes in a Binomial Glm! (Survey Packages)
How to Calculate the Average of a Variable Between Two Date Ranges Using a Loop or Apply Function
Hyperlinking Text in a Ggplot2 Visualization
Writings Functions (Procedures) for Data.Table Objects
Remove All Variables Except Functions
Writing R Function with If Enviornment
How to Produce Time Series for Each Row of a Data Frame with an Unnamed First Column