Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/shrektan/ymd

An R package that handles common dates operations using Rust
https://github.com/shrektan/ymd

r rextendr rust ymd

Last synced: 24 days ago
JSON representation

An R package that handles common dates operations using Rust

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# ymd

[![R-CMD-check](https://github.com/shrektan/ymd/workflows/R-CMD-check/badge.svg)](https://github.com/shrektan/ymd/actions)
[![CRAN status](https://www.r-pkg.org/badges/version/ymd)](https://CRAN.R-project.org/package=ymd)
[![Downloads from the RStudio CRAN mirror](https://cranlogs.r-pkg.org/badges/ymd)](https://cran.r-project.org/package=ymd)
[![Rust Code Coverage](https://coveralls.io/repos/github/shrektan/ymd/badge.svg?branch=main)](https://coveralls.io/github/shrektan/ymd?branch=main)

Convert 'YMD' format number or string to Date efficiently, e.g., `211225` to `as.Date("2021-12-25")`, using Rust's standard library.
It also provides helper functions to handle Date, e.g., quick finding the beginning or end of the given period, adding months to Date, etc.

It's similar to the `lubridate` package but is much lighter and focuses only on Date objects.

## Installation

### Binary version (no Rust toolchain required)

CRAN provides the binary package. So, if you are on Windows or macOS, the package can be installed via:

```r
install.packages("ymd")
```

If you are on Linux, you can try to use the [RSPM (RStudio Package Manager) repo](https://packagemanager.rstudio.com)
provided by RStudio PBC, via (remember to choose the correct binary repo URL for your platform):

```r
install.packages("ymd", repos = "{RSPM-Repo-URL}")
```

### Source version (Rust toolchain required)

If you want to build the dev version from source, you'll need the Rust toolchain, which
can be installed following [the instructions from the Rust book](https://doc.rust-lang.org/book/ch01-01-installation.html).

After that, you can build the package via:

```r
remotes::install_github("ymd")
```

## Use Cases and Benchmarks

```{r setup}
print_bmk <- function(x) {
x[[1]] <- format(x[[1]])
x[[5]] <- format(x[[5]])
rnd <- \(v) if (is.numeric(v)) round(v, 1) else v
x[, 1:8] |>
lapply(rnd) |>
as.data.frame() |>
knitr::kable() |>
print()
}
run_bmk <- function(..., time_unit = "us") {
bench::mark(..., time_unit = time_unit) |> print_bmk()
}
```

### ymd

```{r ymd, results='asis'}
x <- c("210101", "21/02/03", "89-1-03", "1989.03.05", "01 02 03")
x <- rep(x, 100)
run_bmk(
ymd::ymd(x),
lubridate::ymd(x)
)

x <- c(210101, 210224, 211231, 19890103)
x <- rep(x, 100)
run_bmk(
ymd::ymd(x),
lubridate::ymd(x)
)

x <- c("2021-01-01", "2022-12-31", "1995-03-22")
x <- rep(x, 100)
run_bmk(
ymd::ymd(x),
lubridate::ymd(x),
as.Date(x)
)

x <- ymd::ymd(210515) + 1:100
run_bmk(
ymd::eop$tm(x),
lubridate::ceiling_date(x, "month") - 1
)
```

### edate

```{r edate, results='asis'}
`%m+%` <- lubridate::`%m+%`
x <- ymd::ymd(c(200115, 200131, 200229, 200331, 200401))
x <- rep(x, 100)
run_bmk(
ymd::edate(x, 2),
x %m+% months(2)
)
run_bmk(
ymd::edate(x, -12),
x %m+% months(-12)
)
```

### Extract Date Part

```{r date-part, results='asis'}
# tweak from https://github.com/Rdatatable/data.table/pull/5300
set.seed(373L)
x <- as.Date(data.table::as.IDate(sample(seq(-25000, 45000), 1e6, TRUE)))

run_bmk(
data.table::year(x),
lubridate::year(x),
funchir::quick_year(x),
ymd::year(x)
)
run_bmk(
data.table::month(x),
lubridate::month(x),
ymd::month(x)
)
run_bmk(
data.table::quarter(x),
lubridate::quarter(x),
ymd::quarter(x)
)
run_bmk(
data.table::yday(x),
lubridate::yday(x),
funchir::quick_yday(x),
ymd::yday(x)
)
run_bmk(
data.table::mday(x),
lubridate::mday(x),
funchir::quick_mday(x),
ymd::mday(x)
)
run_bmk(
data.table::wday(x),
lubridate::wday(x),
ymd::wday(x)
)
run_bmk(
data.table::isoweek(x),
lubridate::isoweek(x),
ymd::isoweek(x)
)
```

## Session Info

```{r session-info}
xfun::session_info()
```