Skip to content

Commit

Permalink
require all id arguments as character #104
Browse files Browse the repository at this point in the history
  • Loading branch information
fawda123 committed Oct 9, 2024
1 parent 995c333 commit c323477
Show file tree
Hide file tree
Showing 60 changed files with 181 additions and 102 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: rStrava
Type: Package
Title: Access the 'Strava' API
Version: 1.3.1
Date: 2024-03-26
Version: 1.3.1.9000
Date: 2024-10-09
Description: Functions to access data from the 'Strava v3 API' <https://developers.strava.com/>.
BugReports: https://github.com/fawda123/rStrava/issues
License: CC0
Expand Down
9 changes: 6 additions & 3 deletions R/athl_fun.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' Get data for an athlete by web scraping, does not require authentication.

#' @param athl_num numeric vector of athlete id(s) used by Strava
#' @param athl_num numeric vector of athlete id(s) used by Strava, as character string
#' @param trace logical indicating if output is returned to console
#'
#' @export
Expand All @@ -15,12 +15,15 @@
#'
#' @examples
#' ## single athlete
#' athl_fun(2837007)
#' athl_fun('2837007')
#'
#' ## multiple athletes
#' athl_fun(c(2837007, 2527465))
#' athl_fun(c('2837007', '2527465'))
athl_fun <- function(athl_num, trace = TRUE){

if(any(!is.character(athl_num)))
stop('athl_num must be a character vector')

# allocate empty list
out <- vector('list', length(athl_num))
names(out) <- as.character(athl_num)
Expand Down
5 changes: 4 additions & 1 deletion R/athlind_fun.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' Get data for a single athlete by web scraping, does not require authentication.

#' @param athl_num numeric athlete id used by Strava
#' @param athl_num numeric athlete id used by Strava, as character string
#'
#' @import RCurl XML xml2
#'
Expand All @@ -13,6 +13,9 @@
#' @return A list with elements for the athlete's information.
athlind_fun <- function(athl_num){

if(any(!is.character(athl_num)))
stop('athl_num must be a character vector')

# get unparsed url text using input
url_in <- paste0('https://www.strava.com/athletes/', athl_num)

Expand Down
8 changes: 6 additions & 2 deletions R/compile_activities.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#'
#' @param actlist an activities list returned by \code{\link{get_activity_list}}
#' @param acts numeric indicating which activities to compile starting with most recent, defaults to all
#' @param id optional numeric vector to specify the id(s) of the activity/activities to plot, \code{acts} is ignored if provided
#' @param id optional character vector to specify the id(s) of the activity/activities to plot, \code{acts} is ignored if provided
#' @param units chr string indicating metric or imperial
#'
#' @author Daniel Padfield
Expand Down Expand Up @@ -33,6 +33,10 @@
#' }
compile_activities <- function(actlist, acts = NULL, id = NULL, units = 'metric'){

# check id is character
if(!is.null(id) & any(!is.character(id)))
stop('id must be a character vector')

# check units
if(!units %in% c('metric', 'imperial'))
stop('units must be metric or imperial')
Expand Down Expand Up @@ -63,7 +67,7 @@ compile_activities <- function(actlist, acts = NULL, id = NULL, units = 'metric'
out <- purrr::map_dfr(actlist, compile_activity, columns = att)

# convert relevant columns to numeric
cols <- c('id', 'achievement_count', 'athlete.resource_state', 'athlete_count', 'average_speed', 'average_watts', 'comment_count', 'distance', 'elapsed_time', 'elev_high', 'elev_low', 'end_latlng1', 'end_latlng2', 'kilojoules', 'kudos_count', 'map.resource_state', 'max_speed', 'moving_time', 'photo_count', 'resource_state', 'start_latitude', 'start_latlng1', 'start_latlng2', 'start_longitude', 'total_elevation_gain', 'total_photo_count')
cols <- c('achievement_count', 'athlete.resource_state', 'athlete_count', 'average_speed', 'average_watts', 'comment_count', 'distance', 'elapsed_time', 'elev_high', 'elev_low', 'end_latlng1', 'end_latlng2', 'kilojoules', 'kudos_count', 'map.resource_state', 'max_speed', 'moving_time', 'photo_count', 'resource_state', 'start_latitude', 'start_latlng1', 'start_latlng2', 'start_longitude', 'total_elevation_gain', 'total_photo_count')
cols <- names(out)[names(out) %in% cols]
out <- dplyr::mutate_at(out, cols, as.numeric)

Expand Down
7 changes: 5 additions & 2 deletions R/compile_activity_streams.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
#' @author Lorenzo Gaborini
#' @details used internally in \code{\link{get_activity_streams}}
#' @param streams a list containing details of the Strava streams of a single activity (output of \code{\link{get_streams}})
#' @param id if not missing, the activity id of the stream (will be appended to the data.frame, if non-empty)
#' @param id if not missing, the activity id of the stream (will be appended to the data.frame, if non-empty), as character vector
#' @return data frame where every column is the stream data for the retrieved types.
#' @concept token
#' @importFrom magrittr %>%
#' @examples
#' \dontrun{
#' stoken <- httr::config(token = strava_oauth(app_name, app_client_id, app_secret, cache = TRUE))
#'
#' act_id <- 351217692
#' act_id <- '351217692'
#' streams <- get_streams(stoken, id = act_id, types = list('distance', 'latlng'))
#'
#' compile_activity_streams(streams, id = act_id)}
compile_activity_streams <- function(streams, id = NULL){

if(any(!is.character(id)))
stop('id must be a character vector')

if (!is.null(id) && length(id) != 1) {
stop('id must be a scalar or NULL.')
}
Expand Down
6 changes: 3 additions & 3 deletions R/compile_segment.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
#' app_secret, cache = TRUE))
#'
#' # compile segment info
#' get_segment(stoken, id = 229781) %>% compile_segment
#' get_segment(stoken, id = '229781') %>% compile_segment
#'
#' # compile top ten leaderboard for the segment
#' get_segment(stoken, id = 229781, request = "leaderboard") %>% compile_segment
#' get_segment(stoken, id = '229781', request = "leaderboard") %>% compile_segment
#'
#' # compile all efforts for the authenticated user on the segment
#' get_segment(stoken, id = 4483903, request = 'all_efforts') %>% compile_segment
#' get_segment(stoken, id = '4483903', request = 'all_efforts') %>% compile_segment
#'
#' # compile the starred segments for the user
#' get_segment(stoken, request = 'starred') %>% compile_segment
Expand Down
7 changes: 5 additions & 2 deletions R/get_KOMs.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' Get KOMs/QOMs/CRs of an athlete
#'
#' @param id string or integer of athlete
#' @param id string of athlete id
#' @param stoken A \code{\link[httr]{config}} object created using the \code{\link{strava_oauth}} function
#'
#' @details Requires authentication stoken using the \code{\link{strava_oauth}} function and a user-created API on the strava website.
Expand All @@ -22,10 +22,13 @@
#' stoken <- httr::config(token = strava_oauth(app_name, app_client_id,
#' app_secret, cache = TRUE))
#'
#' get_KOMs(2837007, stoken)
#' get_KOMs('2837007', stoken)
#' }
get_KOMs <- function(id, stoken){

if(any(!is.character(id)))
stop('id must be a character vector')

url_ <- paste(url_athlete(id),"/koms", sep = "")
dataRaw <- get_basic(url_, stoken)
return(dataRaw)
Expand Down
7 changes: 5 additions & 2 deletions R/get_activity.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' Get detailed data of an activity, including segment efforts
#'
#' @param id numeric for id of the activity
#' @param id character vector for id of the activity
#' @param stoken A \code{\link[httr]{config}} object created using the \code{\link{strava_oauth}} function
#'
#' @details Requires authentication stoken using the \code{\link{strava_oauth}} function and a user-created API on the strava website.
Expand All @@ -24,10 +24,13 @@
#' stoken <- httr::config(token = strava_oauth(app_name, app_client_id,
#' app_secret, cache = TRUE))
#'
#' get_activity(75861631, stoken)
#' get_activity('75861631', stoken)
#' }
get_activity <- function(id, stoken){

if(any(!is.character(id)))
stop('id must be a character vector')

req <- GET(url_activities(id), stoken, query = list(include_all_efforts=TRUE))
stop_for_status(req)
dataRaw <- content(req)
Expand Down
5 changes: 4 additions & 1 deletion R/get_activity_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' Get an activities list of the desired type (club, user)
#'
#' @param stoken A \code{\link[httr]{config}} object created using the \code{\link{strava_oauth}} function
#' @param id numeric for id of the activity or club if \code{club = TRUE}, leave blank to retrieve all activities
#' @param id character vector for id of the activity or club if \code{club = TRUE}, leave blank to retrieve all activities
#' @param club logical if you want the activities of a club
#' @param before date object for filtering activities before the indicated date
#' @param after date object for filtering activities after the indicated date
Expand Down Expand Up @@ -38,6 +38,9 @@ get_activity_list <- function(stoken, id = NULL, before = NULL, after = NULL, cl
if(!is.null(id) & !club){
dataRaw <- list()

if(any(!is.character(id)))
stop('id must be a character vector')

for(i in id){

tmp <- get_pages(url_activities(id = i), stoken, All=TRUE)
Expand Down
2 changes: 1 addition & 1 deletion R/get_activity_streams.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#'
#' @param act_data an \code{list} object returned by \code{\link{get_activity_list}} or a \code{data.frame} returned by \code{\link{compile_activities}}
#' @param acts numeric indicating which activities to compile starting with most recent, defaults to all
#' @param id optional numeric vector to specify the id(s) of the activity/activities to plot, \code{acts} is ignored if provided
#' @param id optional character vector to specify the id(s) of the activity/activities to plot, \code{acts} is ignored if provided
#' @param stoken A \code{\link[httr]{config}} object created using the \code{\link{strava_oauth}} function
#' @param types list indicating which streams to get for each activity, defaults to all available, see details.
#' @param resolution chr string for the data resolution to retrieve, can be "low", "medium", "high", defaults to all
Expand Down
5 changes: 4 additions & 1 deletion R/get_athlete.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' Get basic athlete data for an athlete using an API request
#'
#' @param stoken A \code{\link[httr]{config}} object created using the \code{\link{strava_oauth}} function
#' @param id string or integer of athlete
#' @param id string of athlete
#'
#' @details Requires authentication stoken using the \code{\link{strava_oauth}} function and a user-created API on the strava website.
#'
Expand All @@ -26,6 +26,9 @@
#' }
get_athlete <-function(stoken, id = NULL){

if(any(!is.character(id)))
stop('id must be a character vector')

dataRaw <- get_basic(url_athlete(id), stoken)
return(dataRaw)

Expand Down
2 changes: 1 addition & 1 deletion R/get_club.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' Get club data for a given request
#'
#' @param stoken A \code{\link[httr]{config}} object created using the \code{\link{strava_oauth}} function
#' @param id numeric for id of the club, defaults to authenticated club of the athlete
#' @param id character vector for id of the club, defaults to authenticated club of the athlete
#' @param request chr string, must be "members", "activities" or \code{NULL} for club details
#'
#' @details Requires authentication stoken using the \code{\link{strava_oauth}} function and a user-created API on the strava website.
Expand Down
9 changes: 6 additions & 3 deletions R/get_efforts_list.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#' Get all the efforts in a segment if no queries are specified
#'
#' @param stoken A \code{\link[httr]{config}} object created using the \code{\link{strava_oauth}} function
#' @param id numeric for id of the segment
#' @param athlete_id numeric for the athlete id for filtering the results
#' @param id character string for id of the segment
#' @param athlete_id character string for the athlete id for filtering the results
#' @param start_date_local the start date for filtering the results
#' @param end_date_local the end date for filtering the results
#'
Expand All @@ -25,10 +25,13 @@
#' stoken <- httr::config(token = strava_oauth(app_name, app_client_id,
#' app_secret, cache = TRUE))
#'
#' get_efforts_list(stoken, id = 229781)
#' get_efforts_list(stoken, id = '229781')
#' }
get_efforts_list <- function(stoken, id, athlete_id=NULL, start_date_local=NULL, end_date_local=NULL){

if(any(!is.character(id)))
stop('id must be a character vector')

queries <- list(athlete_id=athlete_id,
start_date_local=start_date_local,
end_date_local=end_date_local)
Expand Down
5 changes: 4 additions & 1 deletion R/get_elev_prof.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#'
#' @param act_data an activities list object returned by \code{\link{get_activity_list}} or a \code{data.frame} returned by \code{\link{compile_activities}}
#' @param acts numeric value indicating which elements of \code{act_data} to plot, defaults to most recent
#' @param id optional numeric vector to specify the id(s) of the activity/activities to plot, \code{acts} is ignored if provided
#' @param id optional character vector to specify the id(s) of the activity/activities to plot, \code{acts} is ignored if provided
#' @param key chr string of Google API key for elevation data, passed to \code{\link[googleway]{google_elevation}}, see details
#' @param total logical indicating if elevations are plotted as cumulative climbed by distance
#' @param expand a numeric multiplier for expanding the number of lat/lon points on straight lines. This can create a smoother elevation profile. Set \code{expand = 1} to suppress this behavior.
Expand Down Expand Up @@ -48,6 +48,9 @@ get_elev_prof <- function(act_data, ...) UseMethod('get_elev_prof')
#' @method get_elev_prof list
get_elev_prof.list <- function(act_data, acts = 1, id = NULL, key, total = FALSE, expand = 10, units = 'metric', fill = 'darkblue', ...){

if(!is.null(id) & any(!is.character(id)))
stop('id must be a character vector')

# compile
act_data <- compile_activities(act_data, acts = acts, id = id, units = units)

Expand Down
5 changes: 4 additions & 1 deletion R/get_gear.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
#' stoken <- httr::config(token = strava_oauth(app_name, app_client_id,
#' app_secret, cache = TRUE))
#'
#' get_gear("g2275365", stoken)
#' get_gear("2275365", stoken)
#' }
get_gear <- function(id, stoken){

if(any(!is.character(id)))
stop('id must be a character vector')

url_ <- url_gear(id)
dataRaw <- get_basic(url_, stoken)
return(dataRaw)
Expand Down
5 changes: 4 additions & 1 deletion R/get_heat_map.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#' @param act_data an activities list object returned by \code{\link{get_activity_list}}, an \code{actframe} returned by \code{\link{compile_activities}}, or a \code{strfame} returned by \code{\link{get_activity_streams}}
#' @param key chr string of Google API key for elevation data, passed to \code{\link[googleway]{google_elevation}} for polyline decoding, see details
#' @param acts numeric indicating which activities to plot based on index in the activities list, defaults to most recent
#' @param id optional numeric vector to specify the id(s) of the activity/activities to plot, \code{acts} is ignored if provided
#' @param id optional character vector to specify the id(s) of the activity/activities to plot, \code{acts} is ignored if provided
#' @param alpha the opacity of the line desired. A single activity should be 1. Defaults to 0.5
#' @param add_elev logical indicating if elevation is shown by color shading on the activity lines
#' @param as_grad logical indicating if elevation is plotted as percent gradient, applies only if \code{add_elev = TRUE}
Expand Down Expand Up @@ -64,6 +64,9 @@ get_heat_map <- function(act_data, ...) UseMethod('get_heat_map')
#' @method get_heat_map list
get_heat_map.list <- function(act_data, key, acts = 1, id = NULL, alpha = NULL, add_elev = FALSE, as_grad = FALSE, distlab = TRUE, distval = 0, size = 0.5, col = 'red', expand = 10, maptype = 'CartoDB.Positron', zoom = 14, units = 'metric', ...){

if(!is.null(id) & any(!is.character(id)))
stop('id must be a character vector')

# compile
act_data <- compile_activities(act_data, acts = acts, id = id, units = units)

Expand Down
7 changes: 5 additions & 2 deletions R/get_laps.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' Retrieve the laps of an activity
#'
#' @param stoken A \code{\link[httr]{config}} object created using the \code{\link{strava_oauth}} function
#' @param id numeric for id of the activity with the laps to request
#' @param id character for id of the activity with the laps to request
#'
#' @details Requires authentication stoken using the \code{\link{strava_oauth}} function and a user-created API on the strava website.
#'
Expand All @@ -21,10 +21,13 @@
#' stoken <- httr::config(token = strava_oauth(app_name, app_client_id,
#' app_secret, cache = TRUE))
#'
#' get_laps(stoken, id = 351217692)
#' get_laps(stoken, id = '351217692')
#' }
get_laps <- function(stoken, id){

if(any(!is.character(id)))
stop('id must be a character vector')

if (length(id) != 1){
stop('only one activity id can be requested')
}
Expand Down
7 changes: 5 additions & 2 deletions R/get_leaderboard.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' Retrieve the leaderboard of a segment
#'
#' @param stoken A \code{\link[httr]{config}} object created using the \code{\link{strava_oauth}} function
#' @param id numeric for id of the segment
#' @param id charcater for id of the segment
#' @param nleaders numeric for number of leaders to retrieve
#' @param All logical to retrieve all of the list
#'
Expand All @@ -24,10 +24,13 @@
#' stoken <- httr::config(token = strava_oauth(app_name, app_client_id,
#' app_secret, cache = TRUE))
#'
#' get_leaderboard(stoken, id = 229781)
#' get_leaderboard(stoken, id = '229781')
#' }
get_leaderboard <- function(stoken, id, nleaders = 10, All = FALSE){

if(any(!is.character(id)))
stop('id must be a character vector')

dataRaw <- get_pages(paste0('https://www.strava.com/api/v3/segment_efforts?segment_id=', id), stoken, per_page = nleaders, All = All)

return(dataRaw)
Expand Down
11 changes: 7 additions & 4 deletions R/get_segment.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' Retreive details about a specific segment
#'
#' @param stoken A \code{\link[httr]{config}} object created using the \code{\link{strava_oauth}} function
#' @param id numeric for id of the segment
#' @param id character for id of the segment
#' @param request chr string, must be "starred", "leaderboard", "all_efforts", or NULL for segment details
#'
#' @details Requires authentication stoken using the \code{\link{strava_oauth}} function and a user-created API on the strava website. The authenticated user must have an entry for a segment to return all efforts if \code{request = "all_efforts"}. For \code{request = "starred"}, set \code{id = NULL}.
Expand All @@ -25,19 +25,22 @@
#' app_secret, cache = TRUE))
#'
#' # get segment info
#' get_segment(stoken, id = 229781)
#' get_segment(stoken, id = '229781')
#'
#' # get top ten leaderboard for the segment
#' get_segment(stoken, id = 229781, request = "leaderboard")
#' get_segment(stoken, id = '229781', request = "leaderboard")
#'
#' # get all efforts for the authenticated user on the segment
#' get_segment(stoken, id = 4483903, request = 'all_efforts')
#' get_segment(stoken, id = '4483903', request = 'all_efforts')
#'
#' # get the starred segments for the user
#' get_segment(stoken, request = 'starred')
#' }
get_segment <- function(stoken, id = NULL, request = NULL){

if(!is.null(id) & any(!is.character(id)))
stop('id must be a character vector')

dataRaw <- get_basic(url_segment(id, request = request), stoken)
return(dataRaw)

Expand Down
Loading

0 comments on commit c323477

Please sign in to comment.