R/get_occurrences.R

#' get_occurrences
#'
#' \code{get_occurences} provides access to the Fogo Cruzado's API. The function returns a data frame with
#' the individual occurrences of shootings and the filters applied by the researcher.
#'
#' @param \code{source}: \code{numeric} the source of the occurence.
#' See \code{data_source} for the options. Default values gets all the sources
#' @param \code{agent}: \code{numeric} to filter if a police agent participates in the shooting.
#' See  \code{data_agent} for the options. Default value gets all the options
#' @param \code{state_name}: \code{character} to filter the results by State of the occurences.
#' See  \code{data_state_name} for the options.
#' @param \code{city_name}: \code{chracter} to filter the results by  the city of the occurences.
#' See  \code{data_city_name} for the options.
#' @param \code{state_id}: \code{numeric} to filter if a police agent participates in the shooting.
#' @param \code{add_token}: add the user's token to access the API.
#' @usage get_occurrences(source=NULL, agent=NULL, city_name=NULL, state_name=NULL, add_token)
#' @return it returns a dataframe of occurrences of shootings. See the API documentation for a comprehensive codebook of the output
#' @examples
#' data <- get_occurrences()
#' @import httr
#' @import rvest
#' @import purrr
#' @import jsonlite
#' @import tibble
#' @importFrom magrittr "%>%"
#' @export

get_occurrences <- function(source=NULL, agent=NULL,
                           city_name=NULL, state_name=NULL){


  #ERRORS

  error_source(source)

  #endpoint
  endpoint <- "/occurrences"


  #query
  query_list <- list(presen_agen_segur_ocorrencia=agent,
                     fonte_ocorrencia= source,
                     uf_estado=state_name,
                     nome_cidade=city_name)

  # Collect
  message("Collecting the data...")

  response <- httr::GET(paste0(base, endpoint),
                        add_headers('Authorization' = paste("Bearer", token, sep = " ")),
                        query=query_list)

  output <- jsonlite::fromJSON(httr::content(response, as="text", encoding = "UTF-8")) %>%
    tibble::as_tibble()


}
TiagoVentura/get_fogocruzado documentation built on June 27, 2019, 12:19 a.m.