rExtractGlimpse function

String rExtractGlimpse(
  1. String txt
)

Implementation

String rExtractGlimpse(String txt) {
  // If it appears that R has not yet finished loading the dataset then return
  // an appropriate message.

  // if (txt.contains("Error: object 'ds' not found")) {
  //   return ('The dataset appears to still be loading. Please wait.');
  // }

  // Split the string into lines.

  List<String> lines = txt.split('\n');

  List<String> result = [];

  // Initialize with a value that indicates no start index found.

  int startIndex = -1;

  // If the dataset is loaded from a CSV file or the demo dataset it will be
  // summarised by R's `glimpse(ds)` command. For a TXT file we use `cat(ds,
  // sep = "\n")`. In either case we find the latest instance and return the
  // output for display in RattleNG.

  for (int i = lines.length - 1; i >= 0; i--) {
    if (lines[i].contains('> glimpse(ds') || lines[i].contains('> cat(ds,')) {
      startIndex = i;
      break;
    }
  }

  if (startIndex != -1) {
    for (int i = startIndex + 1; i < lines.length; i++) {
      if (lines[i].startsWith('>')) {
        // Found the next line starting with '>'. Stop adding lines to the
        // result.

        break;
      }

      // Remove '$ ' at the start.

      String strippedLine = lines[i].replaceAll(RegExp(r'^\$ '), '');

      result.add(strippedLine);
    }
  }

  // Add a blank line after the first two lines.

  if (result.length >= 2) {
    // Insert an empty string at index 2.

    result.insert(2, '');
  }

  // Join the lines.

  return result.join('\n');
}