rStats.js v1.3.0
    Preparing search index...

    Function setWarningHandler

    • Sets the function that receives R's warnings, and returns the one it replaces (null when there was none). Not an R function. It plays the part of R's printing of warnings (and, by throwing, of options(warn = 2)).

      Every test, model and fit returns R's warnings in its result's warnings whatever the handler. With a handler set, each call of such a function (and pivotLonger without onWarning) also passes its warnings to the handler once it has computed its result: each distinct warning once, in order, with the name of the function called (a function that calls another, such as pairwiseWilcoxTest, passes the warnings once, under its own name). A fit's warnings are passed once, when it is fitted (lm, glm, aov, lmer, ...): a function given a fit or a table (anova, aovSummary, Anova, glmSummary, etaSquared, lmerSummary, ...) keeps them in its result's warnings but passes only the warnings it adds. An error thrown by the handler propagates to the caller. By default there is no handler and nothing is printed.

      To print the warnings as R does, set (message, source) => console.warn(source + ": " + message). A handler that throws ((message) => { throw new Error(message); }) turns each warning into an error, as R's options(warn = 2).

      The handler is kept on globalThis, so it is one setting for every copy of the library loaded on a page.

      Parameters

      • handler: WarningHandler | null

        The function that receives the warnings, or null to stop passing them.

      Returns WarningHandler | null

      The handler set before, or null.

      TypeError if handler is neither a function nor null.

      const seen: string[] = [];
      const previous = setWarningHandler((message, source) => { seen.push(`${source}: ${message}`); });
      optim([1, 1], (p) => (p[0] - 2) ** 2 + (p[1] - 3) ** 2, { lower: [0, 0] }).warnings;
      // ["bounds can only be used with method L-BFGS-B (or Brent)"]
      setWarningHandler(previous);
      seen; // ["optim: bounds can only be used with method L-BFGS-B (or Brent)"]