Skip to contents

Retrieves real-time COVID-19 totals for one or more U.S. states from the 'disease.sh' API.

Usage

get_covid_stats_for_state(states, yesterday = FALSE, allowNull = FALSE)

Arguments

states

A character string with the name of a U.S. state or a comma-separated list of state names. Names must be spelled correctly.

yesterday

Logical. If TRUE, returns data from the previous day. Default is FALSE.

allowNull

Logical. If TRUE, missing values are returned as NA instead of 0. Default is FALSE.

Value

A data frame containing the following columns:

  • state: State name.

  • updated: Last updated timestamp (converted to human-readable datetime in UTC).

  • cases: Total confirmed cases.

  • todayCases: New confirmed cases today.

  • deaths: Total deaths.

  • todayDeaths: New deaths today.

  • population: State population estimate.

Returns NULL if the API is unavailable, the state(s) are not found, or an error occurs.

Details

This function sends a GET request to the 'disease.sh' API for COVID-19 statistics in one or more U.S. states. If multiple states are passed, they must be comma-separated and correctly spelled. The 'updated' field is returned in milliseconds and is converted to a POSIXct datetime. Requires an active internet connection.

Note

Requires an internet connection. Function fails gracefully if API is unavailable.

References

API Docs: https://disease.sh/docs/#/COVID-19

Examples

if (FALSE) { # \dontrun{
# Retrieve COVID-19 data for California
ca <- get_covid_stats_for_state("California")
if (!is.null(ca)) {
  print(ca)
}

# Retrieve yesterday's data for New York and Texas
ny_tx <- get_covid_stats_for_state("New York,Texas", yesterday = TRUE)
} # }