MyNixOS website logo
Description

R6 Based Key-Value Dictionary Implementation.

A key-value dictionary data structure based on R6 class which is designed to be similar usages with other languages dictionary (e.g. 'Python') with reference semantics and extendabilities by R6.

Dict

Overview

Dict is a R package which implements a key-value dictionary data structure based on R6 class. It is designed to be similar usages with other languages' dictionary implementations (e.g. Python).

R's vector and list, of course can have names, so you can get and set value by a name (key) like a dictionary. Using regular data structure must be a recommended way in the most of cases. But, if you are interested in the following characteristics, this package is for you!

  • Reference semantics: Useful to keep project wide parameters or states referred from multiple models.
  • Inheritance: Easily expand features according to your needs.

Installation

install.packages("Dict")

# Or the the development version from GitHub:
# install.packages("devtools")
devtools::install_github("five-dots/Dict")

Usage

Instantiate a dictionary

To instantiate a dict, you can pass any length of key-value pairs to the initialize method.

library(Dict)
ages <- Dict$new(
  Charlie = 40L,
  Alice = 30L,
  Bob = 25L,
  .class = "integer",
  .overwrite = TRUE
)
ages
# A tibble: 3 x 2
 key      value    
 <chr>    <list>   
1 Charlie <int [1]>
2 Alice   <int [1]>
3 Bob     <int [1]>

Some notes

  • dict() can be used instead of Dict$new() as some IDEs cloud not show R6's function arguments hint.
  • .class specifies what kind of objects the dictionary can contains. Default "any" means the dict cloud have any type of value.
  • .overwrite controls the behavior when the same key is added.
  • Dict keep key-value items in tbl_df from tibble package whose key is a character column and value is a list column. You can use various existing tooling for data.frame or tibble to manipulate dict items.

Get a value

A value can be access by both Dict$get() or `[` with a character key or integer index of items rows.

ages["Bob"]
ages$get("Bob")
ages$get(3L)
[1] 25

[1] 25

[1] 25

If no key found, value of default is returned.

ages["Michael", default = 30]
[1] 30

Add a new item

Adding a item also can be done by R6 methods Dict$add() or `[<-`.

ages["John"] <- 18L # or ages$add(John = 18L)
ages["John"]
[1] 18

Can be overridden if .overwrite = TRUE (default).

ages["Bob"] <- 26L
ages$get("Bob")
[1] 26

Other methods and fields

Remove item:

ages$remove("Bob")

Check if items contains a key:

ages$has("Bob")
[1] FALSE

Sort by keys:

ages$sort()
ages
# A tibble: 3 x 2
 key      value    
 <chr>    <list>   
1 Alice   <int [1]>
2 Charlie <int [1]>
3 John    <int [1]>

Clear items:

ages$clear()
ages
# A tibble: 0 x 2
# … with 2 variables: key <chr>, value <list>

Fields:

ages$keys
ages$values
ages$items
ages$length
Metadata

Version

0.1.0

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows