Skip to content

Commit

Permalink
feat: Downgrade {rio} to Suggests from Imports
Browse files Browse the repository at this point in the history
* {rio} may be "archived" on CRAN...
* The R packgae `{rio}` has been downgraded from "Imports" to "Suggests".
  Users who want to use `rio::import()` or `rio::convert()`
  will need to manually install `{rio}`.
* Register S3 methods for {rio} in `.onLoad()` instead of NAMESPACE
  • Loading branch information
trevorld committed Nov 11, 2021
1 parent 5066f99 commit 5db00ba
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 39 deletions.
13 changes: 10 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
Package: ledger
Type: Package
Title: Utilities for Importing Data from Plain Text Accounting Files
Version: 2.0.8
Version: 2.0.9
Authors@R: c(person("Trevor L", "Davis", role = c("aut", "cre"), email="trevor.l.davis@gmail.com"),
person("Jenya", "Sovetkin", role="ctb"),
person("Chris", "Lloyd", role="ctb"))
URL: https://github.com/trevorld/r-ledger, https://trevorldavis.com/R/ledger
BugReports: https://github.com/trevorld/r-ledger/issues
Description: Utilities for querying plain text accounting files from 'Ledger', 'HLedger', and 'Beancount'.
Imports:
dplyr (>= 0.7.0), rio, rlang, stringr, tidyr (>= 0.7.0), tibble, tidyselect, tools
dplyr (>= 0.7.0),
rlang,
stringr,
tidyr (>= 0.7.0),
tibble,
tidyselect,
tools
Suggests:
rio,
testthat
SystemRequirements: ledger (>= 3.1), hledger (>= 1.2), beancount (>= 2.0)
License: MIT + file LICENSE
RoxygenNote: 7.1.0
RoxygenNote: 7.1.2
Encoding: UTF-8
6 changes: 0 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
# Generated by roxygen2: do not edit by hand

S3method(.import,rio_bean)
S3method(.import,rio_beancount)
S3method(.import,rio_hledger)
S3method(.import,rio_ledger)
export(default_toolchain)
export(net_worth)
export(prune_coa)
Expand All @@ -19,7 +14,6 @@ importFrom(dplyr,left_join)
importFrom(dplyr,mutate)
importFrom(dplyr,select)
importFrom(dplyr,summarize)
importFrom(rio,.import)
importFrom(rlang,":=")
importFrom(rlang,.data)
importFrom(rlang,enquo)
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
ledger 2.0.9
============

* The R packgae `{rio}` has been downgraded from "Imports" to "Suggests".
Users who want to use `rio::import()` or `rio::convert()`
will need to manually install `{rio}`.

ledger 2.0.7
============

Expand Down
13 changes: 0 additions & 13 deletions R/register.r
Original file line number Diff line number Diff line change
Expand Up @@ -335,16 +335,3 @@ register_ledger <- function(file, flags = "", date = NULL) {
if (!.is_toolchain_supported(toolchain))
stop(paste(toolchain, "binaries not found on path"))
}

#' @importFrom rio .import
#' @export
.import.rio_beancount <- register # nolint

#' @export
.import.rio_bean <- register # nolint

#' @export
.import.rio_ledger <- register # nolint

#' @export
.import.rio_hledger <- register # nolint
20 changes: 20 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.import.rio_beancount <- register # nolint
.import.rio_bean <- register # nolint
.import.rio_ledger <- register # nolint
.import.rio_hledger <- register # nolint

.onLoad <- function(...) {
register_s3_method("rio", ".import", "rio_beancount")
register_s3_method("rio", ".import", "rio_bean")
register_s3_method("rio", ".import", "rio_hledger")
register_s3_method("rio", ".import", "rio_ledger")

invisible(NULL)
}

register_s3_method <- function(pkg, generic, class) {
fun <- get(paste0(generic, ".", class), envir = parent.frame())
if (requireNamespace(pkg, quietly = TRUE))
registerS3method(generic, class, fun, envir = asNamespace(pkg))
invisible(NULL)
}
2 changes: 1 addition & 1 deletion README.Rrst
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ Examples
API
+++

The main function of this package is ``register`` which reads in the register of a plaintext accounting file. This package also exports S3 methods so one can use ``rio::import`` to read in a register, a ``net_worth`` convenience function, and a ``prune_coa`` convenience function.
The main function of this package is ``register`` which reads in the register of a plaintext accounting file. This package also registers S3 methods so one can use ``rio::import`` to read in a register, a ``net_worth`` convenience function, and a ``prune_coa`` convenience function.

register
~~~~~~~~
Expand Down
4 changes: 2 additions & 2 deletions inst/extdata/example.hledger
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ P 2016/01/01 00:00:00 SP 250 USD
Assets:JT-Brokerage 4 SP @ 250 USD
Equity:Transfer -1000 USD

2016/01/01 * Supermarket | Grocery store ;; Link: ^grocery
2016/01/01 * Supermarket | Grocery store ; Link:grocery
Expenses:Food:Grocery 500.54 USD
Liabilities:JT-Credit-Card -500.54 USD

Expand All @@ -81,7 +81,7 @@ P 2016/01/01 00:00:00 SP 250 USD



2016/01/10 * Restaurant | Restaurant ;; Tag: #restaurant
2016/01/10 * Restaurant | Restaurant ; restaurant:
Expenses:Food:Restaurant 20.07 USD
Liabilities:JT-Credit-Card -20.07 USD

Expand Down
29 changes: 15 additions & 14 deletions tests/testthat/test-ledger.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2018 Trevor L Davis <trevor.l.davis@gmail.com>
# Copyright 2021 Trevor L Davis <trevor.l.davis@gmail.com>

lfile <- system.file("extdata", "example.ledger", package = "ledger")
hfile <- system.file("extdata", "example.hledger", package = "ledger")
Expand Down Expand Up @@ -45,13 +45,13 @@ for (ii in seq_len(nrow(df_file))) {
file <- df_file$file[ii]
empty_file <- df_file$efile[ii]
context(paste(file, toolchain, "works as expected"))
register <- function(...) ledger::register(..., toolchain = toolchain)
net_worth <- function(...) ledger::net_worth(..., toolchain = toolchain)
register_ <- function(...) ledger::register(..., toolchain = toolchain)
net_worth_ <- function(...) ledger::net_worth(..., toolchain = toolchain)

test_that(paste("register works as expected on", basename(file), "using", toolchain), {
skip_toolchain(file, toolchain)

df <- register(file)
df <- register_(file)
expect_equal(sum(dplyr::filter(df, account == "Expenses:Taxes:Federal")$amount), 3 * 82.55)
expect_true(tibble::is_tibble(df))

Expand All @@ -64,8 +64,9 @@ for (ii in seq_len(nrow(df_file))) {
investment <- dplyr::filter(df, account == "Assets:JT-Brokerage")
expect_equal(investment$amount, 4)

if (system.file(package = "rio") != "") {
if (require("rio")) {
df2 <- rio::import(file, toolchain = toolchain)
df2 <- tibble::as_tibble(df2)
ftax_sum <- sum(dplyr::filter(df2, account == "Expenses:Taxes:Federal")$amount)
expect_equal(ftax_sum, 3 * 82.55)
expect_equal(df, df2)
Expand All @@ -74,11 +75,11 @@ for (ii in seq_len(nrow(df_file))) {
if (toolchain %in% c("hledger", "bean-report_hledger")) {
expect_equal(investment$historical_cost, 1000)
expect_equal(investment$market_value, 2000)
df <- register(file, flags = "tag:Tag=#restaurant")
df <- register_(file, flags = "tag:restaurant")
expect_equal(dplyr::filter(df, account == "Expenses:Food:Restaurant")$amount, 20.07)
} else {
expect_error(register(file, flags = "tag:Tag=#restaurant"))
expect_error(register(file, flags = "tag:Link=^grocery"))
expect_error(register_(file, flags = "tag:restaurant"))
expect_error(register_(file, flags = "tag:Link=grocery"))
}
if (toolchain %in% c("ledger", "bean-report_ledger")) {
expect_warning(investment$market_value)
Expand All @@ -89,25 +90,25 @@ for (ii in seq_len(nrow(df_file))) {
skip_toolchain(file, toolchain)

if (!.is_toolchain_supported(toolchain)) {
expect_error(register(file))
expect_error(register_(file))
skip(paste(toolchain, "not supported"))
}
df <- net_worth(file)
df <- net_worth_(file)
expect_true(tibble::is_tibble(df))
expect_equal(df$net_worth, 8125.39)
expect_equal(net_worth(file, include = ".*", exclude = c("^Equity", "^Income", "^Expenses"))$net_worth, 8125.39)
expect_equal(net_worth(file, c("2016-01-01", "2017-01-01", "2018-01-01"))$net_worth,
expect_equal(net_worth_(file, include = ".*", exclude = c("^Equity", "^Income", "^Expenses"))$net_worth, 8125.39)
expect_equal(net_worth_(file, c("2016-01-01", "2017-01-01", "2018-01-01"))$net_worth,
c(5000, 4361.39, 6743.39))
})

test_that(paste("register works as expected on", basename(file), "using", toolchain), {
skip_toolchain(file, toolchain)

if (! toolchain %in% c("ledger", "bean-report_ledger")) {
df <- register(empty_file)
df <- register_(empty_file)
expect_equal(nrow(df), 0)
} else {
expect_error(register(empty_file))
expect_error(register_(empty_file))
}
})
}

0 comments on commit 5db00ba

Please sign in to comment.