checkRInstallation function

Future<bool> checkRInstallation()

Implementation

Future<bool> checkRInstallation() async {
  // Try to run the R command to check its availability.

  // 20250113 gjw Exploring Andriod deployment. For now ignore the check for R
  // installed.

  if (Platform.isAndroid) return true;

  // 20250924 adiar11 On macOS Rattle fails to launch when double-clicked if we
  // simply try to run R here. It only launches correctly when run from the
  // command line (`open rattle.app` or by executing the binary directly).
  //
  // The root cause is the system's PATH environment variable. When launched
  // from the Finder, the app gets a minimal PATH that does not include
  // `/usr/local/bin`, where the R executable is typically located, particularly
  // with homebrew installations. As a result, the app cannot find the R process
  // and crashes silently before a window can appear.
  //
  // When launching the app from the Terminal, the app inherits the shell's
  // PATH, which includes /usr/local/bin, so it can find R and runs perfectly.
  //
  // We need to make the app independent of the launch environment. By using
  // `Process.run()` here with an absolute path to the executable the app
  // launches perfectly with a double-click.
  //
  // We need to fix the absolute path for different installations of R. For now
  // we check for macOs and assume `/usr/local/bin/R` but eventually we need to
  // check for locations if not found in PATH.

  try {
    final result = await Process.run(shell, ['--version']);

    // Check if "R version" is present in the output.

    return result.exitCode == 0;
  } catch (e) {
    // R is not installed or not in PATH.

    return false;
  }
}