R Style Sheets

R Style Sheets

I, by no means, have the definitive take on how R should be written. These are just a collection of other's suggested best practices. Pick one that suits you.

Getting Help in R

Getting Help in R

Problem

Where to find help when using R

Solution

This is a consolidation of many sources, in particular a talk given by Rob Hyndman, which is available as a video, and as a pdf. Although the information is pretty basic, there were a couple of tricks that were new to me.

If you know the function name

Simply key in ?function_name at the prompt. Eg. ?mean, or ?sd

If you know the function name, but not where to find it

??function_name will search within your installed packages. The output is a nicely formatted list of package names and descriptions. The help systems does a approximate string matching on a default set of fields contained in the help files. Here are some examnples:

??DEoptim
??"DEoptim"
help.search("DEoptim")  # same as ??DEoptim or ??"DEoptim"; the quotes are required
??stats::sd  # restricts the search to just the stats package

If you get the chance, check out ?help.search. There are many more options to the help.search function.

If you need to search on something other than a known function name

RSiteSearch("search string") will do the trick. This will open a window in your browser at the search.r-project.org. It searches mailing list archives, help pages, vignettes and task views.

RSiteSearch("differential evolution optimization") # returned 33 items that contained all three search words

Check out these search options for use in the RSiteSearch() function. Options include AND; OR; NOT; grouping with parantheses; * wildcard as a prefix, postfix, or both; regular expression matching with /.../ as delimiters; and lastly searches restricted to specified fields.

RSiteSearch("differential evolution optimization") # returned 1000+ matches
RSiteSearch("diff* evol* optim*") # returned 1500 matches
RSiteSearch("genetic algorithm not neural") # intially found 507, the NOT reduced final results to 476 matches
RSiteSearch("/svm/") # found 33 in task views and vignettes, too many in functions

The home page for http://search.r-project.org/ has lots of other links for searching R. Well worth taking a further look.

If you are looking for a particular function, but can't remember the name exactly, or what package it is in

The third-party package “sos” provides the findFn() function. This function uses RSiteSearch(string, "function") to search only the functions in packages for the specified string. The results are contained in a dataframe that is then displayed as html in the browser.

install.packages("sos") # if not installed already
library(sos)
findFn("psoptim") # displays an html page with 6 results, all in the pso package

Other search facilities

These are repeated from the http://www.r-project.org/search.html page.

If you want to see what packages are available in your subject area

What if you want to install every package mentioned in a Task View

Package ctv will do just that:

install.packages("ctv")  # only if not already installed
library(ctv)
available.views() # will display the names of each view, R is case-sensitive, so you need the exact name
install.views("MachineLearning") # careful, this will download and install every package in that Task View
update.views("MachineLearning")

Working with packages

library() at the prompt will list all the packages installed on your system, and the directories where the are installed.

installed.packages() returns the list of installed packages as a dataframe.

In Conclusion

That is enough for now. Thanks.
_