Skip to content

Commit

Permalink
CRAN compabtible version
Browse files Browse the repository at this point in the history
  • Loading branch information
ake123 committed Jan 17, 2025
1 parent 6a69973 commit a3d4814
Show file tree
Hide file tree
Showing 25 changed files with 118 additions and 514 deletions.
12 changes: 3 additions & 9 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: finna
Title: Title: R Package for Finna API
Title: Title: Access the 'Finna' API
Version: 0.1.1
Date: 2025-01-10
Authors@R: c(
Expand All @@ -9,7 +9,7 @@ Authors@R: c(
comment = c(ORCID = "0000-0001-5537-637X"))
)
Maintainer: Akewak Jeba <[email protected]>
Description: Provides functions to access and retrieve metadata from the Finna API, which aggregates content from Finnish archives, libraries, and museums.
Description: Provides functions to access and retrieve metadata from the 'Finna' API <https://api.finna.fi/>, which aggregates content from Finnish archives, libraries, and museums.
License: BSD_2_clause + file LICENSE
Encoding: UTF-8
Imports:
Expand All @@ -22,14 +22,8 @@ Imports:
readr,
tibble,
curl,
rlang,
stringr,
tidyr,
progress,
purrr,
tm,
stopwords,
wordcloud2
purrr
Suggests:
testthat (>= 3.0.0),
rmarkdown,
Expand Down
8 changes: 6 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ export(load_offline_data)
export(refine_metadata)
export(save_for_offline)
export(search_finna)
export(search_finna_from_file)
export(search_publisher)
export(timeline_relative)
export(top_plot)
import(dplyr)
import(progress)
Expand All @@ -32,10 +30,16 @@ importFrom(dplyr,mutate)
importFrom(dplyr,select)
importFrom(dplyr,summarise)
importFrom(ggplot2,aes)
importFrom(ggplot2,coord_flip)
importFrom(ggplot2,geom_bar)
importFrom(ggplot2,geom_line)
importFrom(ggplot2,geom_text)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,labs)
importFrom(ggplot2,scale_y_log10)
importFrom(ggplot2,theme_minimal)
importFrom(ggplot2,xlab)
importFrom(ggplot2,ylab)
importFrom(glue,glue)
importFrom(httr,GET)
importFrom(httr,content)
Expand Down
27 changes: 8 additions & 19 deletions R/enrich_author_name.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#' @title Enrich Author Name from Finna API and Save Results
#' @title Enrich Author Name from 'Finna' API and Save Results
#'
#' @description
#' This function reads a CSV file from a URL containing Melinda IDs and author names.
#' If the author name is missing (NA), it searches Finna for the corresponding Melinda ID
#' If the author name is missing (NA), it searches the 'Finna' API for the corresponding Melinda ID
#' to retrieve and update the author name. The updated data is saved in a CSV file.
#'
#' @param url A character string specifying the URL of the CSV file with Melinda IDs and author names.
#' @param output_file A character string specifying the output CSV file name.
#' @return A tibble of the updated data, and the file is saved to the `data` directory.
#' @return A tibble with updated author names. The file is saved to a temporary directory using \code{tempdir()}.
#' @importFrom readr read_csv write_csv cols col_character
#' @importFrom dplyr mutate if_else
#' @importFrom purrr map_chr
#' @export
#' @examples
#' \dontrun{
#' enrich_author_name(url = "https://example/na_author_rows.csv",
#' output_file = "updated_na_author_rows.csv")
#' output_file = "updated_na_author_rows.csv")
#' }
enrich_author_name <- function(url, output_file = "updated_na_author_rows.csv") {

Expand All @@ -38,10 +38,6 @@ enrich_author_name <- function(url, output_file = "updated_na_author_rows.csv")
if (!is.null(results) && nrow(results) > 0) {
authors <- results$Author[1]
return(authors)
} else if (httr::status_code(response) == 429) {
message("Rate limit reached. Waiting for ", wait_time, " seconds before retrying.")
Sys.sleep(wait_time)
attempt <- attempt + 1
} else {
return(NA)
}
Expand All @@ -55,7 +51,7 @@ enrich_author_name <- function(url, output_file = "updated_na_author_rows.csv")
author_name = readr::col_character()
))

# Step 2: Update `author_name` if it is 'NA' by fetching from Finna
# Step 2: Update `author_name` if it is 'NA' by fetching from 'Finna'
data <- data %>%
dplyr::mutate(
updated_author_name = dplyr::if_else(
Expand All @@ -70,17 +66,10 @@ enrich_author_name <- function(url, output_file = "updated_na_author_rows.csv")
)
)

# Ensure the 'data' directory exists
if (!dir.exists("data")) {
dir.create("data")
}

# Define output file path
output_csv_path <- file.path("data", output_file)

# Step 3: Save the updated data as CSV
# Step 3: Save the updated data as CSV in a temporary directory
output_csv_path <- file.path(tempdir(), output_file)
readr::write_csv(data, output_csv_path)
message("CSV file with updated author names saved to ", output_csv_path)

return(data) # Return the updated tibble as well
return(data) # Return the updated tibble
}
1 change: 1 addition & 0 deletions R/finna_cite.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#' @param result The Finna collection result as a tibble.
#' @param index The index of the collection to cite (numeric).
#' @param style The citation style to use (default: "citation"). See \code{\link[utils]{bibentry}}.
#' @return A bibliographic entry (\code{bibentry}) printed in the specified style.
#' @export
finna_cite <- function(result, index, style = "citation") {
# Validate the input structure
Expand Down
16 changes: 12 additions & 4 deletions R/refine_metadata.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
#' @title Refine Finna Metadata
#'
#' @description
#' Refines the Finna metadata tibble by keeping relevant fields and cleaning up missing values.
#' The purpose of the refine_metadata function is to: Ensure completeness by filling in missing
#' values with placeholder text, Standardize key metadata fields for easier analysis, Select only
#' the most relevant fields, simplifying the dataset.
#' The `refine_metadata` function cleans and standardizes Finna metadata by:
#' - **Validating Required Fields:** Checks for the presence of key metadata fields and returns `NULL` if any are missing.
#' - **Handling Missing Values:** Replaces `NA` values in critical fields with descriptive placeholder text (e.g., "Unknown Title").
#' - **Selecting Relevant Fields:** Keeps only the following fields for streamlined analysis:
#' - `Title`: The title of the resource.
#' - `Author`: The creator or author of the resource.
#' - `Year`: The publication or release year.
#' - `Language`: The language of the resource.
#' - `Formats`: The format(s) of the resource (e.g., Book, Audio).
#' - `Subjects`: The subject keywords or classifications.
#' - `Library`: The owning library or institution.
#' - `Series`: The series or collection the resource belongs to.
#'
#' @param data A tibble containing raw Finna metadata.
#' @return A tibble with selected, cleaned metadata fields, or NULL if required fields are missing.
Expand Down
37 changes: 14 additions & 23 deletions R/save_for_offline.R
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
#' @title Save Finna Search Results for Offline Access
#' @title Save 'Finna' Search Results for Offline Access
#'
#' @description
#' This function saves Finna search results and metadata locally to a file in `.rds` format,
#' This function saves 'Finna' search results and metadata locally to a file in `.rds` format,
#' allowing users to access and analyze the data offline without an internet connection.
#'
#' @param data A tibble or data frame containing the Finna search results.
#' @param data A tibble or data frame containing the 'Finna' search results.
#' @param file_name A string representing the name of the file to save.
#' The function automatically appends ".rds" to the name if not already included.
#' @return A message confirming that the data has been saved successfully.
#' @return No return value. Called for its side effects of saving the data to a file.
#' @examples
#' \dontrun{
#' search_results <- search_finna("sibelius")
#' save_for_offline(search_results, "sibelius_search_results")
#' }
#' @export
save_for_offline <- function(data, file_name = "offline_search_results") {
# Ensure the data directory exists
dir.create("data", showWarnings = FALSE)

# Ensure the file name has the .rds extension
if (!grepl("\\.rds$", file_name)) {
file_name <- paste0(file_name, ".rds")
}

# Define the full file path
full_path <- file.path("data", file_name)
# Define the full file path in a temporary directory
full_path <- file.path(tempdir(), file_name)

if (!is.null(data) && nrow(data) > 0) {
saveRDS(data, full_path)
Expand All @@ -34,28 +31,30 @@ save_for_offline <- function(data, file_name = "offline_search_results") {
}
}

#' Load Finna Search Results from Offline File
#' @title Load 'Finna' Search Results from Offline File
#'
#' @description
#' This function loads previously saved Finna search results from a local `.rds` file for offline access.
#' This function loads previously saved 'Finna' search results from a local `.rds` file for offline access.
#'
#' @param file_name A string representing the name of the file to load.
#' The function automatically appends ".rds" if not already included.
#' @return The loaded search results in tibble format.
#' @export
#' @return A tibble or data frame containing the loaded search results.
#' @examples
#' \dontrun{
#' search_results <- search_finna("sibelius")
#' save_for_offline(search_results, "sibelius_search_results")
#' offline_data <- load_offline_data("sibelius_search_results")
#' print(offline_data)
#' }
#' @export
load_offline_data <- function(file_name = "offline_search_results") {
# Ensure the file name has the .rds extension
if (!grepl("\\.rds$", file_name)) {
file_name <- paste0(file_name, ".rds")
}

# Define the full file path
full_path <- file.path("data", file_name)
# Define the full file path in the temporary directory
full_path <- file.path(tempdir(), file_name)

if (file.exists(full_path)) {
data <- readRDS(full_path)
Expand All @@ -65,11 +64,3 @@ load_offline_data <- function(file_name = "offline_search_results") {
stop("File not found. Please ensure the file exists and try again.")
}
}

# Example Usage:
# search_results <- search_finna("sibelius")
# save_for_offline(search_results, "sibelius_search_results") # Saves as "data/sibelius_search_results.rds"

# Load the search results back later
# offline_results <- load_offline_data("sibelius_search_results")
# print(offline_results)
67 changes: 0 additions & 67 deletions R/search_finna_from_file.R

This file was deleted.

39 changes: 0 additions & 39 deletions R/timeline_relative.R

This file was deleted.

1 change: 1 addition & 0 deletions R/top_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#' @param show.percentage Show the proportion of each category with respect to the total sample count.
#' @param log10 Show the counts on log10 scale (default FALSE)
#' @return ggplot object
#' @importFrom ggplot2 geom_bar coord_flip xlab ylab geom_text scale_y_log10
#' @export
#' @author Leo Lahti \email{leo.lahti@@iki.fi}
#' @references See citation("bibliographica")
Expand Down
Empty file added man/.Rapp.history
Empty file.
8 changes: 4 additions & 4 deletions man/enrich_author_name.Rd

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

3 changes: 3 additions & 0 deletions man/finna_cite.Rd

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

Loading

0 comments on commit a3d4814

Please sign in to comment.