# Install required packages if you haven't already
# install.packages("plotly")
# install.packages("dplyr")

# Load the required libraries
library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# Assuming you have your data in a CSV file similar to the Python example
file_path <- "C:/Users/Adam Moffitt/Desktop/World Athletics STATS/fifteenhundred.prs.csv"

# Read the data
data <- read.csv(file_path, stringsAsFactors = FALSE)

# Convert the Date column to Date type and extract the year
data$Date <- as.Date(data$Date, format="%d %b %Y")
data$Year <- format(data$Date, "%Y")

# Count the number of runners per year
runners_per_year <- data %>%
  group_by(Year) %>%
  summarise(Count = n())

# Calculate the annual percent change
runners_per_year <- runners_per_year %>%
  arrange(Year) %>%
  mutate(Annual_Percent_Change = (Count / lag(Count) - 1) * 100)

# Create the interactive line plot
fig <- plot_ly(runners_per_year, x = ~Year, y = ~Count, type = 'scatter', mode = 'lines+markers',
               hoverinfo = 'text',
               text = ~paste('Year:', Year, '<br>Count of Runners:', Count, 
                             '<br>Annual % Change:', round(Annual_Percent_Change, 2), '%')) %>%
  layout(title = 'Count of Runners per Year with Annual Percent Change',
         xaxis = list(title = 'Year'),
         yaxis = list(title = 'Count of Runners'))

# Show the plot
fig