The decimal point is at the |
  8 | 
  8 | 5
  9 | 
  9 | 6
  10 | 0
  The decimal point is at the |
  2 | 00ellakaye.github.io/c-for-r-users
Fostering a larger, more diverse community of contributors to base R
stem() displayfunction (x, scale = 1, width = 80, atom = 1e-08) 
{
    if (!is.numeric(x)) 
        stop("'x' must be numeric")
    x <- x[is.finite(x)]
    n <- as.integer(length(x))
    if (is.na(n)) 
        stop("invalid length(x)")
    if (n == 0) 
        stop("no finite and non-missing values")
    if (scale <= 0) 
        stop("'scale' must be positive")
    .Call(C_StemLeaf, as.double(x), scale, width, atom)
    invisible(NULL)
}Error in eval(expr, envir, enclos): object 'C_StemLeaf' not foundC_StemLeaf()R’s C API
SEXP C_StemLeaf(SEXP x, SEXP scale, SEXP swidth, SEXP atom)
{
    if (TYPEOF(x) != REALSXP || TYPEOF(scale) != REALSXP) 
        error("invalid input");
#ifdef LONG_VECTOR_SUPPORT
    if (IS_LONG_VEC(x))
        error(_("long vector '%s' is not supported"), "x");
#endif
    int width = asInteger(swidth), n = LENGTH(x);
    if (n == NA_INTEGER) error(_("invalid '%s' argument"), "x");
    if (width == NA_INTEGER) error(_("invalid '%s' argument"), "width");
    double sc = asReal(scale), sa = asReal(atom);
    if (!R_FINITE(sc)) error(_("invalid '%s' argument"), "scale");
    if (!R_FINITE(sa)) error(_("invalid '%s' argument"), "atom");
    stem_leaf(REAL(x), n, sc, width, sa);
    return R_NilValue;
}stem_leaf()static Rboolean
stem_leaf(double *x, int n, double scale, int width, double atom)
{
    // <initialise variables>
    R_rsort(x,n);
    if (n <= 1) return FALSE;
    //<more code here>
    
    /* Find the print width of the stem. */
    lo = floor(x[0]*c/mu)*mu;
    hi = floor(x[n-1]*c/mu)*mu;
    ldigits = (lo < 0) ? (int) floor(log10(-(double)lo)) + 1 : 0;
    hdigits = (hi > 0) ? (int) floor(log10((double)hi)): 0;
    ndigits = (ldigits < hdigits) ? hdigits : ldigits;
    /* Starting cell */
    if(lo < 0 && floor(x[0]*c) == lo) lo = lo - mu;
    hi = lo + mu;
    if(floor(x[0]*c+0.5) > hi) {
          lo = hi;
          hi = lo + mu;
    }
  // <more code here>
}We’ve seen .Call(). In base R, there’s also .Internal() and .Primitive()
e.g. the source code for tabulate includes:
We can find the underlying code on GitHub with
We can also use pryr to find the SEXTYPE:
/* #RRGGBB[AA] String to Internal Color Code */
static rcolor rgb2col(const char *rgb)
{
    unsigned int r = 0, g = 0, b = 0, a = 0; /* -Wall */
    if(rgb[0] != '#')
          error(_("invalid RGB specification"));
    switch (strlen(rgb)) {
    case 9:
          a = 16 * hexdigit(rgb[7]) + hexdigit(rgb[8]);
    case 7:
          r = 16 * hexdigit(rgb[1]) + hexdigit(rgb[2]);
          g = 16 * hexdigit(rgb[3]) + hexdigit(rgb[4]);
          b = 16 * hexdigit(rgb[5]) + hexdigit(rgb[6]);
          break;
    default:
          error(_("invalid RGB specification"));
    }
    if(strlen(rgb) == 7) 
        return R_RGB(r, g, b);
    else
        return R_RGBA(r, g, b, a);
}switch (strlen(rgb)) {
case 9:
    a = 16 * hexdigit(rgb[7]) + hexdigit(rgb[8]);
case 7:
    r = 16 * hexdigit(rgb[1]) + hexdigit(rgb[2]);
    g = 16 * hexdigit(rgb[3]) + hexdigit(rgb[4]);
    b = 16 * hexdigit(rgb[5]) + hexdigit(rgb[6]);
    break;
case 5: 
  // Equivalent to 16 * hexdigit(rgb[4]) + hexdigit(rgb[4]);
  a = (16 + 1) * hexdigit(rgb[4]);
case 4:
  r = (16 + 1) * hexdigit(rgb[1]);
  g = (16 + 1) * hexdigit(rgb[2]);
  b = (16 + 1) * hexdigit(rgb[3]);
  break;
default:
    error(_("invalid RGB specification"));
}From
to
https://contributor.r-project.org/events/c-study-group-2024/
Will run again January–June 2025, details TBC
Monthly meetings, weekly suggestions
Work through sessions 1-5 of Harvard’s CS50 course
R’s C API
Run by R Contribution Working Group (RCWG)
Fosters a larger, more diverse community of contributors to base R.
ellakaye.github.io/c-for-r-users