Skip to content

Commit

Permalink
make reference bibentry format
Browse files Browse the repository at this point in the history
  • Loading branch information
sbfnk committed Dec 5, 2024
1 parent 336ea55 commit 4137e98
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
6 changes: 2 additions & 4 deletions R/as_contact_survey.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
#' - an element named 'participants', a data frame containing participant
#' information
#' - an element named 'contacts', a data frame containing contact information
#' - (optionally) an element named 'reference, a list containing information
#' information needed to reference the survey, in particular it can contain$a
#' "title", "bibtype", "author", "doi", "publisher", "note", "year"
#' @param id.column the column in both the `participants` and `contacts` data frames that links contacts to participants
#' @param country.column the column in the `participants` data frame containing the country in which the participant was queried
#' @param year.column the column in the `participants` data frame containing the year in which the participant was queried
#' - (optionally) an element named 'reference, a [utils::bibentry()] object
#' @importFrom checkmate assert_list assert_names assert_data_frame
#' assert_character
#' @importFrom purrr walk
Expand All @@ -28,12 +26,12 @@ as_contact_survey <- function(x, id.column = "part_id",
assert_names(names(x), must.include = c("participants", "contacts"))
assert_data_frame(x$participants)
assert_data_frame(x$contacts)
assert_list(x$reference, names = "named", null.ok = TRUE)
assert_character(id.column)
assert_character(year.column, null.ok = TRUE)
assert_character(country.column, null.ok = TRUE)
assert_names(colnames(x$participants), must.include = id.column)
assert_names(colnames(x$contacts), must.include = id.column)
assert_class(x$reference, "bibentry", null.ok = TRUE)

setnames(x$participants, id.column, "part_id")
setnames(x$contacts, id.column, "part_id")
Expand Down
15 changes: 5 additions & 10 deletions R/cite.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@
#' print(citation, style = "bibtex")
#' @export
get_citation <- function(x) {
survey <- get_survey(x)
if (is.null(x$reference)) stop("No citation defined for ", ifelse(is.null(x$name), "survey", x$name))
assert_class(x, "contact_survey", null.ok = TRUE)

ref <-
c(
list(header = gettextf("To cite %s in publications use:", x$ref$title)),
x$reference
)
if (is.null(x$reference)) {
stop("No citation defined")
}

bref <- do.call(bibentry, ref)

return(bref)
return(x$reference)
}
3 changes: 2 additions & 1 deletion R/load_survey.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#' @param ... options for [clean()], which is called at the end of this
#' @autoglobal
#' @importFrom jsonlite fromJSON
#' @importFrom data.table rbindlist
#' @examples
#' \dontrun{
#' list_surveys()
Expand All @@ -25,7 +26,7 @@ load_survey <- function(files, ...) {
survey_files <- grep("csv$", files, value = TRUE) # select csv files
reference_file <- grep("json$", files, value = TRUE) # select json file
if (length(reference_file) > 0) {
reference <- fromJSON(reference_file)
reference <- do.call(bibentry, fromJSON(reference_file))
} else {
reference <- NULL
}
Expand Down

0 comments on commit 4137e98

Please sign in to comment.