Skip to content

Commit

Permalink
fix: Tweaks to various columns
Browse files Browse the repository at this point in the history
* ``register_hledger()`` now imports a transaction `id` column.
* ``register_beancount()`` now coerces the `date` column to a "Date" object.
  ``register_ledger()`` and ``register_hledger()`` already did so.
* ``register_ledger()`` now trims the `comment` column with `stringr::str_trim()`.
  • Loading branch information
trevorld committed May 19, 2024
1 parent cd80a77 commit 64893a9
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 159 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: ledger
Type: Package
Title: Utilities for Importing Data from Plain Text Accounting Files
Version: 2.0.10-0
Version: 2.0.10-2
Authors@R: c(person("Trevor L.", "Davis", role=c("aut", "cre"),
email="[email protected]",
comment = c(ORCID = "0000-0001-6341-4639")),
Expand All @@ -16,7 +16,7 @@ Imports:
stringr,
tidyr (>= 0.7.0),
tibble,
tidyselect,
tidyselect (>= 1.2.0),
tools
Suggests:
rio,
Expand Down
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
ledger 2.0.10 (development)
===========================

* ``register_beancount()`` now imports a transaction `id` column (#21).
* ``register_beancount()`` and ``register_hledger()`` now import a transaction `id` column (#21).
Thanks @vikasrawal for suggestion.
* ``register_beancount()`` now coerces the `date` column to a "Date" object.
``register_ledger()`` and ``register_hledger()`` already did so.
* ``register_ledger()`` now trims the `comment` column with `stringr::str_trim()`.

ledger 2.0.9
============
Expand Down
46 changes: 27 additions & 19 deletions R/register.r
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ register_beancount <- function(file, date = NULL) {
.system("bean-query", args)
}
df <- .read_csv(cfile)
if (nrow(df) == 0) {
if (nrow(df) == 0L) {
df <- tibble(date = as.Date(character()),
mark = character(),
account = character(),
Expand All @@ -169,18 +169,20 @@ register_beancount <- function(file, date = NULL) {
mv_commodity = character(),
tags = character(),
id = character())
} else {
df <- mutate(df,
date = as.Date(str_trim(.data$date)),
mark = str_trim(.data$mark),
account = str_trim(.data$account),
payee = str_trim(.data$payee),
description = str_trim(.data$description),
commodity = str_trim(.data$commodity),
hc_commodity = str_trim(.data$hc_commodity),
mv_commodity = str_trim(.data$mv_commodity),
tags = str_squish(.data$tags),
id = str_trim(.data$id))
.select_columns(df)
}
df <- mutate(df,
mark = str_trim(.data$mark),
account = str_trim(.data$account),
payee = str_trim(.data$payee),
description = str_trim(.data$description),
commodity = str_trim(.data$commodity),
hc_commodity = str_trim(.data$hc_commodity),
mv_commodity = str_trim(.data$mv_commodity),
tags = str_squish(.data$tags),
id = str_trim(.data$id))
.select_columns(df)
}

#' @rdname register
Expand Down Expand Up @@ -235,7 +237,7 @@ register_hledger <- function(file, flags = "", date = NULL, add_mark = TRUE, add
}

.clean_hledger <- function(df) {
if (nrow(df)) {
if (nrow(df) > 0L) {
df <- mutate(df, date = as.Date(date, tryFormats = c("%Y-%m-%d", "%Y/%m/%d")))
df <- mutate(df, description = ifelse(grepl("\\|$", .data$description),
paste0(.data$description, " "),
Expand All @@ -250,16 +252,23 @@ register_hledger <- function(file, flags = "", date = NULL, add_mark = TRUE, add

df <- mutate(df, amount = gsub(" @.*$", "", .data$amount))
df <- mutate(df, amount = to_numeric(.data$amount))
df <- mutate(df, id = as.character(.data$txnidx))

df <- select(df, .data$date, .data$description, .data$payee, .data$amount,
.data$commodity, .data$account, mark = .data$status)
df <- select(df, "date", "description", "payee", "amount",
"commodity", "account", mark = "status", "id")
} else {
df <- mutate(df, payee = .data$description, commodity = .data$amount)
df <- tibble(date = as.Date(character()),
description = character(),
payee = character(),
amount = numeric(),
commodity = character(),
account = character(),
mark = character(),
id = character())
}
df
}


.left_of_split <- function(strings, split) {
sapply(strsplit(strings, split), function(x) x[1])
}
Expand Down Expand Up @@ -298,7 +307,6 @@ register_ledger <- function(file, flags = "", date = NULL) {

.clean_ledger <- function(df) {
names(df) <- c("date", "V2", "description", "account", "commodity", "amount", "mark", "comment")

df <- mutate(df,
date = as.Date(date, "%Y/%m/%d"),
description = ifelse(grepl("\\|$", .data$description), paste0(.data$description, " "),
Expand All @@ -310,7 +318,7 @@ register_ledger <- function(file, flags = "", date = NULL) {
payee = ifelse(.data$payee == "", NA, .data$payee),
description = ifelse(.data$description == "", NA, .data$description),
payee = as.character(.data$payee),
comment = as.character(.data$comment)
comment = str_trim(as.character(.data$comment))
)
df
}
Expand Down
Loading

0 comments on commit 64893a9

Please sign in to comment.