**R snippets**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Recently I have been working with GNU R optimization routines a lot. Function optim has a nice trace option that allows to monitor optimization progress. Another standard function optimize has no such feature but it is possible to add outside tracking to it.

Let us start with an example from documentation:

**<-**

**function**

**(**x, a

**)**

**{**

**(**x

**–**a

**)**

**^**2

**}**

print**(**optimize**(**f, c**(**0, 1**)**, tol **=** 0.0001, a **=** 1**/**3**))**

which produces:

[1] 0

We only get the final result of the optimization. The simple way to add tracing is by inserting print statement inside optimized function like this:

**<-**

**function**

**(**x, a

**)**

**{**

**(**x

**)**

**(**x

**–**a

**)**

**^**2

**}**

print**(**optimize**(**f, c**(**0, 1**)**, tol **=** 0.0001, a **=** 1**/**3**))**

The call produces the following output:

[1] 0

However, GNU R allows for a much fancier method that at the same time shows some nice features of the language itself. We use original function but add a wrapper around it:

**<-**

**function**

**(**f, minimum

**=**

**TRUE**

**)**

**{**

**<-**

**NULL**

**(**list

**(**f

**=**

**function**

**(**x, …

**)**

**{**

**<<-**c

**(**calls, x

**)**

**(**minimum,f

**(**x, …

**)**,

**–**f

**(**x, …

**))**

**}**,

**=**

**function()**

**{**return

**(**calls

**)**

**}**,

**=**

**function**

**()**

**{**calls

**<<-**

**NULL**

**}))**

**}**

**<-**wrap.f

**(**

**function**

**(**x, a

**)**

**{**

**(**x

**–**a

**)**

**^**2

**})**

**(**optimize

**(**wf

**$**f, c

**(**0, 1

**)**, tol

**=**0.0001, a

**=**1

**/**3

**))**

**(**wf

**$**getter

**())**

**$**reset

**()**

print**(**wf**$**getter**())**

The wrap.f function allows for tracking of calls of its first argument and to change optimization direction (this is possible through optimize function, but I add this as an example what can be achieved using this method). It holds internal variable calls which keeps track of calls to f function. It can be accessed using getter function and reset using reset function. The last three lines of code present the effect of resetting calls variable. Here is the output from the code:

NULL

The wrapping method presented here can be used as a replacement for S4 classes in simple situations. We use the environment created by wrap.f function to store attributes and return methods that can access and modify them.

**leave a comment**for the author, please follow the link and comment on their blog:

**R snippets**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.