forked from dzulfiqarfr/bpsr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
89 lines (61 loc) · 2.85 KB
/
README.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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
eval = httr2::secret_has_key("BPSR_KEY"),
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# bpsr
<!-- badges: start -->
[![R-CMD-check](https://github.com/dzulfiqarfr/bpsr/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/dzulfiqarfr/bpsr/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
bpsr allows users to get Statistics Indonesia's (BPS) datasets and other resources from R by wrapping up its application programming interface (API).
## Installation
You can install the development version of bpsr like so:
``` r
# install.packages("devtools")
devtools::install_github("dzulfiqarfr/bpsr")
```
## Usage
You need to register yourself on [BPS API's website](https://https://webapi.bps.go.id/) first to be able to use bpsr.
```{r}
library(bpsr)
```
All functions from bpsr have a `bps_` prefix, so we can benefit from an automatic code completion.
Most functions to get resources take a string of a resource identifier (ID) as the first argument. So we typically need to look it up first in the table of the resource of interest. Functions to get such tables have a noun-y name. For example, use `bps_dataset()` to request the dataset table.
```{r}
table <- bps_dataset(lang = "eng")
table
```
Functions to request datasets have `bps_get_` prefix. Use `bps_get_dataset()` to request a dataset.
```{r}
data <- bps_get_dataset(table$dataset_id[[1]], lang = "eng")
data
```
Under the hood, bpsr parses the JSON returned by the API into a [tibble](https://tibble.tidyverse.org/index.html). More importantly, the package arranges the dataset in a [tidy structure](https://tidyr.tidyverse.org/articles/tidy-data.html), which is well suited for analysis.
Use `bps_get_trade_hs_*()` to request trade datasets. These trade functions take the Harmonized System (HS) chapter or code as the first argument. To look up the HS chapter or code, use `bps_hs_code()`.
```{r}
hs <- bps_hs_code()
export <- bps_get_trade_hs_chapter(
hs$hs_chapter[[1]],
"export",
year = 2021,
freq = "annual"
)
export
```
Some resources are available for download. We can use the download functions to get those resources. This is especially useful to get a dataset stored in a spreadsheet, which comes in a Microsoft Excel file format with the ".xls" extension.
```{r, eval = FALSE}
spreadsheet <- bps_dataset_spreadsheet(lang = "eng")
bps_download_spreadsheet(spreadsheet$dataset_id[[1]], "spreadsheet.xls")
```
Aside from the common usage, bpsr also provides other features, including:
* requesting multiple datasets at once using `bps_get_datasets()`; and
* downloading multiple resources at once using `bps_download()`.
Learn more in the Getting Started vignette (`vignette("bpsr")`).