Reads a GBIF occurrence download and returns a data.table of the fields
required by the 'VasGBIF' workflow.
Arguments
- path
Character scalar. Path to a GBIF occurrence download: either a 'SIMPLE_CSV' or 'DWCA' ZIP archive, or an extracted tab-separated occurrence table. An archive must contain a tab-separated occurrence data file, named
occurrence.txtwhen the archive contains more than one member (as in a Darwin Core Archive).- tempdir
Character scalar or
NULL. Directory into which the ZIP archive is extracted. Ignored whenpathis not a ZIP file.NULL(default): a unique subdirectory is created inside the system temporary directory viabase::tempfile(), and it is deleted on exit unlessremove_tempfile = FALSE.A user-supplied path: if the directory does not exist it is created (recursively). If it already exists and contains files, a warning is issued because those files may be overwritten. The directory is never deleted; on exit only the extracted occurrence file is unlinked, and even that is skipped when
remove_tempfile = FALSE.
- remove_tempfile
Logical scalar. Controls whether the files this call produced are deleted when the function exits (including after an error). Defaults to
TRUE.With
tempdir = NULL, the extraction directory is created by this call and holds nothing else, so the directory is removed whole.With a user-supplied
tempdir, only the extracted occurrence file is unlinked. The directory itself and every unrelated file in it are left untouched.FALSEkeeps the extracted file inex_pathand reports its location.
Value
A data.table of class "import" containing the selected occurrence
fields with Darwin Core / GBIF column names. The gbifID column is always
character.
Details
Two kinds of input are accepted, chosen by the file extension:
A ZIP archive in 'SIMPLE_CSV' or Darwin Core Archive ('DWCA') format. The occurrence data file is extracted and read from there.
An already extracted occurrence table in any other file, e.g.
.csv,.txtor.tsv. The extension is not used to infer the format: the file must be tab-separated regardless.
In both cases the first column must be named gbifID; anything else stops
with an error. This also catches comma-separated files, whose header then
arrives as a single column.
An archive is extracted into a dedicated directory rather than next to the
ZIP file, so a ZIP stored in inst/extdata is never modified.
The function performs the following steps:
Validates that
pathis a non-empty single character string.Resolves the file to read. For a
.zippath the archive members are listed: a 'SIMPLE_CSV' download holds a single data file which is extracted by name, while a 'DWCA' holds several members (typicallymeta.xml,occurrence.txt, and extension files) and itsoccurrence.txtcore file is assumed. For any other path the file itself is read directly.Reads the header of the resolved file with
data.table::fread()usingnrows = 0, and requires its first column to be namedgbifID. Only the header is parsed, so this check is nearly free and rejects unusable files before the full read.Reads the tab-separated, UTF-8 occurrence file with
data.table::fread(), selecting only the GBIF fields used by VasGBIF.Coerces
gbifIDto character.
References
GBIF.org (23 July 2026) GBIF Occurrence Download doi:10.15468/dl.nt5exp
See also
print.import()for a one-line record count.data.table::fread()for delimited-file import.unzip()for ZIP archive handling.GBIF download formats for the difference between 'SIMPLE_CSV' and 'DWCA' downloads.
Examples
if (FALSE) { # interactive()
gbif_file <- system.file(
"extdata",
"0003386-260721160103020.zip",
package = "VasGBIF"
)
occ <- import_records(path = gbif_file)
occ_import <- extract_gbif_issues(occ)
head(occ_import$summary, 5)
}
