-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.Rmd
More file actions
183 lines (134 loc) · 3.45 KB
/
README.Rmd
File metadata and controls
183 lines (134 loc) · 3.45 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
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# euformat
The euformat package provides a lightweight way to:
- store numbers as real numeric values,
- but print them using European number formatting:
- comma for decimals
- dot for thousands
- optional euro currency symbol (" €")
Formatted numbers behave exactly like numeric* in calculations; only printing changes.
The package works consistently in:
- R console
- base data.frames
- tibbles
- DT tables
and includes a global toggle to enable/disable printing.
## Installation
You can install the development version of euformat from [GitHub](https://github.com/) with:
``` r
# install.packages("pak")
pak::pak("icg-cat/euformat")
```
```{r message=FALSE, warning=FALSE}
library(ggplot2)
library(tibble)
library(DT)
library(euformat)
```
## Basic usage
### Create EU-formatted numbers
```{r}
x <- eu_num(c(1234.567, 10.2))
x
```
Internally still numeric:
```{r}
is.numeric(x)
x + 1
```
## Currency formatting
```{r}
eu_currency(c(1500, 20000))
```
## Vectorized decimal digits
```{r}
eu_num(c(1.2345, 2.34567), digits = c(1, 3))
```
## Using euformat inside tibbles
```{r}
tibble(value = eu_num(c(1234.5, 98765.432)))
```
## Using euformat in base data frames
```{r}
data.frame(amount = eu_currency(c(1234.5, 98765.4)))
```
## Formatting in DT tables
```{r eval=FALSE, include=TRUE}
df <- cbind(
item = c("A", "B", "C", "D"),
value = c(2, 10, 1000.5, 12.34),
cost = (c(2, 10, 1000.5, 12.34) * 1.3)
)
datatable(df) |>
dt_format_eu(columns = c("value", "cost"), digits = 2, currency = TRUE)
```
Numeric format is maintained, so further developments can be added to the table:
```{r eval=FALSE, include=TRUE}
datatable(
df,
container = htmltools::withTags(
table(
tableHeader(df),
tableFooter(c("Total", "", "")) # footer placeholders
)
),
options = list(
footerCallback = JS(
"function(tfoot, data, start, end, display) {",
" var api = this.api();",
" // numeric totals for value and cost",
" var total_value = api.column(1, { page: 'current' }).data().reduce(function(a,b){",
" return a + parseFloat(b) || 0;",
" }, 0);",
" var total_cost = api.column(2, { page: 'current' }).data().reduce(function(a,b){",
" return a + parseFloat(b) || 0;",
" }, 0);",
" // write totals into footer",
" $(api.column(0).footer()).html('Total');",
" $(api.column(1).footer()).html(total_value);",
" $(api.column(2).footer()).html(total_cost);",
"}"
)
)
) |> dt_format_eu(columns = c("value", "cost"), digits = 2, currency = TRUE)
```
## Formatting in ggplot2
```{r ggplot-example, message=FALSE, warning=FALSE}
# Example data
df <- data.frame(
category = LETTERS[1:5],
revenue = c(1234.5, 56789, 101112.3, 2000, 300.75)
)
# Plain EU number formatting
p1 <- ggplot(df, aes(category, revenue)) +
geom_col(fill = "#4477AA") +
eu_scale_y() +
ggtitle("Revenue with EU-style number formatting")
# EU currency formatting
p2 <- ggplot(df, aes(category, revenue)) +
geom_col(fill = "#66CCAA") +
eu_scale_y(currency = TRUE) +
ggtitle("Revenue with EU-style currency formatting")
p1
p2
```
## Turning formatting on and off
```{r}
eu_format_off()
x
```
```{r}
eu_format_on()
x
```
This affects printing only; underlying data remain numeric.