-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d568801
commit b021277
Showing
20 changed files
with
1,421 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
linters: linters_with_defaults() # see vignette("lintr") | ||
encoding: "UTF-8" | ||
exclusions: list("data-raw") | ||
exclusions: list( | ||
"data-raw", | ||
"tests/testthat/test_ci/test-full_cff.R", | ||
"vignettes/cffr.Rmd", | ||
"vignettes/bibtex_cff.Rmd" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#' Print Values | ||
#' | ||
#' @noRd | ||
#' @export | ||
print.cff <- function(x, ...) { | ||
cat(yaml::as.yaml(x)) | ||
} | ||
|
||
#' Combine Values into a Vector or List | ||
#' | ||
#' @source | ||
#' Based on `?c.person` \CRANpkg{utils}. | ||
#' | ||
#' <https://github.com/wch/r-source/blob/trunk/src/library/utils/R/citation.R> | ||
#' | ||
#' @noRd | ||
#' @export | ||
c.cff <- | ||
function(..., recursive = FALSE) { | ||
args <- list(...) | ||
args <- lapply(args, unclass) | ||
rval <- do.call("c", args) | ||
class(rval) <- "cff" | ||
rval | ||
} | ||
|
||
|
||
#' Coerce to a Data Frame | ||
#' | ||
#' @noRd | ||
#' @export | ||
as.data.frame.cff <- function(x, row.names = NULL, optional = FALSE, ...) { | ||
Check warning on line 32 in R/cff-methods.R GitHub Actions / Run lintr scanning
Check notice Code scanning / lintr Variable and function name style should match snake_case or symbols. Note
Variable and function name style should match snake_case or symbols.
|
||
# If the cff is unnamed is a list of persons/references | ||
if (is.null(names(x))) { | ||
the_df <- cff_list_to_df(x) | ||
} else { | ||
the_df <- cff_to_df(x) | ||
} | ||
|
||
the_df <- as.data.frame(the_df, | ||
row.names = row.names, optional = optional, | ||
... | ||
) | ||
|
||
return(the_df) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
# Utils for df ---- | ||
unnamed_to_df <- function(key, nm) { | ||
key_l <- as.integer(lengths(key)) | ||
m <- matrix(unlist(key), nrow = 1) | ||
df <- as.data.frame(m) | ||
names(df) <- paste0(nm, ".", sprintf("%02d", seq_len(key_l) - 1)) | ||
return(df) | ||
} | ||
|
||
named_to_df <- function(key, nm) { | ||
key_un <- unlist(key) | ||
|
||
|
||
m <- matrix(as.character(key_un), nrow = 1) | ||
df <- as.data.frame(m) | ||
names(df) <- names(key_un) | ||
return(df) | ||
} | ||
|
||
nested_named_to_df <- function(key, nm) { | ||
key_unlist <- key[[1]] | ||
key_len <- seq_len(length(key_unlist)) | ||
|
||
df_l_type3 <- lapply(key_len, function(z) { | ||
df <- cff_to_df(key_unlist[[z]]) | ||
|
||
# Prepend names | ||
names(df) <- paste0(nm, ".", sprintf("%02d", z - 1), ".", names(df)) | ||
return(df) | ||
}) | ||
|
||
df_list_to_df(df_l_type3) | ||
} | ||
|
||
prefcit_to_df <- function(key, nm = "preferred_citation.") { | ||
key_df <- cff_to_df(key[[1]]) | ||
names(key_df) <- paste0(nm, names(key_df)) | ||
return(key_df) | ||
} | ||
|
||
reflist_to_df <- function(key, nm) { | ||
key_unlist <- key[[1]] | ||
key_len <- seq_len(length(key_unlist)) | ||
|
||
prefix_key <- paste0(nm, ".", sprintf("%02d", key_len - 1), ".") | ||
|
||
df_l <- lapply(key_len, function(y) { | ||
key_l <- key_unlist[y] | ||
nm_pref <- prefix_key[y] | ||
|
||
dff <- prefcit_to_df(key_l, nm_pref) | ||
|
||
dff | ||
}) | ||
|
||
final_df <- df_list_to_df(df_l) | ||
|
||
final_df | ||
} | ||
|
||
df_list_to_df <- function(x) { | ||
# Clean NULL | ||
df_l_clean <- x[!vapply(x, is.null, logical(1))] | ||
|
||
final_df <- do.call(cbind, df_l_clean) | ||
return(final_df) | ||
} | ||
cff_to_df <- function(x) { | ||
# CFF has different models | ||
# type 1: unnamed arrays | ||
unnamed_array <- c("keywords", "languages", "patent-states") | ||
|
||
|
||
# type 2: named arrays | ||
named_array <- c( | ||
"conference", "database-provider", "institution", | ||
"location", "publisher" | ||
) | ||
|
||
|
||
# type 3: nested named arrays | ||
nested_named_array <- c( | ||
"authors", "contact", "editors", "editors-series", | ||
"recipients", "senders", "translators", "identifiers" | ||
) | ||
|
||
nms <- names(x) | ||
x_len <- seq_len(length(x)) | ||
|
||
|
||
df_l <- lapply(x_len, function(y) { | ||
nm <- nms[y] | ||
|
||
if (nm %in% unnamed_array) { | ||
return(unnamed_to_df(x[y], nm)) | ||
} | ||
if (nm %in% named_array) { | ||
return(named_to_df(x[y], nm)) | ||
} | ||
if (nm %in% nested_named_array) { | ||
return(nested_named_to_df(x[y], nm)) | ||
} | ||
if (nm == "preferred-citation") { | ||
return(prefcit_to_df(x[y])) | ||
} | ||
|
||
if (nm == "references") { | ||
return(reflist_to_df(x[y], nm)) | ||
} | ||
|
||
the_df <- as.data.frame(x[[y]]) | ||
names(the_df) <- gsub("-", "_", nm) | ||
return(the_df) | ||
}) | ||
|
||
final_df <- df_list_to_df(df_l) | ||
|
||
return(final_df) | ||
} | ||
|
||
cff_list_to_df <- function(x) { | ||
# Applicable to lists of persons or references | ||
# Guess type | ||
if (!"type" %in% names(x[[1]])) { | ||
guess <- "person" | ||
} else { | ||
guess <- "reference" | ||
} | ||
|
||
|
||
x_len <- seq_len(length(x)) | ||
df_l <- lapply(x_len, function(y) { | ||
df <- as.data.frame(x[y]) | ||
newnames <- paste0(guess, ".", sprintf("%02d", y - 1), ".", names(df)) | ||
names(df) <- newnames | ||
|
||
df | ||
}) | ||
|
||
df_end <- df_list_to_df(df_l) | ||
df_end | ||
} |
Oops, something went wrong.