Writing Functions

Basic Structure       Debugging       Logical Commands       Other       Main Menu

Basic Structure

Remember that for and while loops are slow in R. sapply/apply/lapply are better. You can always do:
sapply(1:n,FUN=function(i){...}). However, this does not always give great speed up if fundamentally your function is slow.

applybreakfor
functionifelseinvisible
mapplynextrepeat
returnsapplystop
stopifnotswitchtapply
warningwhile

Debugging

browserdebugdebugger
editfixproc.time
recoversystem.timetraceback

Logical Commands

Remember, x==expr is a logical vector, and if(x==expr) will only check if first element of that logical vector is true. Use any or all.

allall.equalany
hasArgidenticalis.finite
is.functionis.infiniteis.na
is.nullmissingwhich
which.maxwhich.min

Other

argscallcat
complete.casesevalmatch.arg
on.exitprintrequire