Posts Tagged ‘friday-function’

(Almost) Friday Function: alarm

21st April, 2011 2 comments

Last week I decided to start a weekly column detailing an interesting function each Friday, entirely forgetting that I would be on holiday, without internet access (shock horror!), tomorrow. So here’s your column a little early.

The alarm function is something of a novelty, in that all it does is to make an annoying noise when you call it. The only vaguely sensible time that I can think of that you might want to do this is when an error is thrown. Setting this up means overriding the default error handling behaviour, which is surprisingly easy.

options(error = alarm) 
stop("!!!")  # to test the behaviour

For best results, make sure you’ve unplugged your headphones and turn the volume up loud to annoy friends, family and colleagues while debugging.

(To restore the default behaviour, use options(error = NULL).)

Friday Function: setInternet2

15th April, 2011 2 comments

Corporate IT networks are a pain for programmers. Ideally, when programming, you want the freedom to download, install and run any software that you want. Unfortunately, in the interests of security, many programmers find themselves a little restricted at the office. (I’m sure that many network admins will protest that the situation works both ways – prgrammers are also a pain for corporate IT networks.)

With the default installation of R, you may find that connecting from R to the internet doesn’t work. This is a shame, since there are many useful features of R that require internet access, not least downloading packages and scraping data from webpages with RCurl.

For Windows users, there is a solution. By typing setInternet2(TRUE), R connects via internet2.dll, which Internet Explorer uses. From a network point of view this makes R appear to be the same as Internet Explorer, and sneak through.

In order to have this functionality every time you run R, add that line of code to your file in the R.home("etc") directory. If you have control over the installation of R, choosing a custom installation gives you the option to connect via internet2.dll by default.