Skip to content

Commit 897af81

Browse files
committed
Many documentation improvements
1 parent 04ccfc0 commit 897af81

20 files changed

+183
-134
lines changed

DESCRIPTION

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: wbdataset
22
Title: Making Datasets Truly Interoperable and Reusable in R with Wikibase
3-
Version: 0.1.1052
4-
Date: 2024-06-02
3+
Version: 0.1.1053
4+
Date: 2024-06-06
55
Authors@R:
66
c(person(given="Daniel", family="Antal",
77
email= "[email protected]",
@@ -25,6 +25,7 @@ Suggests:
2525
httptest2,
2626
keyring,
2727
knitr,
28+
mockery,
2829
rmarkdown,
2930
spelling,
3031
testthat (>= 3.0.0)
@@ -42,6 +43,7 @@ Imports:
4243
jsonlite,
4344
magrittr,
4445
purrr,
46+
tibble,
4547
utils
4648
Language: en-US
4749
VignetteBuilder: knitr

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ importFrom(dataset,dataset_df)
2727
importFrom(dataset,defined)
2828
importFrom(dataset,dublincore)
2929
importFrom(dplyr,bind_rows)
30+
importFrom(dplyr,everything)
3031
importFrom(dplyr,filter)
3132
importFrom(dplyr,left_join)
3233
importFrom(dplyr,mutate)
@@ -43,3 +44,4 @@ importFrom(magrittr,"%>%")
4344
importFrom(purrr,safely)
4445
importFrom(tibble,tibble)
4546
importFrom(utils,person)
47+
importFrom(utils,write.table)

R/copy_wikidata_item.R

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ copy_wikidata_item <- function(
8787
csrf = NULL,
8888
wikibase_session = NULL) {
8989

90+
if (is.null(qid_equivalence_property)) qid_equivalence_property <- NA_character_
91+
validated_action <- "copy_wikidata_item()"
92+
9093
language <- resolve_from_session("language", language, wikibase_session)
9194
data_curator <- resolve_from_session("data_curator", data_curator, wikibase_session)
9295
log_file_name <- resolve_from_session("log_file_name", log_file_name, wikibase_session)
@@ -108,11 +111,10 @@ copy_wikidata_item <- function(
108111
equivalence_id = NA_character_,
109112
csrf = csrf,
110113
data_curator = data_curator,
111-
validated_action = "copy_wikidata_property()"
114+
validated_action = validated_action
112115
)
113116

114-
if (is.null(qid_equivalence_property)) qid_equivalence_property <- NA_character_
115-
117+
# Case when multiple items need to be copied ------------
116118
if (length(qid_on_wikidata) > 1) {
117119
return_log_file <- copy_wikidata_items(
118120
qid_on_wikidata = qid_on_wikidata,
@@ -143,15 +145,13 @@ copy_wikidata_item <- function(
143145
log_file_name <- ""
144146
}
145147

146-
147-
# Assert that qid_on_wikidata looks like a QID
148+
# Assert that qid_on_wikidata looks like a QID ----------------------
148149
qid_on_wikidata <- as.character(qid_on_wikidata)
149150
assert_that(is_qid(qid_on_wikidata),
150151
msg = "qid_on_wikidata must start with Q followed by digits."
151152
)
152153

153154
# Getting the data ---------------------------------------------------------
154-
155155
claim_body <- list(
156156
action = "wbgetentities",
157157
ids = qid_on_wikidata,
@@ -162,6 +162,7 @@ copy_wikidata_item <- function(
162162
# it saves various error exceptions when using httr::POST
163163
safely_post <- purrr::safely(httr::POST, NULL)
164164

165+
# The data is always received from Wikidata -------------------
165166
get_claim <- safely_post(
166167
"https://www.wikidata.org/w/api.php",
167168
body = claim_body,
@@ -540,10 +541,15 @@ copy_wikidata_item <- function(
540541
)
541542

542543
if (!is.null(log_file_name) && nchar(log_file_name) > 0) {
543-
write_csv(return_dataframe,
544+
write.table(
545+
return_dataframe,
544546
file = log_file_name,
547+
sep = ",",
548+
row.names = FALSE,
549+
col.names = !file.exists(log_file_name),
545550
na = "NA",
546-
append = TRUE
551+
append = TRUE,
552+
quote = TRUE
547553
)
548554
}
549555
return_ds

R/copy_wikidata_property.R

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
#' useful in interactive workflows or scripts where the same context is
5555
#' reused.
5656
#' @importFrom assertthat assert_that
57-
#' @importFrom utils person
57+
#' @importFrom utils person write.table
5858
#' @return Returns a \code{\link[dataset]{dataset_df}} object. The columns
5959
#' are:\cr
6060
#' \describe{
@@ -548,10 +548,15 @@ copy_wikidata_property <- function(
548548
)
549549

550550
if (!is.null(log_file_name) && nchar(log_file_name) > 0) {
551-
write_csv(return_dataframe,
551+
write.table(
552+
return_dataframe,
552553
file = log_file_name,
554+
sep = ",",
555+
row.names = FALSE,
556+
col.names = !file.exists(log_file_name),
553557
na = "NA",
554-
append = TRUE
558+
append = TRUE,
559+
quote = TRUE
555560
)
556561
}
557562

R/create_item.R

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#' useful in interactive workflows or scripts where the same context is
5757
#' reused.
5858
#' @export
59+
#' @importFrom utils write.table
5960
#' @return Returns a \code{\link[dataset]{dataset_df}} object.
6061
#' The columns are:\cr
6162
#' \describe{
@@ -392,10 +393,15 @@ create_item <- function(label,
392393
)
393394

394395
if(!is.null(log_file_name) && nchar(log_file_name)>0 ) {
395-
write_csv(return_dataframe,
396-
file = log_file_name,
397-
na = "NA",
398-
append = TRUE
396+
write.table(
397+
return_dataframe,
398+
file = log_file_name,
399+
sep = ",",
400+
row.names = FALSE,
401+
col.names = !file.exists(log_file_name),
402+
na = "NA",
403+
append = TRUE,
404+
quote = TRUE
399405
)
400406
}
401407

R/create_property.R

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#' useful in interactive workflows or scripts where the same context is
6262
#' reused.
6363
#' @export
64+
#' @importFrom utils write.table
6465
#' @return Returns a \code{\link[dataset]{dataset_df}} object.
6566
#' The columns are:\cr
6667
#' \describe{
@@ -388,10 +389,15 @@ create_property <- function(label,
388389
)
389390

390391
if(!is.null(log_file_name) && nchar(log_file_name)>0 ) {
391-
write_csv(return_dataframe,
392-
file = log_file_name,
393-
na = "NA",
394-
append = TRUE
392+
write.table(
393+
return_dataframe,
394+
file = log_file_name,
395+
sep = ",",
396+
row.names = FALSE,
397+
col.names = !file.exists(log_file_name),
398+
na = "NA",
399+
append = TRUE,
400+
quote = TRUE
395401
)
396402
}
397403

R/get_claim.R

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@
4949
#'
5050
#' @export
5151

52-
get_claim <- function(qid = "Q528626",
53-
pid = "P625",
54-
wikibase_api_url = "https://www.wikidata.org/w/api.php",
55-
csrf = NULL,
56-
first = TRUE) {
52+
get_claim <- function(
53+
qid = "Q528626",
54+
pid = "P625",
55+
wikibase_api_url = "https://www.wikidata.org/w/api.php",
56+
csrf = NULL,
57+
first = TRUE) {
58+
5759
if (!is_qid(qid)) {
5860
stop(sprintf("Invalid QID: '%s'. QIDs must begin with 'Q' followed by digits (e.g., 'Q42').", qid))
5961
}
@@ -158,19 +160,16 @@ get_claim <- function(qid = "Q528626",
158160
#' @title Deprecated: retrieve a claim from a Wikidata item
159161
#'
160162
#' @description
161-
#' \strong{Deprecated.} This function has been replaced by \code{\link{get_claim}}.
163+
#' \strong{Deprecated.} This function has been replaced by
164+
#' \code{\link{get_claim}}.
162165
#'
163166
#' @details
164167
#' \code{get_claims()} was used to retrieve a claim (statement) for a specific property from
165168
#' a Wikidata or Wikibase item. This function is now deprecated and will be removed in a
166169
#' future release. Please use \code{\link{get_claim}} instead, which provides more robust
167170
#' functionality and better support for modern data types.
168171
#'
169-
#' @param qid A character string giving the QID of the item.
170-
#' @param property A character string giving the property ID.
171-
#' @param wikibase_api_url The full URL of the Wikibase API endpoint (must end with \code{api.php}).
172-
#' @param csrf (Optional) A CSRF token, not used in this read-only function.
173-
#'
172+
#' @param ... Any parameters.
174173
#' @return A data frame or list depending on implementation. For updated behaviour, use \code{get_claim()}.
175174
#'
176175
#' @seealso \code{\link{get_claim}}

R/get_wikidata_item.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,22 @@
1212
#' By default, the function retrieves metadata in multiple languages,
1313
#' including English, French, German, Italian, and Spanish.
1414
#'
15+
#' @param title The title of the returned (log) dataset.
16+
#' @param prefix The prefix to be used as a namespace for downloaded items.
1517
#' @param qid_on_wikidata A character vector of Wikidata QIDs (e.g.,
1618
#' \code{"Q42"}).
19+
#' @param language Defaults to \code{c("en")}. A character string of
20+
#' the languages in which the users wants to receive the labels and
21+
#' descriptions of the property. The vector of languages must use \href{https://en.wikipedia.org/wiki/IETF_language_tag}{BCP
22+
#' 47}-compliant language tags (e.g., "en" for English, and "hu"
23+
#' for Hungarian.)
1724
#' @param wikibase_api_url The full URL of the Wikibase API endpoint (must end
1825
#' with \code{api.php}).
26+
#' @param data_curator The name of the data curator who runs the function and
27+
#' creates the log file, created with \link[utils]{person}.
28+
#' It is either given as a parameter or resolved from
29+
#' \code{wikibase_session}. If no curator is given, then filled with
30+
#' \code{person("Unknown", "Curator")}.
1931
#' @param fallback_language A BCP 47 language code (e.g., "en") used as a fallback
2032
#' when no label is available in the requested language(s). If a label is missing
2133
#' in one or more specified languages, this fallback will be used instead when available.
@@ -28,8 +40,10 @@
2840
#' @seealso \code{\link{get_claim}} to retrieve claims (properties) for Wikidata
2941
#' items.
3042
#' @importFrom dplyr select mutate filter bind_rows relocate left_join
43+
#' @importFrom dplyr everything
3144
#' @importFrom tibble tibble
3245
#' @importFrom dataset dataset_df defined dublincore
46+
#' @importFrom utils write.table
3347
#' @examples
3448
#' \dontrun{
3549
#' get_wikidata_item("Q42", language = c("hu"), fallback_language = "en")

R/handle_search_term_ambiguity.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
#' @param search_results A list of search results from the Wikibase API.
55
#' @param search_term The original search term used in the query.
66
#' @param language The language code used in the search.
7-
#' @param prefer_first Logical; if TRUE, returns the first match when multiple
8-
#' matches are found.
7+
#' @param strategy If \code{return_null} than a NULL value is returned if
8+
#' there are ambiguous results, if \code{return_first} then the first result
9+
#' is returned.
910
#' @return A single search result or NULL if ambiguity cannot be resolved.
1011
#' @keywords internal
1112
handle_search_term_ambiguity <- function(search_results,

R/left_join_column.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ left_join_column <- function(
7373
result_df <- dplyr::bind_rows(result_df)
7474

7575
if (property %in% names(ds)) {
76-
ds <- ds |>
76+
ds <- ds %>%
7777
dplyr::select(-dplyr::all_of(property))
7878
}
7979

0 commit comments

Comments
 (0)