/
json.Rmd
100 lines (82 loc) · 2.29 KB
/
json.Rmd
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
---
title: "Export to JSON"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Export to JSON}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
```{r setup, message = FALSE, warning = FALSE}
library(tfrmt)
library(dplyr)
```
Aside from R code, `tfrmt` objects can be represented as machine-readable metadata in `JSON` format. This enables users to save formatting metadata to a database, or alternatively import templates stored as `JSON` files. There are several utilities available for seamless translation between `tfrmt` and `JSON`.
## tfrmt to JSON
A `tfrmt` object can be converted to `JSON` using the `tfrmt_to_json` function. Suppose we have a basic `tfrmt`:
```{r}
template_tfrmt <- tfrmt(
group = grp,
label = label,
column = column,
param = param,
value=value,
body_plan = body_plan(
frmt_structure(group_val = ".default",
label_val = ".default",
frmt("xx.x")))
)
template_tfrmt %>% print_mock_gt()
```
We can pass this through `tfrmt_to_json` to convert to `JSON` metadata.
```{r}
template_tfrmt %>%
tfrmt_to_json()
```
This `JSON` can optionally be saved to a file by providing a file path as the second argument to the function:
```{r eval = FALSE}
tfrmt(
group = grp,
label = label,
column = column,
param = param,
value=value,
body_plan = body_plan(
frmt_structure(group_val = ".default",
label_val = ".default",
frmt("xx.x")))
) %>%
tfrmt_to_json(path = "template.JSON")
```
## JSON to tfrmt
If a `JSON` file already exists, users can import this into R.
```{r echo=FALSE}
template_json <- tfrmt(
group = grp,
label = label,
column = column,
param = param,
value=value,
title= 'mytitle',
body_plan = body_plan(
frmt_structure(group_val = ".default",
label_val = ".default",
frmt("xx.x")))
)
```
```{r eval = FALSE}
template_json <- json_to_tfrmt(path = "template.JSON")
```
Once available in the R session, users can optionally build on this template as needed.
```{r}
template_json %>%
layer_tfrmt(
tfrmt(title = "Custom title")
) %>%
print_mock_gt()
```