-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.Rmd
More file actions
272 lines (187 loc) · 6.14 KB
/
README.Rmd
File metadata and controls
272 lines (187 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# slash <a><img src='man/figures/slash.png' align="right" height="200" /></a>
<!-- badges: start -->
[](https://github.com/feddelegrand7/slash/actions/workflows/R-CMD-check.yaml)
[](https://github.com/feddelegrand7/rlowdb)
[](https://app.codecov.io/gh/feddelegrand7/slash?branch=main)
[](https://CRAN.R-project.org/package=slash)
<!-- badges: end -->
The goal of slash is to provide a hierarchical key-value store where elements can be accessed and modified using simple path-like strings, such as `"cars/1/model"` or `"garage/vw/golf/color"`.
It supports:
- Named and unnamed lists
- Nested access with `/` paths
- Optional strict mode
- List path enumeration
- Full get/set/delete API
## Installation
You can install `slash` from `CRAN` with:
``` r
install.packages("slash")
```
You can install the development version of `slash` like so:
``` r
devtools::install_github("feddelegrand7/slash")
```
## Getting elements from a `list`
Consider the following `list` object:
```{r}
cars_list <- list(
cars = list(
list(manufacturer = "VW", model = "Golf V", year = 2005),
list(manufacturer = "Toyota", model = "Corolla", year = 2010),
list(manufacturer = "Tesla", model = "Model S", year = 2022)
)
)
```
If one wants to access the `manufacturer` element, one can do:
```{r}
cars_list$cars[[1]]$manufacturer
```
Using `slash`, you can access the same element using a `file-path` syntax:
```{r}
library(slash)
sl <- slash$new(data = cars_list)
sl$get(path = "cars/1/manufacturer")
```
`slash` can operate on unnamed elements like above and/or on named elements like the following:
```{r}
garage <- list(
vw = list(
golf = list(year = 2005, color = "black"),
passat = list(year = 2011)
),
toyota = list(
corolla = list(year = 2010)
)
)
```
Let's say we want to access the color of the __VW Golf__. While in standard `R` one can do:
```{r}
garage$vw$golf$color
```
Using `slash`, we can operate as the following:
```{r}
sl <- slash$new(data = garage)
sl$get("vw/golf/color")
```
If now, for example, we would want to access all the properties of the `Golf` car, we would do:
```{r}
sl <- slash$new(data = garage)
sl$get("vw/golf")
```
It is possible to return the whole list if needed using the `get_all` method:
```{r}
sl$get_all()
```
You'll also get the whole `list` element when `NULL` (the default) is provided to the `get` method:
```{r}
sl$get(NULL)
```
If you try to access an element that does not exist, you'll get a `NULL` as the returned value:
```{r}
sl$get("vw/polo")
```
You can change this behavior and get an `error` back when an element is not found using the `strict` parameter. You can set the parameter at the initialization of the instance:
```{r}
sl <- slash$new(data = garage, strict = TRUE)
```
or afterward, using the `set_strict` method:
```{r}
sl$set_strict(strict = TRUE)
```
This way, we get an `error` back when an element is not found:
```{r, error=TRUE}
sl$get("vw/polo")
```
## Setting elements in a `list`
You can change the value of an element or add a new element within a list using the `set` method, suppose I want to add a new car to my previous list:
```{r}
sl$set(path = "vw/polo/year", value = 2013)
sl$set(path = "vw/polo/color", value = "Steelblue")
sl$get("vw")
```
Now, if you want to modify the year from `2013` to `2023` for example, you can do:
```{r}
sl$set(path = "vw/polo/year", value = 2023)
sl$get("vw")
```
You can even build your list element from scrath:
```{r}
sl <- slash$new()
sl$get()
```
```{r}
sl$set("vw/golf/year", value = 2005)
sl$set("vw/golf/color", value = "black")
sl$set("vw/passat/year", value = 2011)
sl$set("vw/polo/year", value = "Steelblue")
sl$set("vw/polo/color", value = 2023)
sl$get("vw")
```
## Deleting an element from the `list`
You can delete an element using the `delete` method, suppose we don't need the `polo` car element anymore, we could do:
```{r}
sl$delete("vw/polo")
sl$get("vw")
```
You can delete at any level on the list, for example if we want to delete the `color` field of the `golf` element, we could do:
```{r}
sl$delete("vw/golf/color")
sl$get("vw")
```
## Listing the available paths
If you want to list the available paths of your `list` object, you can call the `list_paths()` method:
```{r}
sl$list_paths()
```
## Check if an element exists:
Use the `exists` method to check if a particular path exists:
```{r}
sl$exists("vw")
sl$exists("vw/golf")
sl$exists("vw/golf/color")
sl$exists("porshe/911")
```
## Printing a `slash` object
A `slash` object has a particular `print` method attached to it, it prints a nice view of the available paths among other information (`strict mode`):
```{r}
sl
```
## Printing the `list` object
Each `slash` object is build on top of a `list` object, if you want to print the `list` it-self, use the `print_list` method:
```{r}
sl$print_list()
```
## Printing a `Tree` representation
__only available in the development version (not yet on CRAN)__
You can print a `Tree` representation of your `slash` object and its underlying list using the `print_tree` method:
```{r}
sl$print_tree()
```
```{r}
# Adding the 208 peugeot model
# Make sure to quote the `208`, otherwise slash will
# understand it as indices (Not name)
sl$set("peugeot/`208`/year", 2013)
sl$print_tree()
```
```{r}
sl$print_tree("peugeot")
```
```{r}
sl$set("peugeot/`208`/energy/class", "Diesel")
sl$print_tree("peugeot/`208`/energy")
```
## Code of Conduct
Please note that the slash project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/1/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.