Skip to content

Commit 74f6b2e

Browse files
committed
tidying up via styler
1 parent 76bb057 commit 74f6b2e

21 files changed

+237
-242
lines changed

R/cast-pv-data.R

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ get_cast_fun <- function(data_type) {
66
# Some fields aren't documented, so we don't know what their data type is. Use
77
# string type for these.
88
if (length(data_type) != 1) data_type <- "string"
9-
switch(
10-
data_type,
9+
switch(data_type,
1110
"string" = as_is,
1211
"date" = as.Date,
1312
"float" = as.numeric,
@@ -97,7 +96,7 @@ cast_one <- function(one, name, typesdf) UseMethod("cast_one")
9796
cast_pv_data <- function(data) {
9897
validate_pv_data(data)
9998

100-
entity_name <- names(data) # preserve insanity, singular/plural/nonsensical name
99+
entity_name <- names(data) # preserve insanity, singular/plural/nonsensical name
101100
endpoint <- endpoint_from_entity(entity_name)
102101

103102
typesdf <- fieldsdf[fieldsdf$endpoint == endpoint, c("common_name", "data_type")]
@@ -110,7 +109,7 @@ cast_pv_data <- function(data) {
110109

111110
df[] <- list_out
112111
out_data <- list(x = df)
113-
names(out_data) <- entity_name # preserve entity's name
112+
names(out_data) <- entity_name # preserve entity's name
114113

115114
structure(
116115
out_data,

R/check-query.R

+17-12
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,39 @@ swap_null_nms <- function(obj) {
55
}
66

77
#' @noRd
8-
is_int <- function(x)
9-
if (is.numeric(x)) abs(x - round(x)) < .Machine$double.eps^ 0.5 else FALSE
8+
is_int <- function(x) {
9+
if (is.numeric(x)) abs(x - round(x)) < .Machine$double.eps^0.5 else FALSE
10+
}
1011

1112
#' @noRd
12-
is_date <- function(x)
13+
is_date <- function(x) {
1314
grepl("[12][[:digit:]]{3}-[01][[:digit:]]-[0-3][[:digit:]]", x)
15+
}
1416

1517
#' @noRd
1618
one_check <- function(operator, field, value, f1) {
17-
18-
if (nrow(f1) == 0)
19+
if (nrow(f1) == 0) {
1920
stop2(field, " is not a valid field to query for your endpoint")
20-
if (f1$data_type == "date" && !is_date(value))
21+
}
22+
if (f1$data_type == "date" && !is_date(value)) {
2123
stop2("Bad date: ", value, ". Date must be in the format of yyyy-mm-dd")
22-
if (f1$data_type %in% c("string", "fulltext") && !is.character(value))
24+
}
25+
if (f1$data_type %in% c("string", "fulltext") && !is.character(value)) {
2326
stop2(value, " must be of type character")
24-
if (f1$data_type == "integer" && !is_int(value))
27+
}
28+
if (f1$data_type == "integer" && !is_int(value)) {
2529
stop2(value, " must be an integer")
30+
}
2631

2732
if (
2833
# The new version of the API blurrs the distinction between string/fulltext fields.
2934
# It looks like the string/fulltext functions can be used interchangeably
30-
(operator %in% c("_begins", "_contains", "_text_all", "_text_any", "_text_phrase") &&
35+
(operator %in% c("_begins", "_contains", "_text_all", "_text_any", "_text_phrase") &&
3136
!(f1$data_type == "fulltext" || f1$data_type == "string")) ||
32-
(f1$data_type %in% c("string", "fulltext") &&
33-
operator %in% c("_gt", "_gte", "_lt", "_lte"))
34-
)
37+
(f1$data_type %in% c("string", "fulltext") &&
38+
operator %in% c("_gt", "_gte", "_lt", "_lte")) ) {
3539
stop2("You cannot use the operator ", operator, " with the field ", field)
40+
}
3641
}
3742

3843
#' @noRd

R/data.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
#' \item{group}{The group the field belongs to}
1616
#' \item{common_name}{The field name without the parent group structure}
1717
#' }
18-
"fieldsdf"
18+
"fieldsdf"

R/get-fields.R

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ get_fields <- function(endpoint, groups = NULL) {
6161
#' @return A character vector with the names of each endpoint.
6262
#' @export
6363
get_endpoints <- function() {
64-
6564
# now the endpoints are singular
6665
# note that now there are two rel_app_texts, one under patents and one under publications
6766
c(

R/print.R

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ print.pv_data_result <- function(x, ...) {
2424
)
2525

2626
utils::str(
27-
x, vec.len = 1, max.level = 2, give.attr = FALSE, strict.width = "cut"
27+
x,
28+
vec.len = 1, max.level = 2, give.attr = FALSE, strict.width = "cut"
2829
)
2930
}
3031

3132
#' @export
3233
print.pv_relay_db <- function(x, ...) {
3334
utils::str(
34-
x, vec.len = 1, max.level = 2, give.attr = FALSE, strict.width = "cut"
35+
x,
36+
vec.len = 1, max.level = 2, give.attr = FALSE, strict.width = "cut"
3537
)
3638
}
3739

R/query-dsl.R

+12-12
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ create_between_fun <- function(fun) {
133133
#'
134134
#' There is 1 convenience function:
135135
#' \itemize{
136-
#' \item \code{in_range} - Builds a <= x <= b query
136+
#' \item \code{in_range} - Builds a <= x <= b query
137137
#' }
138138
#'
139139
#' @return An object of class \code{pv_query}. This is basically just a simple
@@ -144,9 +144,9 @@ create_between_fun <- function(fun) {
144144
#'
145145
#' qry_funs$not(qry_funs$eq(patent_date = "2001-01-01"))
146146
#'
147-
#' qry_funs$in_range(patent_year=c(2010, 2021))
147+
#' qry_funs$in_range(patent_year = c(2010, 2021))
148148
#'
149-
#' qry_funs$in_range(patent_date=c("1970-01-01","1983-02-28"))
149+
#' qry_funs$in_range(patent_date = c("1970-01-01", "1983-02-28"))
150150

151151
#' @export
152152
qry_funs <- c(
@@ -185,16 +185,16 @@ qry_funs <- c(
185185
#' )
186186
#' )
187187
#'
188-
#' #...With it, this becomes:
188+
#' # ...With it, this becomes:
189189
#' with_qfuns(
190-
#' and(
191-
#' gte(patent_date = "2007-01-01"),
192-
#' text_phrase(patent_abstract = c("computer program")),
193-
#' or(
194-
#' eq(inventors.inventor_name_last = "Ihaka"),
195-
#' eq(inventors.inventor_name_last = "Chris")
196-
#' )
197-
#' )
190+
#' and(
191+
#' gte(patent_date = "2007-01-01"),
192+
#' text_phrase(patent_abstract = c("computer program")),
193+
#' or(
194+
#' eq(inventors.inventor_name_last = "Ihaka"),
195+
#' eq(inventors.inventor_name_last = "Chris")
196+
#' )
197+
#' )
198198
#' )
199199
#'
200200
#' @export

R/search-pv.R

+23-23
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ one_request <- function(method, query, base_url, arg_list, api_key, ...) {
8484

8585
one_request(method, query, base_url, arg_list, api_key, ...)
8686
} else {
87-
8887
if (httr::http_error(resp)) throw_er(resp)
8988

9089
resp
@@ -248,7 +247,6 @@ search_pv <- function(query,
248247
error_browser = NULL,
249248
api_key = Sys.getenv("PATENTSVIEW_API_KEY"),
250249
...) {
251-
252250
validate_args(api_key, fields, endpoint, method, page, per_page, sort)
253251
deprecate_warn_all(error_browser, subent_cnts, mtchd_subent_only)
254252

@@ -269,7 +267,9 @@ search_pv <- function(query,
269267

270268
result <- one_request(method, query, base_url, arg_list, api_key, ...)
271269
result <- process_resp(result)
272-
if (!all_pages) return(result)
270+
if (!all_pages) {
271+
return(result)
272+
}
273273

274274
# Here we ignore the user's sort and instead have the API sort by the primary
275275
# key for the requested endpoint. This simplifies the paging's after parameter.
@@ -291,15 +291,15 @@ search_pv <- function(query,
291291
# The API throws an error if the sort field is not present in the fields list
292292
# **Should we remember if we added the primary_sort_key and remove it before
293293
# returning data to the user?
294-
primary_sort_key <- get_default_sort(endpoint)
294+
primary_sort_key <- get_default_sort(endpoint)
295295

296296
if (!names(primary_sort_key) %in% fields) fields <- c(fields, names(primary_sort_key))
297297

298298
arg_list <- to_arglist(fields, page, per_page, primary_sort_key)
299299
full_data <- request_apply(result, method, query, base_url, arg_list, api_key, ...)
300300

301301
# apply the user's sort
302-
data.table::setorderv(full_data, names(sort), ifelse(as.vector(sort)=="asc", 1, -1))
302+
data.table::setorderv(full_data, names(sort), ifelse(as.vector(sort) == "asc", 1, -1))
303303
result$data[[1]] <- full_data
304304

305305
result
@@ -321,41 +321,41 @@ search_pv <- function(query,
321321
#'
322322
#' retrieve_linked_data(
323323
#' "https://search.patentsview.org/api/v1/cpc_group/G01S7:4811/"
324-
#' )
324+
#' )
325325
#'
326326
#' retrieve_linked_data(
327327
#' 'https://search.patentsview.org/api/v1/patent/?q={"_text_any":{"patent_title":"COBOL cotton gin"}}&s=[{"patent_id": "asc" }]&o={"size":50}&f=["inventors.inventor_name_last","patent_id","patent_date","patent_title"]'
328-
#' )
328+
#' )
329329
#' }
330330
#'
331331
#' @export
332332
retrieve_linked_data <- function(url,
333333
api_key = Sys.getenv("PATENTSVIEW_API_KEY"),
334-
...
335-
) {
336-
334+
...) {
337335
# There wouldn't be url parameters on a HATEOAS link but we'll also accept
338336
# example urls from the documentation, where there could be parameters
339337
url_peices <- httr::parse_url(url)
340338
params <- list()
341-
query <- ''
339+
query <- ""
342340

343-
if(!is.null(url_peices$query))
344-
{
345-
url = paste0(url_peices$scheme,'://',url_peices$hostname, '/', url_peices$path)
341+
if (!is.null(url_peices$query)) {
342+
url <- paste0(url_peices$scheme, "://", url_peices$hostname, "/", url_peices$path)
346343

347-
# need to change f to fields, s to sort and o to opts
348-
# probably a whizbangy better way to do this in R
349-
if(!is.null(url_peices$query$f))
350-
params$fields = jsonlite::fromJSON(url_peices$query$f)
344+
# need to change f to fields, s to sort and o to opts
345+
# probably a whizbangy better way to do this in R
346+
if (!is.null(url_peices$query$f)) {
347+
params$fields <- jsonlite::fromJSON(url_peices$query$f)
348+
}
351349

352-
if(!is.null(url_peices$query$s))
353-
params$sort = jsonlite::fromJSON(url_peices$query$s)
350+
if (!is.null(url_peices$query$s)) {
351+
params$sort <- jsonlite::fromJSON(url_peices$query$s)
352+
}
354353

355-
if(!is.null(url_peices$query$o))
356-
params$opts = jsonlite::fromJSON(url_peices$query$o)
354+
if (!is.null(url_peices$query$o)) {
355+
params$opts <- jsonlite::fromJSON(url_peices$query$o)
356+
}
357357

358-
query <- if(!is.null(url_peices$query$q)) url_peices$query$q else ''
358+
query <- if (!is.null(url_peices$query$q)) url_peices$query$q else ""
359359
}
360360

361361
# Only send the API key to subdomains of patentsview.org

R/unnest-pv-data.R

+19-22
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,30 @@
1818
#'
1919
#' @export
2020
get_ok_pk <- function(endpoint) {
21+
# most of the nested endpoints use patent_id, except patent/attorney uses "attorney_id"
22+
# publications (unnested) uses document_number
23+
use_document_number <- c(
24+
# if endpoint is passed
25+
"publication", "publication/rel_app_text",
2126

22-
# most of the nested endpoints use patent_id, except patent/attorney uses "attorney_id"
23-
# publications (unnested) uses document_number
24-
use_document_number <- c(
25-
# if endpoint is passed
26-
"publication", "publication/rel_app_text",
27-
28-
# if names(pv_out[["data"]]) passed
29-
"publications", "rel_app_text_publications"
30-
)
27+
# if names(pv_out[["data"]]) passed
28+
"publications", "rel_app_text_publications"
29+
)
3130

32-
use_patent_id <- c(
33-
# if endpoint passed
34-
"patent/us_application_citation", "patent/us_patent_citation",
35-
"patent/rel_app_text", "patent/foreign_citation", "patent/rel_app_text",
36-
"publication/rel_app_text",
31+
use_patent_id <- c(
32+
# if endpoint passed
33+
"patent/us_application_citation", "patent/us_patent_citation",
34+
"patent/rel_app_text", "patent/foreign_citation", "patent/rel_app_text",
35+
"publication/rel_app_text",
3736

38-
# if names(pv_out[["data"]]) passed
39-
"us_application_citations", "us_patent_citations", "foreign_citations",
40-
"rel_app_texts", "patents","brf_sum_texts","detail_desc_texts",
41-
"draw_desc_texts"
42-
)
37+
# if names(pv_out[["data"]]) passed
38+
"us_application_citations", "us_patent_citations", "foreign_citations",
39+
"rel_app_texts", "patents", "brf_sum_texts", "detail_desc_texts",
40+
"draw_desc_texts"
41+
)
4342
if (endpoint %in% use_document_number) {
4443
"document_number"
45-
}
46-
else if (endpoint %in% use_patent_id ) {
44+
} else if (endpoint %in% use_patent_id) {
4745
"patent_id"
4846
} else {
4947
key <- to_singular(endpoint)
@@ -86,7 +84,6 @@ use_patent_id <- c(
8684
#'
8785
#' @export
8886
unnest_pv_data <- function(data, pk = get_ok_pk(names(data))) {
89-
9087
validate_pv_data(data)
9188

9289
df <- data[[1]]

R/utils.R

+25-25
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ to_singular <- function(plural) {
5454
# wipo endpoint returns singluar wipo as the entity
5555
# attorneys accidently works
5656

57-
if(plural == "wipo") {
58-
singular = plural
57+
if (plural == "wipo") {
58+
singular <- plural
5959
} else if (endsWith(plural, "ies")) {
6060
singular <- sub("ies$", "y", plural)
6161
} else if (endsWith(plural, "es") && !endsWith(plural, "ees")) {
@@ -75,11 +75,10 @@ to_plural <- function(singular) {
7575
singular <- sub("^patent/", "", singular)
7676
singular <- sub("^publication/", "", singular)
7777

78-
if(singular == "wipo") {
79-
plural = singular
80-
}
81-
else if (singular == "attorney") {
82-
plural <- "attorneys"
78+
if (singular == "wipo") {
79+
plural <- singular
80+
} else if (singular == "attorney") {
81+
plural <- "attorneys"
8382
} else if (endsWith(singular, "y")) {
8483
plural <- sub("y$", "ies", singular)
8584
} else if (endsWith(singular, "s")) {
@@ -91,22 +90,23 @@ to_plural <- function(singular) {
9190

9291
#' @noRd
9392
endpoint_from_entity <- function(entity) {
94-
# needed for casting to work with singular endpoints and mostly plural entities
95-
96-
if(entity == "rel_app_text_publications") {
97-
"publication/rel_app_texts"
98-
} else if (entity == "rel_app_text") {
99-
"publication/rel_app_text"
100-
} else if (entity == "other_reference") {
101-
"patent/otherreference"
102-
} else {
103-
singular <- to_singular(entity)
104-
105-
# figure out if the endpoint is nested
106-
nested <- c("attorney", "foreign_citation", "us_application_citation",
107-
"us_patent_citation", "rel_app_text")
108-
109-
if(singular %in% nested) paste0("patent/", singular) else singular
110-
}
111-
}
93+
# needed for casting to work with singular endpoints and mostly plural entities
94+
95+
if (entity == "rel_app_text_publications") {
96+
"publication/rel_app_texts"
97+
} else if (entity == "rel_app_text") {
98+
"publication/rel_app_text"
99+
} else if (entity == "other_reference") {
100+
"patent/otherreference"
101+
} else {
102+
singular <- to_singular(entity)
103+
104+
# figure out if the endpoint is nested
105+
nested <- c(
106+
"attorney", "foreign_citation", "us_application_citation",
107+
"us_patent_citation", "rel_app_text"
108+
)
112109

110+
if (singular %in% nested) paste0("patent/", singular) else singular
111+
}
112+
}

0 commit comments

Comments
 (0)