[Rd] Monkey patching +.POSIXt
Mark.Bravington at csiro.au
Mark.Bravington at csiro.au
Wed Sep 9 23:40:11 CEST 2009
Hi Hadley
> This summer I've been working with a grad student to bring more of the
> date time classes from JODA (http://joda-time.sourceforge.net/) into
> R. To make these work seamlessly with existing date time objects, we
> need to patch +.POSIXt. (The ruby community uses the term
> monkey-patching for this sort of ill-advised, by sometimes necessary,
> internal hackery, hence the title.) The problem is I can't figure out
> how to override +.POSIXt so that it gets called from +. Simply
> including a new +.POSIXt function in the package doesn't work because
> (I presume) + finds and uses base::+.POSIXt. The next thing I tried
> was
>
> assignInNamespace("+.POSIXt", lubridate::`+.POSIXt`, "base")
>
> but that doesn't seem to work either, despite the documentation: "They
> do attempt to alter a copy registered as an S3 method if one is
> found." (perhaps there is a missing not there?)
>
>
In this particular case, the trick is to assign the new version both into the namespace and into the environment '.__S3MethodsTable__.' inside the namespace.
Speaking as a monkey, I do this kind of thing regularly, and there is a function 'assign.to.base' in the 'mvbutils' package that does it automatically, e.g.
assign.to.base( '+.POSIXt', my.plus.POSIXt)
The '.onLoad' of mvbutils has a few examples, including as it happens '+.POSIXt'. Although the version of 'mvbutils' on CRAN is quite out-of-date, the code for 'assign.to.base' should be in there.
HTH
Mark Bravington
CSIRO, Hobart, Australia
________________________________________
From: r-devel-bounces at r-project.org [r-devel-bounces at r-project.org] On Behalf Of Hadley Wickham [hadley at rice.edu]
Sent: 10 September 2009 00:40
To: r-devel at r-project.org
Subject: [Rd] Monkey patching +.POSIXt
Hi all,
In desperation, I also tried the following internal, undocumented
function, but it didn't help either:
registerS3method("+", "POSIXt", lubridate::`+.POSIXt`)
Any ideas?
Thanks,
Hadley
--
http://had.co.nz/
______________________________________________
R-devel at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
More information about the R-devel
mailing list