Skip to content

Commit

Permalink
adding timeline function
Browse files Browse the repository at this point in the history
  • Loading branch information
ake123 committed Jan 22, 2025
1 parent a3d4814 commit 35b0a55
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 1 deletion.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: finna
Title: Title: Access the 'Finna' API
Title: Access the 'Finna' API
Version: 0.1.1
Date: 2025-01-10
Authors@R: c(
Expand All @@ -14,6 +14,8 @@ License: BSD_2_clause + file LICENSE
Encoding: UTF-8
Imports:
dplyr,
tidyr,
reshape2,
glue,
httr,
xml2,
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export(refine_metadata)
export(save_for_offline)
export(search_finna)
export(search_publisher)
export(timeline)
export(top_plot)
import(dplyr)
import(progress)
Expand Down Expand Up @@ -50,10 +51,12 @@ importFrom(readr,col_character)
importFrom(readr,cols)
importFrom(readr,read_csv)
importFrom(readr,write_csv)
importFrom(reshape2,melt)
importFrom(stats,reorder)
importFrom(stats,setNames)
importFrom(tibble,as_tibble)
importFrom(tibble,tibble)
importFrom(tidyr,pivot_wider)
importFrom(utils,URLdecode)
importFrom(utils,menu)
importFrom(xml2,read_xml)
Expand Down
75 changes: 75 additions & 0 deletions R/timeline.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#' @title Retrieve Timeline
#' @description Timeline data for selected variable (possibly across various groups).
#' @param x data frame
#' @param field Numeric field to summarize in the timeline. The number of entries (title count) per decade is used by default. If this argument is used, the sum of entries per decade for this field is given.
#' @param group Optional. Name for a data field that indicates groups to compare.
#' @param nmin Include only entries with at least nmin absolute frequency
#' @param mode "absolute" or "relative"
#' @param time.window Time window for the timeline in years. Default: 10 (publication decade).
#' @param time.field Specify the field to be used for time. By default: "Year", or if time.window is 10, then "publication_decade"
#' @return data.frame
#' @importFrom tidyr pivot_wider
#' @importFrom reshape2 melt
#' @export
#' @author Leo Lahti \email{leo.lahti@@iki.fi}
#' @references See citation("bibliographica")
#' @examples \dontrun{timeline(df, "gatherings")}
#' @keywords utilities
timeline <- function (x, field = "titlecount", group = NULL, nmin = 0, mode = "absolute", time.window = 10, time.field = "Year") {

publication_decade <- publication_time <- NULL

x$publication_time <- x[[time.field]]

# Set the desired time window (default one decade)
x$publication_time <- time.window * floor(x$publication_time / time.window)


if (time.field == "publication_decade" || (time.field == "Year" & time.window == 10 & "publication_decade" %in% names(x))) {
x$publication_time <- x$publication_decade
}

if (!is.null(group)) {
x <- x[, c("publication_time", group)]
x$group <- x[[group]]
} else {
x$group <- rep(1, nrow(x))
}

if (is.null(field)) {
field <- "titlecount"
}

if (field == "titlecount" && !field %in% names(x)) {
x[[field]] <- rep(1, nrow(x))
}

x$field <- x[[field]]
#print(x)

df2 <- x %>% filter(!is.na(group)) %>%
group_by(publication_time, group) %>%
summarise(absolute = sum(field, na.rm = TRUE))

# Remove entries with too few occurrences
df2 <- df2 %>% filter(!is.na(publication_time) &
group %in% setdiff(unique(as.character(unname(unlist(df2[which(df2$absolute >= nmin), "group"])))), "NA"))
df2$group <- factor(df2$group)
df2$group <- droplevels(df2$group)

# Add relatives
df3 <- pivot_wider(df2, names_from = "publication_time", values_from = "absolute", values_fill = 0)
df3 <- as.data.frame(df3)
df3[, -1] = 100 * apply(df3[, -1], 2, function (x) {x/sum(x, na.rm = TRUE)})
df3 <- melt(as.data.frame(df3), "group")
colnames(df3) <- c("group", "publication_time", "relative")
df3$publication_time <- as.numeric(as.character(df3$publication_time))
df3 <- df3[, c("publication_time", "group", "relative")]

# Combine counts and relatives
dfs <- dplyr::full_join(df2, df3)
dfs$mode <- dfs[[mode]]

return(dfs)

}
47 changes: 47 additions & 0 deletions man/timeline.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 35b0a55

Please sign in to comment.