The function that receives the warnings, or null to stop passing them.
The handler set before, or 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)"]
Sets the function that receives R's warnings, and returns the one it replaces (
nullwhen there was none). Not an R function. It plays the part of R's printing of warnings (and, by throwing, ofoptions(warn = 2)).Every test, model and fit returns R's warnings in its result's
warningswhatever the handler. With a handler set, each call of such a function (andpivotLongerwithoutonWarning) 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 aspairwiseWilcoxTest, 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'swarningsbut 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'soptions(warn = 2).The handler is kept on
globalThis, so it is one setting for every copy of the library loaded on a page.