R/read_transactions.R

#' Read Nordea transaction files.
#'
#' \code{read_transactions} parses a Nordea transaction file
#' and returns a clean tibble dataset object.
#'
#' @param file A full path to a Nordea transaction file, typically
#' named like "Transaactions_<ACCOUNT>_<DATE>_<DATE>.txt".
#'
#' @return A clean tibble dataset of the transactions.
#'
#' @export

read_transactions <- function(file) {

  new_col_names <- c(
    "record_date",
    "value_date",
    "transaction_date",
    "amount",
    "counterparty",
    "account_number",
    "bic_code",
    "transaction_type",
    "index_number",
    "counterparty_index_number",
    "message",
    "card_number",
    "receipt"
  )

  reader <- purrr::partial(
    readr::read_tsv,
    skip = 3,
    col_types = stringr::str_dup("c", 13),
    col_names = new_col_names
    ) %>%
    purrr::quietly()


  file %>%
    reader() %>%
    purrr::pluck("result") %>%
    dplyr::mutate(
        record_date = lubridate::dmy(record_date),
        value_date = lubridate::dmy(value_date),
        transaction_date = lubridate::dmy(transaction_date),
        amount = amount %>% stringr::str_replace(",", ".") %>% as.numeric()
    ) %>%
    dplyr::mutate_at(
        dplyr::vars(dplyr::contains("number")),
        stringr::str_remove_all,
        pattern = " "
    ) %>%
    dplyr::select(
        transaction_type,
        counterparty,
        dplyr::contains("date"),
        account_number,
        amount,
        everything()
    ) %>%
    arrange(transaction_date)

}
eteppo/r-nordea-tools documentation built on June 14, 2019, 3:48 a.m.