---
title: "Introduction to npi"
output: 
  rmarkdown::html_vignette:
    toc: true
vignette: >
  %\VignetteIndexEntry{Introduction to npi}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(npi)
``` 

This vignette provides an brief introduction to the npi package. 

`npi` is an R package that allows R users to access the [U.S. National Provider Identifier (NPI) Registry](https://npiregistry.cms.hhs.gov/) API by the Center for Medicare and Medicaid Services (CMS). 

The package makes it easy to obtain administrative data linked to a specific individual or organizational healthcare provider. Additionally, users can perform advanced searches based on provider name, location, type of service, credentials, and many other attributes.

## Search registry

To explore organizational providers with primary locations in New York City, we could use the `city` argument in the `npi_search()`. The nyc dataset here finds 10 organizational providers with primary locations in New York City, since 10 is the default number of records that are returned in `npi_search()`. The response is a tibble that has high-cardinality data organized into list columns.

```{r}
nyc <- npi_search(city = "New York City")
nyc
```

Other search arguments for the function include `number`, `enumeration_type`, `taxonomy_description`, `first_name`, `last_name`, `use_first_name_alias`, `organization_name`, `address_purpose`, `state`, `postal_code`, `country_code`, and `limit`. 

Additionally, more than one search argument can be used at once.

```{r}
nyc_multi <- npi_search(city = "New York City", state = "NY", enumeration_type = "org")
nyc_multi
```

Visit the function's help page via `?npi_search` after installing and loading the package for more details.

## Increasing number of records returned

The `limit` argument of `npi_search()` lets you set the maximum records to return from 1 to 1200 inclusive, defaulting to 10 records if no value is specified.

```{r}
nyc_25 <- npi_search(city = "New York City", limit = 25)
nyc_25
```

When using `npi_search()`, searches with greater than 200 records (for example 300 records) may result in multiple API calls. This is because the API itself returns up to 200 records per request, but allows previously requested records to be skipped. `npi_search()` will automatically make additional API calls up to the API's limit of 1200 records for a unique set of query parameter values, and will still return a single tibble. However, to save time, the function only makes additional requests if needed. For example, if you request 1200 records, and 199 are returned in the first request, then the function does not need to make a second request because there are no more records to return.

```{r}
nyc_300 <- npi_search(city = "New York City", limit = 300)
nyc_300
```

The NPPES API documentation does not specify additional API rate limitations. However, if you need more than 1200 NPI records for a set of search terms, you will need to download the [NPPES Data Dissemination File](https://download.cms.gov/nppes/NPI_Files.html).

## Obtaining more human-readable output

`npi_summarize()` provides a more human-readable overview of output already obtained through `npi_search()`. 

```{r}
npi_summarize(nyc)
```

Additionally, users can flatten all the list columns using `npi_flatten()`.

```{r}
npi_flatten(nyc)
```

Alternatively, individual columns can be flattened for each npi by using the `cols` argument. Only the columns specified will be flattened and returned with the npi column by default.

```{r}
npi_flatten(nyc, cols = c("basic", "taxonomies"))
```

