rExtractTree function

String rExtractTree(
  1. String log
)

Extract from the R log lines of output from the decision tree.

Implementation

String rExtractTree(String log) {
  String extract = _basicTemplate(log);

  extract = extract.replaceAllMapped(
    RegExp(r'\nn= '),
    (match) {
      return '\nObservations = ';
    },
  );

  extract = extract.replaceAllMapped(
    RegExp(r'\n(Classification tree:)\n'),
    (match) {
      return '\n${match.group(1)}\n\n';
    },
  );

  // Nicely format the call to rpart.

  extract = extract.replaceAllMapped(
    RegExp(
      r'\n(rpart\(.*\))\)',
      multiLine: true,
      dotAll: true,
    ),
    (match) {
      // The first group is then the whole rpart(...) call.

      String txt = match.group(1) ?? '';

      txt = txt.replaceAll('\n', '');
      txt = txt.replaceAll(RegExp(r',\s*m'), ', m');

      txt = txt.replaceAllMapped(
        RegExp(r'(\w+)\s*=\s*([^,]+),'),
        (match) {
          return '\n    ${match.group(1)}=${match.group(2)},';
        },
      );

      txt = txt.replaceAll(' = ', '=');

      return '\n$txt\n)';
    },
  );

  extract = extract.replaceAllMapped(
    RegExp(r'\n(Variables actually used in.*)\n'),
    (match) {
      return '\n${match.group(1)}\n\n';
    },
  );

  return extract;
}