ropensci / patentsview

An R client to the PatentsView API
https://docs.ropensci.org/patentsview
Other
31 stars 9 forks source link

with_qfuns should respect calling environment #20

Closed jcheng5 closed 4 years ago

jcheng5 commented 4 years ago

with_qfuns doesn't find objects in the calling environment, only objects in qry_funs or the base environment.

library(patentsview)

create_query <- function(date_of_patent) {
  with_qfuns(
   and(
     gte(patent_date = date_of_patent),
     text_phrase(patent_abstract = c("computer program")),
     or(
       eq(inventor_last_name = "ihaka"),
       eq(inventor_first_name = "chris")
     )
   )
  )
}

create_query("2007-01-01")
#> Error in gte(patent_date = date_of_patent): object 'date_of_patent' not found

Created on 2020-03-24 by the reprex package (v0.3.0)

This alternate implementation of with_qfuns solves it:

with_qfuns <- function(code, envir = parent.frame()) {
  eval(substitute(code), qry_funs, envir)
}

create_query("2007-01-01")
#> {"_and":[{"_gte":{"patent_date":"2007-01-01"}},{"_text_phrase":{"patent_abstract":"computer program"}},{"_or":[{"_eq":{"inventor_last_name":"ihaka"}},{"_eq":{"inventor_first_name":"chris"}}]}]}
crew102 commented 4 years ago

Thanks!