rLoadDataset function
- BuildContext context,
- WidgetRef ref
Load the specified dataset using the appropriate R script.
The R script is expected to load the data into the template variable ds,
and define dsname as the dataset name and vnames as a named list of the
original variable names having as values the current variable names, being
different in the case where the dataset variables have been normalised,
which is the default.
Implementation
Future<void> rLoadDataset(BuildContext context, WidgetRef ref) async {
// On loading a dataset we run the main R script to initialise a new session.
// Get the path to the dataset from the provider to identify either a filename
// or an R package dataset.
String path = ref.read(pathProvider);
// TODO 20231018 gjw IF A DATASET HAS ALREADY BEEN LOADED AND NOT YET
// PROCESSED (dataset_template.R) THEN PROCESS ELSE ASK IF WE CAN OVERWRITE IT
// AND IF SO DO SO OTHERWISE DO NOTHING.
debugText('R LOAD', path);
// Ensure there is no extraneous white space.
path = path.trim();
// R Scripts.
String sl = 'session_library';
String ss = 'session_setup';
String dc = 'dataset_load_csv';
String dx = 'dataset_load_txt';
String dp = 'dataset_prep'; // Dataset cleaning and prepartion pre-template.
String dfp = 'dataset_load_from_package';
String dxl = 'dataset_load_xls';
if (isFromPackage(path)) {
// 20250725 gjw This is not yet supported but ready when we support datasets
// from packages, like rattle::weather.
if (context.mounted) await rSource(context, ref, [sl, ss, dfp, dp]);
} else if (path.endsWith('.csv')) {
// 20241007 gjw We will load a CSV file into the R process. Note that we do
// not yet run the DATA TEMPLATE as we need to first set up the ROLES. The
// dataset template is run in `home.dart` on leaving the DATASET tab.
if (context.mounted) await rSource(context, ref, [sl, ss, dc, dp]);
ref.read(datatypeProvider.notifier).state = 'table';
} else if (path.endsWith('.xlsx')) {
// 20250309 gjw Load an Excel file into Rattle.
if (context.mounted) await rSource(context, ref, [sl, ss, dxl, dp]);
ref.read(datatypeProvider.notifier).state = 'table';
} else if (path.endsWith('.txt')) {
// 20241007 gjw We can also load a text file for the word cloud
// functionality as a stop gap toward implementing more complete text mining
// and language capabilities.
if (context.mounted) await rSource(context, ref, [sl, ss, dx]);
ref.read(datatypeProvider.notifier).state = 'text';
} else {
// 20241007 gjw Through the GUI we don't expect to be able to reach here.
debugPrint('LOAD_DATASET: PATH NOT RECOGNISED -> ABORT: $path.');
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('PATH NOT RECOGNISED'),
content: const Text('Unable to load dataset'),
actions: <Widget>[
ElevatedButton(
child: const Text('OK'),
onPressed: () => Navigator.of(context).pop(),
),
],
),
);
return;
}
debugText('R LOADED', path);
return;
}