Skip to contents

Finds personal best times for specified distances from Strava activities.

Usage

calculate_pbs(
  stoken,
  activity_type = "Run",
  distance_meters,
  max_activities = 500,
  date_range = NULL
)

Arguments

stoken

A valid Strava token from `rStrava::strava_oauth()`.

activity_type

Type(s) of activities to search for PBs (e.g., "Run"). Note: Current logic relies on Strava's `best_efforts`, primarily available for Runs.

distance_meters

Numeric vector of distances (in meters) to find PBs for (e.g., `c(1000, 5000, 10000)`).

max_activities

Maximum number of recent activities to check. Default 500. Reducing this can speed up the process and help avoid API rate limits.

date_range

Optional. Filter activities within a date range `c("YYYY-MM-DD", "YYYY-MM-DD")`.

Value

A data frame containing all found best efforts for the specified distances. Includes columns: `activity_id`, `activity_date`, `distance`, `time_seconds` (elapsed time), `cumulative_pb_seconds` (the PB for that distance as of that date), `is_pb` (TRUE if this effort set a new PB), `distance_label` (e.g., "5k"), and `time_period` (formatted time).

Details

Fetches detailed activity data, extracts Strava's 'best efforts', and calculates cumulative PBs for specified distances.

Provides data for `plot_pbs`. Processes activities chronologically. Fetching detailed data is slow due to API limits (includes 1s delay per activity).

Examples

# 使用模拟数据示例 (注意: 模拟数据本身已是计算结果, 此处仅作演示)
data(Athlytics_sample_data)
if (!is.null(athlytics_sample_pbs)) {
  print(head(athlytics_sample_pbs))
}
#> # A tibble: 6 × 10
#>   activity_id activity_date       distance elapsed_time moving_time time_seconds
#>   <chr>       <dttm>                 <dbl>        <dbl>       <dbl>        <dbl>
#> 1 12846473912 2022-01-11 20:46:47     1000          326         326          326
#> 2 12846473209 2022-01-12 20:51:44     1000          302         302          302
#> 3 12846472820 2022-01-13 22:24:56     1000          315         315          315
#> 4 6928788461  2022-02-04 21:40:44     1000          348         348          348
#> 5 6928788329  2022-02-04 21:55:42     1000          592         592          592
#> 6 6907736431  2022-02-12 11:47:36     1000          314         314          314
#> # ℹ 4 more variables: cumulative_pb_seconds <dbl>, is_pb <lgl>,
#> #   distance_label <fct>, time_period <Period>

# \donttest{
# 使用真实数据的示例(需要认证)
stoken <- rStrava::strava_oauth(..., cache = TRUE)
#> Error: '...' used in an incorrect context

# 计算1k, 5k, 10k的PB (限制活动数量以提高速度)
pb_data <- calculate_pbs(stoken = stoken, 
                           distance_meters = c(1000, 5000, 10000),
                           max_activities = 100)
#> Error: object 'stoken' not found
print(head(pb_data))
#> Error: object 'pb_data' not found

# 仅显示创造新PB的成绩
new_pbs <- pb_data[pb_data$is_pb, ]
#> Error: object 'pb_data' not found
print(new_pbs)
#> Error: object 'new_pbs' not found
# }