## How do I temporarily install an R package I am working on?

You don't need to, if it has been checked, then it is already installed.

R CMD check foo
R --vanilla
library(foo, lib.loc = "foo.Rcheck")


## How do I install an R package if I don't have administrator privileges?

You install it somewhere where you do have write privileges (on linux, in your home directory). Say you want to install in (under linux) in ~/library.

R CMD INSTALL --library=~/library foo_0.1-2.tar.gz


will install the library from the unix command line and

install.packages("foo", lib = "~/library")

will install a library on CRAN from the R command line.

Then to use the library put a file .Renviron in your home directory that contains the single line

R_LIBS = ~/library


Then R will always know to always also look there for installed packages (in addition to the library subdirectory of the RHOME directory).

## Where is the R API documentation?

Documentation? We don't need need no stinkin' documentation. Real programmers don't do documentation (see also the legend of Mel the real programmer).

There ain't no documentation, you just have to RTFS.

As bad as this situation is, it is infinitely worse for closed source proprietary software. That you just can't extend. R you can. It is very well behaved with respect to extensions.

The R API consists of the C functions in the headers you can include when doing R CMD INSTALL. What are these? On our linux boxen do

cd R RHOME
pwd
find include -name '*.h'


This will list all the header files that can be included. Any C function that doesn't have a prototype in one of these headers is not part of the R API. Don't try to call it from C.

Any C function that does have a prototype in one of these headers is part of the R API. These include files constitute a contract between the R core team and application programmers that they will try very hard to not break any of these functions. If you use them, they should continue to work in the future.

### Source code

So suppose you have found a function you want to call, for definiteness say pbeta.
find include -name '*.h' | xargs grep pbeta


shows you the prototype, but what do the arguments do?

For that we have to RTFS and for that you have to get the R source and unpack it. Say

wget http://www.biometrics.mtu.edu/CRAN/src/base/R-2/R-2.10.1.tar.gz
tar zxf R-2.10.1.tar.gz


But here in the stat department we already have a copy.

cd /APPS/src/R-2.10.1
find src -name pbeta.c


This may not work because the file name does not have to reflect the function(s) defined in the file. You may have to do

find src -name '*.c' | xargs grep pbeta


This, of course, turns up not only the file pbeta.c where the function is defined but every place in the R codebase where this function is called.

## How do I turn on Compiler Flags in my R Package?

For example (the one done in class), to check the Fortran in your package, create a file name Makevars in the src directory of your package that contains the single line

PKG_FFLAGS = -fbounds-check -Wall -Wextra


For extra flags for C code use PKG_CFLAGS. For extra flags for C++ code use PKG_CXXFLAGS.

Warning: You cannot use these flags in the form of the package you ship to CRAN. They only allow standard compiler flags (whatever that means). But you can use this for debugging.

## What is the Difference Between the | and || Operators?

See the on-line help obtained by doing, for example help("||").

& and && indicate logical AND and | and || indicate logical OR. The shorter form performs elementwise comparisons in much the same way as arithmetic operators. The longer form evaluates left to right examining only the first element of each vector. Evaluation proceeds only until the result is determined. The longer form is appropriate for programming control-flow and typically preferred in if clauses.

In short, you want | when dealing with vectors. You may want || when dealing with if clauses.

## How do I Vectorize Logical And and Or?

The all (on-line help) and any (on-line help) functions do this.

## How to find LaTeX operator names?

One way is to look in Lamport (2nd ed) or the LaTeX Companion (2nd ed).

Another way is to use the Detexify2, the LaTeX symbol classifier (Sai Okabayashi told me about this one).

## How to do Null and Alternative Hypotheses in LaTeX?

\begin{align*}
H_0 & : h_1(t) = h_0(t),   \qquad \text{for all $t \in[0,\tau]$}
\\
H_a & : h_1(t) \ne h_0(t), \qquad \text{for some $t \in[0,\tau]$}
\end{align*}


## How do I do Math in LaTeX that has Goofy Word-Like Variables?

$D \times (1 - \text{EPS})$

or
$\text{D} \times (1 - \text{EPS})$


## How to do Clean Examples in R?

data(rats)
attach(rats)
twostage(T, DELTA, GROUP)


## How to Browse the C API?

The unix functions man and man -k find out all kinds of stuff about unix. Functions in the C standard library are in sections 2 and 3. They will say in the CONFORMING TO section C89 if they are in the C standard adopted in 1989 and C99 if they are in the C standard adopted in 1999.

man -k tangent

lists all the functions that have tangent in their description. And
man atanh

shows the manual page for the specific function atanh.

## How to Make BEAMER Work like Powerpoint?

I'm not sure I understood the question, and I hate BEAMER. It faithfully copies most of the vices of Powerpoint (there aren't any virtues).

It allows you to encumber your slides with a lot of fritterware that gives the audience some eye candy to look at instead listening to what you say. Real valuable!

I assumed the question was about doing things like this for which I used the good old-fashioned slides document class. Here is the LaTeX. It uses

• the color package to set the background and text colors,
• the graphicx package to include the JPG figure downloaded from the web, and
• the fancybox package to absolutely position the text so it goes over the figure.

An alternative to the fancybox package is the textpos package. I have not tried the latter, but I assume it works just as well.

Note: This file must be processed using pdflatex because the latex command only handles PostScript (not JPG) includes.

## How to use Valgrind when Not Checking a Package?

R --debugger=valgrind

runs R under valgrind.

Other flags can be combined with this, for example

R CMD BATCH --vanilla --debugger=valgrind foo.R


## How to Differentiate Sums in R?

An example: log likelihood for the Cauchy location model

logl <- expression(- log(1 + (x - theta)^2))
scor <- D(logl, "theta")
hess <- D(scor, "theta")

foo <- function(theta) list(value = sum(eval(logl)),
gradient = sum(eval(scor)), hessian = matrix(sum(eval(hess))))

### make up data
x <- rcauchy(30)

### do it
library(trust)
trust(foo, median(x), 0.5, 2, minimize = FALSE)


The trick is to use the sum rule yourself and take the derivative inside the sum.

## How to fit LM or GLM to large datasets?

R contributed package biglm (on-line help) may be of use.

## How to use the R formula mini-language?

See the function dr in the library dr by Sandy Weisberg.

library(dr)
dr


## How to make new operators in LaTeX?

In the preamble (the technical term for between \documentclass and \begin{document}) put

\usepackage{amsmath}
\DeclareMathOperator{\var}{var}


Then in your document $\var_\theta(X)$ does the right thing, something like varθ(X).

## How to change the interline space in a LaTeX table?

\begin{center}
\renewcommand{\arraystretch}{1.75}
\begin{tabular}{lc}
\hline
one thing & $x$ \\
and another &
$\frac{\int_0^\infty g(x) f(x) \, d x}{\int_0^\infty f(x) \, d x}$ \\
and yet another & $\frac{\frac{a + b}{c + d}}{\frac{e + f}{g + h}}$ \\
\hline
\end{tabular}
\end{center}


The secret is \renewcommand{\arraystretch}{1.75} which multiplies the interline spacing by a factor of 1.75. Because it is done inside an environment, it only applies to this one table.

## How to fix the way LaTeX spaces math formulas

In general you don't. TeX knows more about typesetting than you do.

The LaTeX eqnarray environment is so brain-damaged that it is impossible to use it to produce non-ugly mathematics. AMS LaTeX provides six different replacements to do a variety of jobs, all of which must be done and done poorly by eqnarray when you use plain LaTeX. All six of the AMS LaTeX replacements work beautifully.

There are two places where TeX doesn't know enough about math to know where to put in space and you have to do that.

Before the differential in integrals

\int_{- infty}^\infty f(x) \, d x


The \, makes a thin space. For all TeX knows, this is just f(x) times d times x. It doesn't know calculus. So you have to tell it about the extra space before a differential.

There are two kinds of set builder notation { a, b, c } and { xA : x < 3 }. The latter but not the former should have thin space inside the curly brackets. I write the latter in LaTeX as

\set{x \in A : x < 3}


where I have defined the macro

\newcommand{\set}[1]{\{\,#1\,\}}


in the preamble of the document (between \documentclass and \begin{document}).