Advertisement
Guest User

R Export Activities

a guest
Mar 15th, 2019
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.34 KB | None | 0 0
  1.  
  2. # using the function to export several CSV files
  3. exportCSV('Sport = "Bike"','2018/07/14','2019/12/31',"/home/majikstone/GoldenCheetah/export_mid2018_2019_Bike.csv");
  4.  
  5. exportCSV('Sport = "Bike"','2018/01/01','2019/12/31',"/home/majikstone/GoldenCheetah/export_2018_2019_Bike.csv");
  6.  
  7. exportCSV('Sport CONTAINS "Trainer"','2018/01/01','2019/12/31',"/home/majikstone/GoldenCheetah/export_2018_2019_Trainer.csv");
  8.  
  9.  
  10. # CSV export function
  11. # Parameters:
  12. #   filtText    - the text used to filter the activities, same format as for GC graphs
  13. #   startDate   - string in POSIX compatible format corresponding to the start date of the interval
  14. #   endDate     - string in POSIX compatible format corresponding to the end date of the interval
  15. #   outFileName - the destination (absolute or relative) and the filename of the exported CSV file
  16. exportCSV<-function(filtText,startDate,endDate,outFileName)
  17. {
  18.     # convert the dates to POSIXct variables, initialize other variables
  19.     sDate = as.POSIXct(startDate);
  20.     eDate = as.POSIXct(endDate);
  21.     noDatasets = 3;
  22.     init = 0;
  23.  
  24.     # get the full list of activities filtered by the filtered text
  25.     # the list is a list of POSIXct dates, that can be used to extract one single activity later
  26.     actDates <- GC.activities(filter=filtText);
  27.     # for each date in the list
  28.     for (date in actDates)
  29.     {
  30.         # if the date is valid
  31.         if (date >= sDate && date <= eDate)
  32.         {
  33.             # extract the activity with the given date
  34.             # remark: the date is actually the entire starting time of the activity
  35.             activity = GC.activity(date)[[1]];
  36.  
  37.             # the metrics are not processed here, we extract and concatenate raw data
  38.             # either initialize or concatenate the data vectors
  39.             # for now, only speed, cadence and HR are being extracted
  40.             if (init == 0)
  41.             {
  42.                 cadence = c(activity$cadence);
  43.                 hr = c(activity$heart.rate);
  44.                 speed = c(activity$speed);
  45.                 init = 1;
  46.             }
  47.             else
  48.             {
  49.                 cadence = c(cadence,activity$cadence);
  50.                 hr = c(hr,activity$heart.rate);
  51.                 speed = c(speed,activity$speed);
  52.             }
  53.         }
  54.     }
  55.  
  56.     # filter out all NA values (bad readings)
  57.     # if a value is filtered out from one array, the corresponding values from the other arrays have to be filtered out as well
  58.     # we do that to make sure the datasets are still coherent one with the other
  59.     cadence = cadence[!is.na(cadence)];
  60.     speed = speed[!is.na(cadence)];
  61.     hr = hr[!is.na(cadence)];
  62.  
  63.     cadence = cadence[!is.na(speed)];
  64.     speed = speed[!is.na(speed)];
  65.     hr = hr[!is.na(speed)];
  66.  
  67.     cadence = cadence[!is.na(hr)];
  68.     speed = speed[!is.na(hr)];
  69.     hr = hr[!is.na(hr)];
  70.  
  71.     # we create and initialize a matrix to merge together all the datasets
  72.     len = length(speed);
  73.     csvMat = matrix(nrow=len, ncol=noDatasets);
  74.  
  75.     # we put each dataset on a column of the matrix
  76.     csvMat[,1] = speed;
  77.     csvMat[,2] = cadence;
  78.     csvMat[,3] = hr;
  79.  
  80.     # export the CSV file, with no headers and ; as a separator
  81.     write.table(csvMat, file=outFileName, col.names=FALSE, row.names=FALSE, sep=";");
  82.  
  83.     # display a success message
  84.     message("Exporting complete to: ",outFileName);
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement