# Load necessary libraries
library(httr)
library(jsonlite)
library(dplyr)
library(openxlsx)

# Store your API key in a variable
api_key <- "YOUR_API_KEY_HERE"

get_adverse_events <- function(drug_name, skip=0) {
  # Define the URL for the FDA API request
  url <- paste0("https://api.fda.gov/drug/event.json?search=patient.drug.medicinalproduct:", 
                URLencode(drug_name), "+AND+receivedate:[20220101+TO+20220105]&limit=100&skip=", skip)
  
  # Add the API key to the request headers
  headers <- add_headers(Authorization = paste("Bearer", api_key))
  
  response <- GET(url, headers)  # Make a GET request to the FDA API with headers
  if (status_code(response) == 200) {  # Check if the request was successful (status code 200)
    return(content(response, as="parsed"))  # Return the response data as a parsed JSON object
  } else {
    return(NULL)  # Return NULL if the request was not successful
  }
}

statins <- c("atorvastatin", "rosuvastatin", "simvastatin", "pravastatin", "lovastatin")
# Vector of statin drugs to search for adverse events

all_data <- data.frame()  # Initialize an empty data frame to store all adverse event data

for (statin in statins) {  # Loop through each statin in the vector
  skip <- 0  # Initialize the skip parameter to 0 for pagination
  repeat {  # Infinite loop to keep fetching data until no more results
    data <- get_adverse_events(statin, skip)  # Get adverse events data for the current statin
    if (is.null(data) || !("results" %in% names(data))) {  # Check if there is no data or no results
      break  # Exit the loop if no more data
    }
    events <- data$results  # Extract the events from the data
    for (i in seq_along(events)) {  # Loop through each event in the results
      adverse_event <- data.frame(
        Drug = paste0(toupper(substring(statin, 1, 1)), substring(statin, 2)),
        Reaction = ifelse(is.null(events[[i]]$patient$reaction[[1]]$reactionmeddrapt), NA, events[[i]]$patient$reaction[[1]]$reactionmeddrapt),
        Outcome = ifelse(is.null(events[[i]]$patient$reaction[[1]]$reactionoutcome), NA, events[[i]]$patient$reaction[[1]]$reactionoutcome),
        Date = ifelse(is.null(events[[i]]$receivedate), NA, events[[i]]$receivedate),
        Age = ifelse(is.null(events[[i]]$patient$patientonsetage), NA, events[[i]]$patient$patientonsetage),
        Age_Unit = ifelse(is.null(events[[i]]$patient$patientonsetageunit), NA, events[[i]]$patient$patientonsetageunit),
        Gender = ifelse(is.null(events[[i]]$patient$patientsex), NA, events[[i]]$patient$patientsex)
      )
      
      all_data <- bind_rows(all_data, adverse_event)  # Add the adverse event data to the data frame
    }
    skip <- skip + 100  # Increment the skip parameter to get the next set of results
  }
}

write.xlsx(all_data, "adverse_events_statins_r.xlsx", rowNames=FALSE)  # Save the data frame to an Excel file