Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # using the function to export several CSV files
- exportCSV('Sport = "Bike"','2018/07/14','2019/12/31',"/home/majikstone/GoldenCheetah/export_mid2018_2019_Bike.csv");
- exportCSV('Sport = "Bike"','2018/01/01','2019/12/31',"/home/majikstone/GoldenCheetah/export_2018_2019_Bike.csv");
- exportCSV('Sport CONTAINS "Trainer"','2018/01/01','2019/12/31',"/home/majikstone/GoldenCheetah/export_2018_2019_Trainer.csv");
- # CSV export function
- # Parameters:
- # filtText - the text used to filter the activities, same format as for GC graphs
- # startDate - string in POSIX compatible format corresponding to the start date of the interval
- # endDate - string in POSIX compatible format corresponding to the end date of the interval
- # outFileName - the destination (absolute or relative) and the filename of the exported CSV file
- exportCSV<-function(filtText,startDate,endDate,outFileName)
- {
- # convert the dates to POSIXct variables, initialize other variables
- sDate = as.POSIXct(startDate);
- eDate = as.POSIXct(endDate);
- noDatasets = 3;
- init = 0;
- # get the full list of activities filtered by the filtered text
- # the list is a list of POSIXct dates, that can be used to extract one single activity later
- actDates <- GC.activities(filter=filtText);
- # for each date in the list
- for (date in actDates)
- {
- # if the date is valid
- if (date >= sDate && date <= eDate)
- {
- # extract the activity with the given date
- # remark: the date is actually the entire starting time of the activity
- activity = GC.activity(date)[[1]];
- # the metrics are not processed here, we extract and concatenate raw data
- # either initialize or concatenate the data vectors
- # for now, only speed, cadence and HR are being extracted
- if (init == 0)
- {
- cadence = c(activity$cadence);
- hr = c(activity$heart.rate);
- speed = c(activity$speed);
- init = 1;
- }
- else
- {
- cadence = c(cadence,activity$cadence);
- hr = c(hr,activity$heart.rate);
- speed = c(speed,activity$speed);
- }
- }
- }
- # filter out all NA values (bad readings)
- # if a value is filtered out from one array, the corresponding values from the other arrays have to be filtered out as well
- # we do that to make sure the datasets are still coherent one with the other
- cadence = cadence[!is.na(cadence)];
- speed = speed[!is.na(cadence)];
- hr = hr[!is.na(cadence)];
- cadence = cadence[!is.na(speed)];
- speed = speed[!is.na(speed)];
- hr = hr[!is.na(speed)];
- cadence = cadence[!is.na(hr)];
- speed = speed[!is.na(hr)];
- hr = hr[!is.na(hr)];
- # we create and initialize a matrix to merge together all the datasets
- len = length(speed);
- csvMat = matrix(nrow=len, ncol=noDatasets);
- # we put each dataset on a column of the matrix
- csvMat[,1] = speed;
- csvMat[,2] = cadence;
- csvMat[,3] = hr;
- # export the CSV file, with no headers and ; as a separator
- write.table(csvMat, file=outFileName, col.names=FALSE, row.names=FALSE, sep=";");
- # display a success message
- message("Exporting complete to: ",outFileName);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement