There's a bit of preamble before I get to my question, so hold on to me!
For the R package I'm working on, I would like to make it as simple as possible for users to partially apply the inline functions. I had the idea of using the [] operators to call my partial application function, which I call "partialApplication". I want to achieve this:
dnorm[mean = 3](1:10)
# Which would be exactly equivalent to:
dnorm(1:10, mean = 3)
To do this, I tried to define a new method [] for class function objects, i.e.
`[.function` <- function(...) partialApplication(...)
However, R gives a warning that the [] method for the function objects is "locked". (Is there a way to override this?)
My idea seemed to be frustrated, but I thought of one simple solution: I can come up with a new class "partialAppliable" of the S3 class and create a method for it [], ie
`[.partialAppliable` = function(...) partialApplication(...)
Then I can take any function I want and add “partialAppliable” to my class, and now my method will work.
class(dnorm) = append(class(dnorm), 'partialAppliable')
dnorm[mean = 3](1:10)
# It works!
Now here is my question / problem: I would like users to be able to use any function they wanted, so I thought that if I scroll through all the objects in the active environment (using ls) and add "partialAppliable" to the class of all functions? For instance:
allobjs = unlist(lapply(search(), ls))
for(i in allobjs) {
if(is.function(get(i))) {
curfunc = get(i)
class(curfunc) = append(class(curfunc), 'partialAppliable')
assign(i, curfunc)
}
}
Voila! It works. (I know, I should probably assign the changed functions back to the source package environments, but you will get an image).
, , ( ), , /. - , . , : ? - /, , ?
!