Code examples that (mainly) use concepts from Session 1
Finding the number of the active device, starting from from
int nextDevice(int from)
{
if (R_NumDevices == 1)
return 0;
else {
int i = from;
int nextDev = 0;
while ((i < (R_MaxDevices-1)) && (nextDev == 0))
if (active[++i]) nextDev = i;
if (nextDev == 0) {
/* start again from 1 */
i = 0;
while ((i < (R_MaxDevices-1)) && (nextDev == 0))
if (active[++i]) nextDev = i;
}
return nextDev;
}
}
Creating a 3 x 3 identity matrix (part of wider code base related to transformation matrices).
void identity(LTransform m)
{
int i, j;
for (i=0; i<3; i++)
for (j=0; j<3; j++)
if (i == j)
m[i][j] = 1;
else
m[i][j] = 0;
}
LTransfrom
type defined here: https://github.com/r-devel/r-svn/blob/2c0ed92eae8a9845b42dfcd984b9942e751e57a6/src/library/grid/src/grid.h#L153
Compute a "padj" value, which is 0 if the axis label should be parallel to the axis and 0.5 if it should be perpendicular. (See ?par
for the definition of las
in R;
side
numbers the sides of the plot from the bottom, in a clockwise direction.)
static double ComputePAdjValue(double padj, int side, int las)
{
if (!R_FINITE(padj)) {
switch(las) {
case 0:/* parallel to axis */
padj = 0.0; break;
case 1:/* horizontal */
switch(side) {
case 1:
case 3: padj = 0.0; break;
case 2:
case 4: padj = 0.5; break;
}
break;
case 2:/* perpendicular to axis */
padj = 0.5; break;
case 3:/* vertical */
switch(side) {
case 1:
case 3: padj = 0.5; break;
case 2:
case 4: padj = 0.0; break;
}
break;
}
}
return padj;
}
Generate a random value from an
double rf(double n1, double n2)
{
double v1, v2;
if (ISNAN(n1) || ISNAN(n2) || n1 <= 0. || n2 <= 0.)
ML_WARN_return_NAN;
v1 = R_FINITE(n1) ? (rchisq(n1) / n1) : 1;
v2 = R_FINITE(n2) ? (rchisq(n2) / n2) : 1;
return v1 / v2;
}