Create data frame of specified fields from database collection
Source:R/dbGetFieldsIntoDf.R
dbGetFieldsIntoDf.Rd
Fields in the collection are retrieved from all records into a data frame (or tibble). Within a given trial record, a fields can be hierarchical and structured, that is, nested. Th function uses the field names to appropriately type the values that it returns, harmonising original values (e.g. "Information not present in EudraCT" to `NA`, "Yes" to `TRUE`, "false" to `FALSE`, date strings to dates or time differences, number strings to numbers). The function simplifies the structure of nested data and may concatenate multiple strings in a field using " / " (see example) and may have widened the returned data frame with additional columns that were recursively expanded from simply nested data (e.g., "externalRefs" to columns "externalRefs.doi", "externalRefs.eudraCTNumber" etc.). For an alternative way for handling the complex nested data, see dfTrials2Long followed by dfName2Value for extracting the sought variable(s).
Arguments
- fields
Vector of one or more strings, with names of sought fields. See function dbFindFields for how to find names of fields and ctrShowOneTrial for interactively selecting field names. Dot path notation ("field.subfield") without indices is supported. If compatibility with `nodbi::src_postgres()` is needed, specify fewer than 50 fields, consider also using parent fields e.g., `"a.b"` instead of `c("a.b.c.d", "a.b.c.e")`, accessing sought fields with dfTrials2Long followed by dfName2Value or other R functions.
- con
A database connection object, created with
nodbi
. See section `1 - Database connection` in ctrdata.- verbose
Printing additional information if set to
TRUE
; (defaultFALSE
).- ...
Do not use (captures deprecated parameter
stopifnodata
)
Value
A data frame (or tibble, if tibble
is loaded)
with columns corresponding to the sought fields.
A column for the records' `_id` will always be included.
The maximum number of rows of the returned data frame is equal to,
or less than the number of trial records in the database collection.
Examples
dbc <- nodbi::src_sqlite(
dbname = system.file("extdata", "demo.sqlite", package = "ctrdata"),
collection = "my_trials",
RSQLite::SQLITE_RO)
#> RSQLite version has enabled accelerating docdb_create() and docdb_update() functions when used with value = <NDJSON file name>.
# get fields that are nested within another field
# and can have multiple values with the nested field
dbGetFieldsIntoDf(
fields = "b1_sponsor.b31_and_b32_status_of_the_sponsor",
con = dbc)
#> _id b1_sponsor.b31_and_b32_status_of_the_sponsor
#> 1 2012-003632-23-CZ Commercial
#> 2 2012-003632-23-SE Commercial
#> 3 2014-002606-20-PT Commercial
#> 4 2014-003556-31-GB Commercial
#> 5 2014-003556-31-SE Commercial
# fields that are lists of string values are
# returned by concatenating values with a slash
dbGetFieldsIntoDf(
fields = "keyword",
con = dbc)
#> _id
#> 1 NCT03280147
#> 2 NCT03325439
#> 3 NCT03431558
#> 4 NCT04001712
#> 5 NCT04041765
#> keyword
#> 1 Neonate / Sepsis / Antibiotics / Duration
#> 2 Electroencephalographic neonatal seizures / Brivaracetam / Epilepsy / ENS / Newborns / Pharmacokinetic
#> 3 Bovine Lactoferrin, Neonatal infection, Low Birth Weight
#> 4 early caffeine preterm
#> 5 IgM-enriched Intravenous Immunoglobulin