-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.Rmd
More file actions
213 lines (152 loc) · 5.59 KB
/
Copy pathREADME.Rmd
File metadata and controls
213 lines (152 loc) · 5.59 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
---
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%"
)
```
# datacaged <img src="man/figures/datacaged.svg" align="right" height="138" alt="" />
*[Leia em Português](README.pt-BR.md)*
[](https://github.com/gecomt/datacaged/actions)
[](https://app.codecov.io/gh/gecomt/datacaged)
[](LICENSE.md)
[](https://doi.org/10.5281/zenodo.20058818)
> CAGED microdata straight into your 'DuckDB', with a complete pipeline ready for analysis.
**datacaged** is an R package that automates the entire workflow with CAGED (Cadastro Geral de Empregados e Desempregados — Brazil's General Register of Employed and Unemployed Workers) microdata. Data is downloaded from a [HuggingFace repository](https://huggingface.co/datasets/alexsandroprado/caged) via HTTPS, parsed with streaming decompression, and stored in a local 'DuckDB' database for fast, scalable queries with `dplyr` or SQL.
---
## ✨ Key features
* 📥 Automated download from HuggingFace (HTTPS — no FTP required)
* ⚡ Parallel downloads (`workers = 3` by default)
* 📦 Stream decompression of `.7z` files — no temporary disk extraction
* 🗄️ Efficient storage in a local **'DuckDB'** database
* 🔄 Full pipeline in a single function call
* 📊 Integration with `dplyr` for data analysis
* 🖥️ **Compatible with Windows, macOS and Linux**
* 🧩 Support for multiple datasets:
* Novo CAGED (2020+)
* Legacy CAGED (1992–2019)
* CAGED Adjustments (historical corrections)
---
## 🚀 Installation
```r
# install.packages("remotes")
remotes::install_github("gecomt/datacaged")
```
---
## 🖥️ Platform compatibility
| Feature | Windows | macOS | Linux |
|---|---|---|---|
| Download (HTTPS) | ✅ | ✅ | ✅ |
| Parallel downloads | ✅ | ✅ | ✅ |
| Novo CAGED (2020+, LZMA) | ✅ | ✅ | ✅ |
| Legacy CAGED (pre-2020, PPMd) | ⚠️ | ⚠️ | ⚠️ |
| DuckDB | ✅ | ✅ | ✅ |
**Legacy CAGED (pre-2020)** uses PPMd compression which requires 7-Zip. **Novo CAGED (2020+) works on all platforms without any extra software.**
### Installing 7-Zip (only needed for pre-2020 data)
**Windows** — install the [7-Zip installer](https://www.7-zip.org/download.html) (standard installation to `C:\Program Files\7-Zip` is automatically detected).
**macOS**
```sh
brew install 7-zip
```
**Linux (Debian/Ubuntu)**
```sh
sudo apt install 7zip
```
**Linux (Fedora/RHEL)**
```sh
sudo dnf install 7zip
```
---
## ⚡ Quick start
```r
library(datacaged)
# Full pipeline — downloads, parses and writes to DuckDB
caged_load(
years = 2022:2023,
months = seq_len(12L),
db_path = file.path(tempdir(), "caged.duckdb")
)
# Connect and query
con <- caged_connect(file.path(tempdir(), "caged.duckdb"))
library(dplyr)
tbl(con, "caged_mov") |>
filter(uf == 35, competenciamov >= 202201L) |>
group_by(competenciamov) |>
summarise(saldo = sum(saldomovimentacao, na.rm = TRUE)) |>
collect()
DBI::dbDisconnect(con, shutdown = TRUE)
```
---
## ⚙️ Performance
```r
# Parallel downloads (default: 3 workers)
caged_download(years = 2023, months = 1:12, workers = 3)
# Sequential (for slow connections or debugging)
caged_download(years = 2023, months = 1:12, workers = 1)
# Set globally
options(datacaged.workers = 4)
```
Parallel downloads process MOV, FOR and EXC files simultaneously per month — approximately **3× faster** than sequential for Novo CAGED.
---
## 🧠 Pipeline
```
HuggingFace (HTTPS)
↓
caged_download() ← parallel, with local cache
↓
caged_parse() ← stream decompression via archive_read()
↓
caged_to_duckdb() ← bulk insert, deduplication by period
↓
caged_connect() ← dplyr / SQL / DBI
```
---
## 📚 Main functions
| Function | Description |
|---|---|
| `caged_load()` | Full pipeline (download → parse → DuckDB) |
| `caged_adjustments_load()` | Adjustments pipeline |
| `caged_download()` | Download `.7z` files with cache |
| `caged_download_layouts()` | Download official MTE layouts |
| `caged_parse()` | Parse a single `.7z` file |
| `caged_parse_batch()` | Batch parse |
| `caged_to_duckdb()` | Write to DuckDB |
| `caged_connect()` | Open DBI connection |
| `caged_info()` | Database statistics |
| `caged_status()` | Check HuggingFace connectivity |
| `caged_hf_files()` | List available competencies |
| `caged_update()` | Download only new competencies |
| `caged_to_parquet()` | Export tables to Parquet files |
---
## 🗄️ Database tables
| Table | Content | Period |
|---|---|---|
| `caged_mov` | Movements | 2020+ |
| `caged_for` | Late declarations | 2020+ |
| `caged_exc` | Exclusions | 2020+ |
| `caged_antigo` | Historical data | 1992–2019 |
| `caged_ajustes` | Retroactive adjustments | 1992–2019 |
---
## 🧾 Requirements
* R ≥ 4.2.0
* 7-Zip *(optional — only required for CAGED pre-2020 with PPMd compression)*
---
## 📊 Use cases
* Labour market analysis
* Economic indicators
* Academic research
* Formal employment monitoring
* Econometric modelling
---
## 📄 Licence
MIT © Alexsandro Prado
---
## 📬 Contact
**Author:** Alexsandro Prado
**Email:** [alexsandro.prado@ufersa.edu.br](mailto:alexsandro.prado@ufersa.edu.br)
**GitHub:** <https://github.com/gecomt/datacaged>