-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.qmd
157 lines (106 loc) · 3.08 KB
/
README.qmd
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
---
format: gfm
---
# tomledit
<!-- badges: start -->
[](https://github.com/extendr/tomledit/actions/workflows/R-CMD-check.yaml)
[](https://extendr.github.io/extendr/extendr_api/)
<!-- badges: end -->
Create or edit TOML documents from R using `tomledit`.
`tomledit` is written in Rust using [extendr](https://extendr.github.io/) and the [`toml_edit`](https://docs.rs/toml_edit/) crate.
## Installation
Install the package from CRAN using
```{r eval = FALSE}
install.packages("tomledit")
```
or, install the development version using
```{r eval = FALSE}
remotes::install_github("extendr/tomledit")
```
## Usage
TOML can be created using either the `as_toml()` or `toml()` functions.
Use `as_toml()` to convert a list to TOML:
```{r}
library(tomledit)
as_toml(
list(
person = list(age = 30L, name = "Wilma")
)
)
```
Create TOML directly by passing key values to `toml()`:
```{r}
x <- toml(person = list(age = 30L, name = "Wilma"))
x
```
Or, parse a string as TOML while preserving comments:
```{r}
raw_toml <- '# Top-level table begins.
name = "Fido"
breed = "pug"
# Top-level table ends.
[owner]
name = "Regina Dogman"
member_since = 1999-08-04'
x <- parse_toml(raw_toml)
x
```
Write a `Toml` object to a file using `write_toml()`.
```{r}
tmp <- tempfile(fileext = ".toml")
write_toml(x, tmp)
```
Read a TOML file using `read_toml()`.
```{r}
read_toml(tmp)
```
Items can be inserted into a `Toml` document using `insert_items()`
```{r}
y <- x |>
insert_items(
date = Sys.Date(),
date_parts = list(year = 2015L, month = "February", day = 7L)
)
y
```
Or items can be removed as well using `remove_items()`
```{r}
remove_items(y, c("date", "date_parts"))
```
Individual items can be fetched recursively from the `Toml` document.
```{r}
get_item(y, c("date_parts", "month"))
```
Or the entire `Toml` document can be converted to a list. Note, though, that it is not always possible to perform a perfect round trip of R objects and TOML.
```{r}
from_toml(y)
```
## Array of Tables
By default `tomledit` converts `data.frame` objects to an [array of tables](https://toml.io/en/v1.0.0#array-of-tables).
```{r}
toml(iris = iris[1:3,])
```
This is the default behavior as it is most consistent with TOML files that are encountered in the wild. To create a single table from a `data.frame`, set the argument `df_as_array = FALSE`.
```{r}
toml(
iris = iris[1:3,],
df_as_array = FALSE
)
```
## Missing Values
One reason why array of tables are recommended for `data.frame`s is because there is no concept of a missing or null value in TOML.
Take the following example:
```{r}
x <- data.frame(
x = c(1L, NA, 2L),
y = letters[1:3]
)
```
Notice that when this `data.frame` is serialized to TOML the missing `x` value is omitted:
```{r}
toml(table = x)
```
Whereas when serializing to a single table the `x` array has 2 elements whereas the `y` element has 3 elements.
```{r}
toml(table = x, df_as_array = FALSE)
```