{"id":26730385,"url":"https://github.com/stefano-meschiari/dictr","last_synced_at":"2025-08-20T12:40:41.815Z","repository":{"id":75804172,"uuid":"47356503","full_name":"stefano-meschiari/dictr","owner":"stefano-meschiari","description":"A better dictionary class for R.","archived":false,"fork":false,"pushed_at":"2021-04-05T16:54:30.000Z","size":161,"stargazers_count":20,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-14T10:12:35.317Z","etag":null,"topics":["data-structures","dictionary","r"],"latest_commit_sha":null,"homepage":"","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stefano-meschiari.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-12-03T19:54:06.000Z","updated_at":"2022-08-19T15:27:26.000Z","dependencies_parsed_at":"2023-03-14T15:45:46.028Z","dependency_job_id":null,"html_url":"https://github.com/stefano-meschiari/dictr","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stefano-meschiari%2Fdictr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stefano-meschiari%2Fdictr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stefano-meschiari%2Fdictr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stefano-meschiari%2Fdictr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stefano-meschiari","download_url":"https://codeload.github.com/stefano-meschiari/dictr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248860178,"owners_count":21173342,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-structures","dictionary","r"],"created_at":"2025-03-27T23:29:33.538Z","updated_at":"2025-04-14T10:12:38.022Z","avatar_url":"https://github.com/stefano-meschiari.png","language":"R","funding_links":[],"categories":[],"sub_categories":[],"readme":"`dict` provides a new data structure specialized for representing dictionaries with string keys.\n\nInstall the package using [devtools](http://cran.r-%20project.org/package=devtools):\n\n``` r\ndevtools::install_github('stefano-meschiari/dict')\n```\n\nand import the library with `library`:\n\n``` r\nlibrary(dict)\n```\n\nA proper dictionary class\n=========================\n\nThe new class `dict` can be used to represent a heterogeneous dictionary with character keys. `dict`s have a few advantages over named lists:\n\n-   Every value is uniquely associated with a key (no holes).\n-   Keys are unique and cannot be repeated.\n-   Printing of dicts is more compact.\n-   Keys are never partially matched (a key named `test` will not match with `t` or `te`).\n-   A dict can have default values for non-existing keys.\n-   Dicts can contain NULL values.\n\nThis library also provides some useful functions for manipulating dictionary-like objects.\n\nCreating a dictionary\n---------------------\n\nThe `dict` function can be used to create a new dictionary:\n\n``` r\nd \u003c- dict(color='blue', pattern='solid', width=3)\n```\n\nYou can create a dictionary out of a list of keys and values using `make_dict`:\n\n``` r\nd \u003c- make_dict(keys=c('color', 'pattern', 'width'),\n               values=c('blue', 'solid', 3))\n```\n\nYou can convert a named list or vector to a dictionary using `as_dict`:\n\n``` r\nd \u003c- as_dict(list(color='blue', pattern='solid', width=3))\nd \u003c- as_dict(c(\"a\" = 1, \"b\" = 2))\n```\n\nPrinting looks nice:\n\n``` r\nprint(d)\n```\n\n    ##   $ color : [1] \"blue\"\n    ## $ pattern : [1] \"solid\"\n    ##   $ width : [1] 3\n\nYou can use a shorthand and leave keys implicit (this is similar to the ES6 object literal shorthand). `dict` will try to create implicit keys based on the arguments:\n\n``` r\noperating_system \u003c- 'Amiga OS'\nversion \u003c- '3.1'\ncpu \u003c- '68040'\n\nmachine \u003c- dict(operating_system, version, cpu)\n\nprint(machine)\n```\n\n    ## $ operating_system : [1] \"Amiga OS\"\n    ##          $ version : [1] \"3.1\"\n    ##              $ cpu : [1] \"68040\"\n\nAccessing entries\n-----------------\n\nYou can access values using the familiar R syntax for lists:\n\n``` r\nd \u003c- dict(color='blue', pattern='solid', width=3)\n\nd$color                   # blue\nd[['pattern']]            # solid\nd['color', 'pattern']  # a sub-dictionary with keys color and pattern\n```\n\nYou can get keys and values using the `keys` and `values` functions:\n\n``` r\nkeys(d)      # c('color', 'pattern', 'width')\nvalues(d)    # list('blue', 'solid', 3)\n```\n\nYou can get a list of \"entries\" (each element being a list with `key` and `value`). This is useful for iteration:\n\n``` r\nfor (entry in entries(d)) {\n  cat(entry$key, ' = ', entry$value)\n}\n```\n\nYou can use the `map_dict` function to map over keys and values, and the `keep_dict` and `discard_dict` to filter entries. These functions are specializations of the `map`, `keep` and `discard` family of functions in [purrr](https://github.com/hadley/purrr) to the dictionary class.\n\n``` r\n# Returns a dict with the same keys and squared values\nd \u003c- dict(a=1, b=2, c=3)\nmap_dict(d, function(k, v) v^2)\n```\n\nSetting entries\n---------------\n\nEntries can be set just like regular lists:\n\n``` r\nd \u003c- dict(first='Harrison', last='Solo')  # first=Harrison, last=Solo\nd$last \u003c- 'Ford'                          # first=Harrison, last=Ford\nd[['first']] \u003c- 'Leia'                    # first=Leia, last=Ford\nd[c('last', 'title')] \u003c- c('Organa', 'Princess') # first=Leia, last=Organa, title=Princess\n```\n\nSetting an entry to `NULL` does *not* delete the entry, but instead sets the entry to `NULL`. To delete one or more entries, use the `omit` function:\n\n``` r\nd \u003c- dict(a=1, c=3)\nd$b \u003c- NULL         # a=1, b=NULL, c=3\nd \u003c- omit(a, 'a')   # b=NULL, c=3\n```\n\nUtility functions\n-----------------\n\nA few utility functions inspired by the Underscore library:\n\n-   `invert(dict)` returns a dictionary where keys and values are swapped.\n-   `has(dict, key)` returns TRUE if `dict` contains `key`.\n-   `omit(dict, key1, key2, ...)` returns a new dictionary omitting all the specified keys.\n-   `extend(dict, dict1, ...)` copies all entries in `dict1` into `dict`, overriding any existing entries and returns a new dictionary.\n-   `defaults(dict, defaults)` fill in entries from `defaults` into `dict` for any keys missing in `dict`.\n\nThe following functions specialize functions from the `purrr` library to dictionary objects:\n\n-   `map_dict(dict, fun, ...)` calls a function on each (key, value) pair and builds a dictionary from the transformed input.\n-   `keep_dict(dict, fun)` and `discard_dict(dict, fun)` keep or discard entries based on the function or predicate.\n-   `compact_dict(dict)` removes any entries with NULL values.\n\nDefault dictionaries\n--------------------\n\nYou can create a dictionary with default values using `default_dict`. Any time a non-existing key is accessed, the default value is returned.\n\n``` r\nsalaries \u003c- default_dict(employee_a = 50000,\n                         employee_b = 100000,\n                         default = 65000)\n```\n\nYou can provide a default value for an existing dictionary by setting its `default` attribute:\n\n``` r\nattr(salaries, 'default') \u003c- 70000\n```\n\nStrict dictionaries\n-------------------\n\nYou can create a strict dictionary using `strict_dict`. Any time a non-existing key is accessed, an exception is thrown using `stop()`.\n\n``` r\n# Associating each letter with a number\nletters_to_numbers \u003c- strict_dict(a=1, b=2, c=3, d=4) # etc.\n\n# Accessing an existing key, that's OK!\nprint(letters_to_numbers$a)\n```\n\n    ## [1] 1\n\n``` r\n# Accessing a non-letter will throw!\ntryCatch(letters_to_numbers$notaletter, error=function(e) print(e))\n```\n\n    ## \u003csimpleError in `$.dict`(letters_to_numbers, notaletter): Attempted access of non-existing keynotaletter\u003e\n\nImmutable collections\n---------------------\n\nYou can turn any collection (lists, vectors, and `dicts`) into an immutable collection using the `immutable` function. Such a collection cannot be modified using the `[`, `[[` and `$` operators; otherwise, it will behave the same as the original collection. The `immutable_dict` function creates an immutable dictionary.\n\n``` r\nconst_letters \u003c- immutable(letters)\n\n# Will throw an error\nconst_letters[1] \u003c- '1' \n```\n\n    ## Error in `[\u003c-.immutable`(`*tmp*`, 1, value = \"1\"): Attempting to mutate an immutable collection.\n\n``` r\nphysical_constants \u003c- immutable_dict(\n  astronomical_unit = 1.5e11,\n  speed_of_light = 3e8,\n  gravitational_constant = 6.7e-11\n)\n\n# Will throw an error\nphysical_constants$speed_of_light = 1\n```\n\n    ## Error in `$\u003c-.immutable`(`*tmp*`, \"speed_of_light\", value = 1): Attempting to mutate an immutable collection.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstefano-meschiari%2Fdictr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstefano-meschiari%2Fdictr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstefano-meschiari%2Fdictr/lists"}