Use R operators to build queries for PubMed

This is an extension to R operators +, -, |, and @ to gain the ability to paste character vectors. This is useful when building queries for the PubMed database (see for example the rentrez package).

Get it from GitHub:



Avoiding the explicit call to paste makes the code much cleaner:


query = "eeg" + "epilepsy" - "temporal"
#> eeg AND epilepsy NOT temporal

The @ operator can be used to add field specification:

query = "eeg" @ "TIAB" + "epilepsy" @ "MeSH"
#> eeg[TIAB] AND epilepsy[MeSH]

You can build nested queries using the g function (for "group"). You can add field specification to multiple terms at once by providing a character vector:

query = "pain" @ "TIAB" +
  g("eeg" | "seeg", "MeSH") -
  g("mri" | "fmri" | "meg", "TIAB")
#> pain[TIAB] AND (eeg[MeSH] OR seeg[MeSH]) NOT (mri[TIAB] OR fmri[TIAB] OR meg[TIAB])

Operators still work the usual way with numeric or logical inputs, but they may be slower, so beware:

# With redefined operators
system.time(runif(10^7) + runif(10^7))
#>    user  system elapsed 
#>   0.988   0.028   1.020

# With original operators
detach("package:qbuildr", unload = TRUE)
system.time(runif(10^7) + runif(10^7))
#>    user  system elapsed 
#>   0.888   0.008   0.896

The advantages and pitfalls of this approach are discussed here and here.

jchrom/qbuildr documentation built on May 18, 2019, 10:23 p.m.