Skip to contents

Downloads files for a specified NHS TRUD item (requires a subscription). By default this is the latest release.

Usage

download_item(
  item,
  directory = ".",
  download_file = "archive",
  TRUD_API_KEY = NULL,
  release = NULL
)

Arguments

item

An integer, the item to be downloaded.

directory

Path to the directory to which this item will be downloaded to. This is set to the current working directory by default.

download_file

The item file to be downloaded. Valid values:

  • "archive" (the release item)

  • "checksum"

  • "signature"

  • "publickKey"

TRUD_API_KEY

A string. The name of an environmental variable containing your TRUD API key. If NULL (default) this is assumed to be called TRUD_API_KEY.

release

The name of a specific release ID to be downloaded (this can be ascertained using get_item_metadata()). If NULL (default), then the latest item release will be downloaded.

Value

The file path to the downloaded file, returned invisibly.

Examples

# \dontrun{
 # Download Community Services Data Set pre-deadline extract XML Schema
 x <- download_item(394, directory = tempdir())
#> ⠙ Downloading archive file for TRUD item 394...
#>  Successfully downloaded `CSDS_Provpredextract_1.6.6_20221115000001.zip` to /t
#> 

 # List downloaded files
 unzip(x, list = TRUE)
#>                                                   Name Length
#> 1           CSDS_ProvPreExtract__V1_6_6_SAMPLE_XML.xml   7887
#> 2              CSDS_ProvPreExtract__V1_6_6_FinalV1.XSD  75650
#> 3 CSDS ProvPreDExtract v1.6.6 Production Log v0.1.xlsx  32216
#>                  Date
#> 1 2022-11-15 12:48:00
#> 2 2022-10-31 11:36:00
#> 3 2022-11-15 11:57:00

 # Download a previous release
 release <- get_item_metadata(394)$releases[[2]]$id

 y <- download_item(394, directory = tempdir(), release = release)
#> ⠙ Downloading archive file for TRUD item 394...
#>  Successfully downloaded `csds_provpredextract_1.5.21_20200805000001.zip` to /
#> 

 unzip(y, list = TRUE)
#>                                            Name Length                Date
#> 1      CSDS_ProvPreExtract__V1.5.21_FinalV1.XSD  58918 2020-07-14 17:41:00
#> 2   CSDS_ProvPreExtract__V1.5.21_SAMPLE_XML.xml   7799 2020-07-23 17:03:00
#> 3 CSDS ProvPreDExtract Production Log v0.1.xlsx  32645 2020-07-15 16:20:00
# }

# An informative error is raised if your API key is invalid or missing
try(download_item(394, TRUD_API_KEY = "INVALID_API_KEY"))
#> Error in download_item(394, TRUD_API_KEY = "INVALID_API_KEY") : 
#>    Can't find NHS TRUD API key
#>  Set your NHS TRUD API key as an environment variable using
#>   `Sys.setenv(TRUD_API_KEY='<<your-key>>')`, or preferably use a `.Renviron`
#>   file
#>  To get an API key, first sign up for a NHS TRUD account at
#>   <https://isd.digital.nhs.uk/trud/users/guest/filters/0/account/form>
#>  To find Your API key, log in and visit your account profile page
#>   (<https://isd.digital.nhs.uk/trud/users/authenticated/filters/0/account/manage>).