| Title: | Client for the 'MoSPI' Microdata Portal |
|---|---|
| Description: | Download unit-level survey microdata from the Ministry of Statistics and Programme Implementation ('MoSPI', Government of India) Microdata Portal <https://microdata.gov.in>. Provides functions to list datasets, list files within a dataset, and download individual files or entire datasets through the portal's 'NADA' API. |
| Authors: | Saket Choudhary [aut, cre] (ORCID: <https://orcid.org/0000-0001-5202-7633>) |
| Maintainer: | Saket Choudhary <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.1.9000 |
| Built: | 2026-10-18 11:27:41 UTC |
| Source: | https://github.com/saketkc/mospiR |
Download all files for a dataset
download_dataset(dataset_id, folder_path, api_key, overwrite = FALSE)download_dataset(dataset_id, folder_path, api_key, overwrite = FALSE)
dataset_id |
Dataset 'idno'. |
folder_path |
Destination directory. The function makes the directory if it does not exist. |
api_key |
MoSPI API key. |
overwrite |
If 'FALSE' (default), the function does not download the files that are already in 'folder_path'. |
A character vector of the paths of the saved files, as an invisible value.
# This dataset is approximately 270 MB. paths <- download_dataset( "DDI-IND-NSO-ASI-2020-21", tempdir(), Sys.getenv("MOSPI_KEY") )# This dataset is approximately 270 MB. paths <- download_dataset( "DDI-IND-NSO-ASI-2020-21", tempdir(), Sys.getenv("MOSPI_KEY") )
Download a single file from a dataset
download_file(dataset_id, file_name, folder_path, api_key, .files_cache = NULL)download_file(dataset_id, file_name, folder_path, api_key, .files_cache = NULL)
dataset_id |
Dataset 'idno'. |
file_name |
File name from the 'name' column of [list_files()]. |
folder_path |
Destination directory. The function makes the directory if it does not exist. |
api_key |
MoSPI API key. |
.files_cache |
A file list from [list_files()]. If 'NULL' (default), the function gets the file list from the portal. Use this argument when you download many files from one dataset. This prevents more API requests. |
The path of the saved file, as an invisible value. 'NULL' if the download is not successful.
path <- download_file( "DDI-IND-NSO-ASI-2020-21", "DDI-IND-NSO-ASI-2020-21.xml", tempdir(), Sys.getenv("MOSPI_KEY") )path <- download_file( "DDI-IND-NSO-ASI-2020-21", "DDI-IND-NSO-ASI-2020-21.xml", tempdir(), Sys.getenv("MOSPI_KEY") )
Extracts a '.zip', '.rar', '.7z' or '.tar' file from the portal. The function uses the archive package first. If the archive package is not installed or cannot extract the file, the function uses the command line tool 'unar'.
extract_archive(path, dir)extract_archive(path, dir)
path |
Path of the archive file. |
dir |
Destination directory. The function makes the directory if it does not exist. |
On Linux, the archive package uses the libarchive of the system. libarchive versions before 3.7.4 (for example 3.7.2 in Ubuntu 24.04) give a "File CRC error" on some '.rar' files from the portal. To extract these files, install 'unar' ('sudo apt-get install unar' on Ubuntu, 'brew install unar' on macOS).
The function extracts into a temporary directory and then moves the files into 'dir'. Thus, if the extraction fails, 'dir' does not change, and the function does not make 'dir'. Files in 'dir' that have the same names as files in the archive are replaced.
A character vector of the paths of the extracted files, as an invisible value. 'NULL' if the extraction is not successful.
src <- file.path(tempdir(), "example.txt") writeLines("example", src) zip_file <- file.path(tempdir(), "example.zip") archive::archive_write_files(zip_file, src) extract_archive(zip_file, file.path(tempdir(), "extracted"))src <- file.path(tempdir(), "example.txt") writeLines("example", src) zip_file <- file.path(tempdir(), "example.zip") archive::archive_write_files(zip_file, src) extract_archive(zip_file, file.path(tempdir(), "extracted"))
List datasets from the MoSPI Microdata Portal
list_datasets(api_key, page = NULL, query = NULL)list_datasets(api_key, page = NULL, query = NULL)
api_key |
MoSPI API key. |
page |
Page number to get. If 'NULL' (default), the function gets all the pages. |
query |
Text to find in the dataset titles. Upper case and lower case letters are equal. The comparison occurs on your computer. |
A data frame with the columns 'id', 'idno', 'title', and other columns. 'NULL' if the request is not successful.
page1 <- list_datasets(Sys.getenv("MOSPI_KEY"), page = 1) head(page1[, c("idno", "title")])page1 <- list_datasets(Sys.getenv("MOSPI_KEY"), page = 1) head(page1[, c("idno", "title")])
List files available for a dataset
list_files(dataset_id, api_key)list_files(dataset_id, api_key)
dataset_id |
Dataset 'idno', for example '"DDI-IND-NSO-ASI-2020-21"'. |
api_key |
MoSPI API key. |
A data frame with the columns 'name', 'base64', 'size', and other columns. 'NULL' if the request is not successful.
files <- list_files("DDI-IND-NSO-ASI-2020-21", Sys.getenv("MOSPI_KEY"))files <- list_files("DDI-IND-NSO-ASI-2020-21", Sys.getenv("MOSPI_KEY"))