Git Product home page Git Product logo

bpsr's Introduction

bpsr

R-CMD-check

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:

# install.packages("devtools")
devtools::install_github("dzulfiqarfr/bpsr")

Usage

You need to register yourself on BPS API’s website first to be able to use bpsr.

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.

table <- bps_dataset(lang = "eng")
table
#> # A tibble: 10 × 9
#>    dataset_id title   subject_id subject def   notes vertical_var_group_id unit 
#>    <chr>      <chr>   <chr>      <chr>   <chr> <chr> <chr>                 <lgl>
#>  1 70         Popula… 2          Commun… <NA>  &lt;… 1                     NA   
#>  2 111        Percen… 2          Commun… <NA>  Sour… 1                     NA   
#>  3 391        Percen… 2          Commun… <NA>  Sour… 1                     NA   
#>  4 392        Active… 2          Commun… <NA>  Sour… 1                     NA   
#>  5 393        Percen… 2          Commun… <NA>  Sour… 1                     NA   
#>  6 395        Percen… 2          Commun… <NA>  Sumb… 1                     NA   
#>  7 396        Percen… 2          Commun… <NA>  &lt;… 1                     NA   
#>  8 398        Percen… 2          Commun… <NA>  &lt;… 1                     NA   
#>  9 402        Percen… 2          Commun… <NA>  Sour… 152                   NA   
#> 10 403        Househ… 2          Commun… <NA>  Sour… 1                     NA   
#> # ℹ 1 more variable: graph <int>

Functions to request datasets have bps_get_ prefix. Use bps_get_dataset() to request a dataset.

data <- bps_get_dataset(table$dataset_id[[1]], lang = "eng")
data
#> # A tibble: 558 × 5
#>    vertical_var derived_var  year period   var
#>    <chr>        <chr>       <int> <chr>  <dbl>
#>  1 INDONESIA    Male         2014 Annual  18.8
#>  2 INDONESIA    Male         2015 Annual  23.7
#>  3 INDONESIA    Male         2016 Annual  27.2
#>  4 INDONESIA    Male         2017 Annual  34.5
#>  5 INDONESIA    Male         2018 Annual  42.3
#>  6 INDONESIA    Male         2019 Annual  50.5
#>  7 INDONESIA    Male         2020 Annual  56.6
#>  8 INDONESIA    Male         2021 Annual  65.0
#>  9 INDONESIA    Female       2014 Annual  15.4
#> 10 INDONESIA    Female       2015 Annual  20.2
#> # ℹ 548 more rows
#> # ℹ Read the metadata with `bps_metadata()`

Under the hood, bpsr parses the JSON returned by the API into a tibble. More importantly, the package arranges the dataset in a tidy structure, 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().

hs <- bps_hs_code()

export <- bps_get_trade_hs_chapter(
  hs$hs_chapter[[1]],
  "export", 
  year = 2021,
  freq = "annual"
)

export
#> # A tibble: 67 × 7
#>    hs_chapter description    port                partner  year net_weight export
#>    <chr>      <chr>          <chr>               <chr>   <int>      <dbl>  <dbl>
#>  1 01         Binatang hidup ATAPUPU             EAST T…  2021     15036. 1.98e5
#>  2 01         Binatang hidup KUALA NAMU INTERNA… JAPAN    2021         5  1.03e2
#>  3 01         Binatang hidup KUALA NAMU INTERNA… SINGAP…  2021     14950  1.77e4
#>  4 01         Binatang hidup KUALA TANJUNG       MALAYS…  2021     23601  6.51e4
#>  5 01         Binatang hidup NGURAH RAI (U)      EAST T…  2021      1932. 1.13e4
#>  6 01         Binatang hidup NGURAH RAI (U)      HONG K…  2021       240  2.05e4
#>  7 01         Binatang hidup SEKUPANG            SINGAP…  2021  25167463. 5.59e7
#>  8 01         Binatang hidup SOEKARNO-HATTA (U)  AFGHAN…  2021       128  5.50e4
#>  9 01         Binatang hidup SOEKARNO-HATTA (U)  AUSTRIA  2021        60  2.07e2
#> 10 01         Binatang hidup SOEKARNO-HATTA (U)  BAHRAIN  2021       127  8.40e3
#> # ℹ 57 more rows
#> # ℹ Read the metadata with `bps_metadata()`

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.

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")).

bpsr's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.