R/csv_handler.R

Defines functions csv_handler

Documented in csv_handler

#' Requests clinicaltrials.gov API and Returns Parsed csv Response
#'
#' This function handles parsing of csv format API response. Returns a csv dataframe.
#' @param url A formatted string passed on to the API request
#' @keywords csv format response
#' @examples
#' csv_handler(url)
#'
csv_handler <- function(url) {

  # request
  response <- request_ct(url)
  # extract content
  con <- httr::content(response,"text")
  # parse response into records
  split_hdrs <- stringi::stri_split(con,regex = "\\n\\s*\\n")
  split_lines  <- stringi::stri_split_regex(split_hdrs[[1]],"\\n")
  records  <- read.table(text = split_lines[[2]], sep = ",", header = TRUE)
  
  
  return(records)
}
AdlCruz/rctapi documentation built on Jan. 28, 2024, 8:15 p.m.