Learn R Programming

ctrdata (version 1.1)

dbGetFieldsIntoDf: Create data frame by extracting specified fields from database collection

Description

With this convenience function, fields in the mongo database are retrieved into an R dataframe. As mongo json fields within the record of a trial can be hierarchical and structured, the function flattens the data and returns a concatenation of values if there is more than one value or if the field is (in) an array, such as follows: value 1 / value 2 / ... (see example)

Usage

dbGetFieldsIntoDf(fields = "", con, verbose = FALSE, stopifnodata = TRUE)

Arguments

fields

Vector of one or more strings, with names of the sought fields. See function dbFindFields for how to find names of fields.

con

A src connection object, as obtained with nodbi::src_mongo() or nodbi::src_sqlite()

verbose

Printing additional information if set to TRUE; default is FALSE.

stopifnodata

Stops with an error (TRUE, default) or with a warning (FALSE) if the sought field is empty in all, or not available in any of the records in the database collection.

Value

A data frame with columns corresponding to the sought fields. Note: a column for the record _id will always be included. The maximum number of rows of the returned data frame is equal to, or less than the number of records in the data base.

Details

For more sophisticated data retrieval from the database, see vignette examples and other packages to query mongodb such as mongolite.

Examples

Run this code
# NOT RUN {
# }
# NOT RUN {
dbGetFieldsIntoDf("b1_sponsor.b31_and_b32_status_of_the_sponsor")[1,]
#                   _id  b1_sponsor.b31_and_b32_status_of_the_sponsor
#  1  2004-000015-25-GB                   Non-commercial / Commercial

dbGetFieldsIntoDf("keyword")[1,]
#            _id                                           keyword
#  1 NCT00129259  T1D / type 1 diabetes / type 1 diabetes mellitus

# }
# NOT RUN {
# }

Run the code above in your browser using DataLab