Skip to content

Commit 6ecf206

Browse files
committed
code intendation and styling
1 parent efc7d3a commit 6ecf206

11 files changed

+100
-79
lines changed

R/add_id_statement.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ add_id_statement <- function(
2121
wikibase_type = "external-id",
2222
wikibase_api_url = "https://reprexbase.eu/demowiki/api.php",
2323
csrf) {
24-
2524
datavalue <- paste0('"', o, '"')
2625
datavalue
2726

R/add_statement.R

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ add_statement <- function(
2323
wikibase_type,
2424
wikibase_api_url = "https://reprexbase.eu/demowiki/api.php",
2525
csrf) {
26-
2726
if (wikibase_type == "string") {
2827
add_item_statement(
2928
qid = qid, pid = pid, o = o,

R/check_existing_item.R

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
#' @return A data.frame or NULL.
2323
#' @examples
2424
#' check_existing_item(
25-
#' search_term="Estonian National Museum",
26-
#' language = "en",
27-
#' wikibase_api_url="https://www.wikidata.org/w/api.php",
28-
#' csrf=NULL)
25+
#' search_term = "Estonian National Museum",
26+
#' language = "en",
27+
#' wikibase_api_url = "https://www.wikidata.org/w/api.php",
28+
#' csrf = NULL
29+
#' )
2930
#' @export
3031

3132
check_existing_item <- function(search_term,
@@ -39,7 +40,6 @@ check_existing_item <- function(search_term,
3940
data_curator = person("Unknown", "Person"),
4041
wikibase_api_url = "https://www.wikidata.org/w/api.php",
4142
csrf = NULL) {
42-
4343
action_timestamp <- action_timestamp_create()
4444
action_time <- Sys.time()
4545

@@ -76,9 +76,11 @@ check_existing_item <- function(search_term,
7676
this_display$label$value == search_term && this_display$label$language == language
7777
}
7878

79-
matching_items <- vapply(1:length(search_response$search),
80-
function(x) search_response$search[[x]]$id,
81-
character(1))
79+
matching_items <- vapply(
80+
1:length(search_response$search),
81+
function(x) search_response$search[[x]]$id,
82+
character(1)
83+
)
8284

8385

8486
exact_match <- vapply(
@@ -87,14 +89,22 @@ check_existing_item <- function(search_term,
8789
logical(1)
8890
)
8991

90-
if (sum(exact_match)>1) {
91-
stop("Multiple items [", paste(matching_items, collapse=", "), "] are matching '", search_term, "' in language='", language, "'." )
92+
if (sum(exact_match) > 1) {
93+
stop("Multiple items [", paste(matching_items, collapse = ", "), "] are matching '", search_term, "' in language='", language, "'.")
9294
}
9395

94-
if (! any(exact_match)) { return(NULL) }
95-
if ( is.null(search_response$search[[1]])) { return(NULL) }
96-
if ( ! is.list(search_response$search[[1]])) { return(NULL) }
97-
if ( is.null(search_response$search[[which(exact_match)]])) { return(NULL)}
96+
if (!any(exact_match)) {
97+
return(NULL)
98+
}
99+
if (is.null(search_response$search[[1]])) {
100+
return(NULL)
101+
}
102+
if (!is.list(search_response$search[[1]])) {
103+
return(NULL)
104+
}
105+
if (is.null(search_response$search[[which(exact_match)]])) {
106+
return(NULL)
107+
}
98108

99109
matching_items[exact_match]
100110

@@ -181,7 +191,7 @@ check_existing_item <- function(search_term,
181191
)
182192
)
183193

184-
prefix <- ifelse(wikibase_api_url=="https://www.wikidata.org/w/api.php", "wbi:", "wd:")
194+
prefix <- ifelse(wikibase_api_url == "https://www.wikidata.org/w/api.php", "wbi:", "wd:")
185195
return_ds$rowid <- defined(paste0(prefix, as.character(return_ds$id_on_target)),
186196
namespace = wikibase_api_url
187197
)

R/check_existing_property.R

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
2424
#' @examples
2525
#' # No CSRF needed for Wikidata, but you will need it for Wikibase Suit
2626
#' check_existing_property(
27-
#' search_term="instance of",
28-
#' language = "en",
29-
#' wikibase_api_url="https://www.wikidata.org/w/api.php")
27+
#' search_term = "instance of",
28+
#' language = "en",
29+
#' wikibase_api_url = "https://www.wikidata.org/w/api.php"
30+
#' )
3031
#' @export
3132

3233
check_existing_property <- function(
@@ -39,9 +40,8 @@ check_existing_property <- function(
3940
action = "create_property",
4041
log_file_name = NA_character_,
4142
data_curator = person("Unknown", "Person"),
42-
wikibase_api_url="https://www.wikidata.org/w/api.php",
43-
csrf=NULL) {
44-
43+
wikibase_api_url = "https://www.wikidata.org/w/api.php",
44+
csrf = NULL) {
4545
action_timestamp <- action_timestamp_create()
4646
action_time <- Sys.time()
4747

@@ -61,8 +61,9 @@ check_existing_property <- function(
6161
)
6262

6363
search_response <- httr::content(get_search,
64-
as = "parsed",
65-
type = "application/json")
64+
as = "parsed",
65+
type = "application/json"
66+
)
6667

6768
if (!is.null(search_response$error)) {
6869
stop(paste(search_response$error$code, ": ", search_response$error$info))
@@ -87,24 +88,33 @@ check_existing_property <- function(
8788
logical(1)
8889
)
8990

90-
if (sum(exact_match)>1) {
91-
stop("Multiple items [", paste(matching_props, collapse=", "), "] are matching '", search_term, "' in language='", language, "'." )
91+
if (sum(exact_match) > 1) {
92+
stop("Multiple items [", paste(matching_props, collapse = ", "), "] are matching '", search_term, "' in language='", language, "'.")
9293
}
9394

94-
if (! any(exact_match)) { return(NULL) }
95-
if ( is.null(search_response$search[[1]])) { return(NULL) }
96-
if ( ! is.list(search_response$search[[1]])) { return(NULL) }
97-
if ( is.null(search_response$search[[which(exact_match)]])) { return(NULL)}
95+
if (!any(exact_match)) {
96+
return(NULL)
97+
}
98+
if (is.null(search_response$search[[1]])) {
99+
return(NULL)
100+
}
101+
if (!is.list(search_response$search[[1]])) {
102+
return(NULL)
103+
}
104+
if (is.null(search_response$search[[which(exact_match)]])) {
105+
return(NULL)
106+
}
98107

99108
matching_props[exact_match]
100109

101110
matching_property_data <- search_response$search[[which(exact_match)]]
102111

103112
if (action %in% c("create_property", "copy_property")) {
104113
datatype <- get_property_definition(matching_property_data$id, "en",
105-
wikibase_api_url = wikibase_api_url,
106-
return_type = "data.frame",
107-
csrf= csrf)$datatype
114+
wikibase_api_url = wikibase_api_url,
115+
return_type = "data.frame",
116+
csrf = csrf
117+
)$datatype
108118
comment_text <- glue::glue("A property with the label ", search_term, " already exists in this Wikibase.")
109119
}
110120

@@ -187,8 +197,9 @@ check_existing_property <- function(
187197
)
188198
)
189199

190-
prefix <- ifelse(wikibase_api_url=="https://www.wikidata.org/w/api.php",
191-
"wd:", "wbi:")
200+
prefix <- ifelse(wikibase_api_url == "https://www.wikidata.org/w/api.php",
201+
"wd:", "wbi:"
202+
)
192203

193204
return_ds$rowid <- defined(paste0(prefix, as.character(return_ds$id_on_target)),
194205
namespace = wikibase_api_url

R/copy_wikidata_item.R

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ copy_wikidata_item <- function(
7878
log_file_name = NULL,
7979
csrf,
8080
wikibase_session = NULL) {
81-
8281
if (!is.null(wikibase_session)) {
8382
# For repeated queries you can add your variables directly or in a list
8483

@@ -388,7 +387,6 @@ copy_wikidata_item <- function(
388387
time = action_timestamp,
389388
logfile = log_file_name
390389
)
391-
392390
} else if (
393391
# Case when we have clear message about a label conflict
394392
any(c(
@@ -552,11 +550,11 @@ copy_wikidata_item <- function(
552550
namespace = wikibase_api_url
553551
)
554552

555-
if(!is.null(log_file_name) && nchar(log_file_name)>0 ) {
553+
if (!is.null(log_file_name) && nchar(log_file_name) > 0) {
556554
write_csv(return_dataframe,
557-
file = log_file_name,
558-
na = "NA",
559-
append = TRUE
555+
file = log_file_name,
556+
na = "NA",
557+
append = TRUE
560558
)
561559
}
562560
return_ds

R/copy_wikidata_property.R

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ copy_wikidata_property <- function(
8080
log_file_name = NULL,
8181
csrf,
8282
wikibase_session = NULL) {
83-
8483
if (!is.null(wikibase_session)) {
8584
# For repeated queries you can add your variables directly or in a list
8685

@@ -377,7 +376,6 @@ copy_wikidata_property <- function(
377376
time = action_timestamp,
378377
logfile = log_file_name
379378
)
380-
381379
} else if ("wikibase-validator-label-conflict" %in% unlist(created_property_response$error$messages)) {
382380
# Unwrap error message and send it to terminal
383381
message_strings <- unlist(created_property_response$error$messages)
@@ -438,7 +436,6 @@ copy_wikidata_property <- function(
438436
time = action_timestamp,
439437
logfile = log_file_name
440438
)
441-
442439
} else {
443440
# Return an emptier data.frame if there was some error
444441

@@ -539,11 +536,11 @@ copy_wikidata_property <- function(
539536
namespace = wikibase_api_url
540537
)
541538

542-
if(!is.null(log_file_name) && nchar(log_file_name)>0 ) {
539+
if (!is.null(log_file_name) && nchar(log_file_name) > 0) {
543540
write_csv(return_dataframe,
544-
file = log_file_name,
545-
na = "NA",
546-
append = TRUE
541+
file = log_file_name,
542+
na = "NA",
543+
append = TRUE
547544
)
548545
}
549546

R/get_propety_definition.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ get_property_definition <- function(
5050
wikibase_api_url = "https://www.wikidata.org/w/api.php",
5151
return_type = "JSON",
5252
csrf = NULL) {
53-
5453
## Ensure that the pid is a character string starting with P followed by
5554
## numbers.
5655
pid <- as.character(pid)
@@ -226,4 +225,3 @@ get_property_definition <- function(
226225
)
227226
}
228227
}
229-
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
test_that("check_existing_item() works", {
2-
test <- check_existing_item(search_term="Estonian National Museum",
3-
language = "en")
4-
expect_equal(as.character(test$rowid),
5-
"wbi:Q1370397")
2+
test <- check_existing_item(
3+
search_term = "Estonian National Museum",
4+
language = "en"
5+
)
6+
expect_equal(
7+
as.character(test$rowid),
8+
"wbi:Q1370397"
9+
)
610
})

tests/testthat/test-check_existing_property.R

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
test_that("check_existing_property() works", {
2-
test <- check_existing_property(search_term="instance of", language = "en")
3-
expect_equal(as.character(test$rowid),
4-
"wd:P31")
5-
expect_equal(test$datatype,
6-
"wikibase-item")
2+
test <- check_existing_property(search_term = "instance of", language = "en")
3+
expect_equal(
4+
as.character(test$rowid),
5+
"wd:P31"
6+
)
7+
expect_equal(
8+
test$datatype,
9+
"wikibase-item"
10+
)
711
})
8-
9-
10-

tests/testthat/test-create_item.R

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ agya_wikibase_session <- list(
2424
)
2525

2626
digital_record_class <- create_item(
27-
label="digital record",
28-
description="a subclass of the record, a record in the collection of the Andrássy Gyula Foundation.",
27+
label = "digital record",
28+
description = "a subclass of the record, a record in the collection of the Andrássy Gyula Foundation.",
2929
language = "en",
30-
classification_property = "P137", #subclass of
31-
classification_id = "Q584", #record
30+
classification_property = "P137", # subclass of
31+
classification_id = "Q584", # record
3232
wikibase_api_url = agya_wikibase_api_url,
3333
csrf = agya_wikibase_session$csrf,
34-
wikibase_session = agya_wikibase_session)
34+
wikibase_session = agya_wikibase_session
35+
)

0 commit comments

Comments
 (0)