Learn R Programming

r2r

r2r provides a flexible implementation of hash tables in R, allowing for:

  • arbitrary R objects as keys and values,
  • arbitrary key comparison and hash functions,
  • customizable behaviour (throw or return a default value) on missing key exceptions.

Installation

You can install the development version of r2r from GitHub with:

# install.packages("devtools")
devtools::install_github("vgherard/r2r")

Usage

library(r2r)
m <- hashmap()

# Insert and query a single key-value pair
m[[ "user" ]] <- "vgherard"
m[[ "user" ]]
#> [1] "vgherard"

# Insert and query multiple key-value pairs
m[ c(1, 2, 3) ] <- c("one", "two", "three")
m[ c(1, 3) ]
#> [[1]]
#> [1] "one"
#> 
#> [[2]]
#> [1] "three"

# Keys and values can be arbitrary R objects
m[[ lm(mpg ~ wt, mtcars) ]] <- c(TRUE, FALSE, TRUE)
m[[ lm(mpg ~ wt, mtcars) ]]
#> [1]  TRUE FALSE  TRUE

Getting help

For further details, including an introductory vignette illustrating the features of r2r hash maps, you can consult the r2r website. If you encounter a bug, want to suggest a feature or need further help, you can open a GitHub issue.

Comparison with hash

CRAN package {hash} also offers an implementation of hash tables based on R environments. The two tables below offer a comparison between {r2r} and {hash} (for more details, see the benchmarks Vignette)

Featurer2rhash
Basic data structureR environmentR environment
Arbitrary type keysX
Arbitrary type valuesXX
Arbitrary hash functionX
Arbitrary key comparison functionX
Throw or return default on missing keysX
Hash table inversionX

Features supported by {r2r} and {hash}.

TaskComparison
Key insertion{r2r} ~ {hash}
Key query{r2r} < {hash}
Key deletion{r2r} << {hash}

Performances of {r2r} and {hash} for basic hash table operations.

Copy Link

Version

Install

install.packages('r2r')

Monthly Downloads

1,071

Version

0.1.1

License

GPL (>= 3)

Issues

Pull Requests

Stars

Forks

Maintainer

Valerio Gherardi

Last Published

July 6th, 2021

Functions in r2r (0.1.1)

length.r2r_hashtable

Size of hash tables
query

Query keys from an hash table.
hashtable_methods

Methods for S3 classes hashmap and hashset
r2r-package

r2r: R-Object to R-Object Hash Maps
on_missing_key

On missing key behaviour
values

List all values from an hash map
subsetting_hashtables

Subsetting hashsets and hashmaps
compare_fn

Get key comparison function of an hash table
hash_fn

Get hash function of an hash table
keys

List all keys from an hash table
has_key

Key existence in hash tables
insert

Insert keys or key/value pairs into an hash table.
hashtable

Hash maps and sets
default_hash_fn

String hashes for arbitrary R objects
default

Default hashmap values
delete

Delete keys or key/value pairs from an hash table.