Skip to content

Commit

Permalink
some modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
ake123 committed Oct 9, 2024
1 parent 013a071 commit f4fc85e
Show file tree
Hide file tree
Showing 27 changed files with 1,007 additions and 158 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ data/
.Rhistory
docs
inst/doc
doc
docs/
Meta
Meta/

11 changes: 11 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
# Generated by roxygen2: do not edit by hand

export(analyze_metadata)
export(check_api_access)
export(get_finna_records)
export(integrate_metadata)
export(refine_metadata)
export(search_finna)
export(visualize_author_distribution)
export(visualize_format_distribution)
export(visualize_library_distribution)
export(visualize_subject_distribution)
export(visualize_top_20_titles)
export(visualize_year_distribution)
import(dplyr)
import(ggplot2)
importFrom(curl,curl_download)
importFrom(httr,GET)
importFrom(httr,content)
Expand Down
32 changes: 32 additions & 0 deletions R/analysisFinna.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#' Analyze Refined Finna Metadata
#'
#' Performs basic analysis on Finna metadata, summarizing the distribution of formats, years, and authors.
#'
#' @param metadata A tibble containing refined Finna metadata.
#' @return A list of tibbles with summaries of formats, years, and authors.
#' @import dplyr
#' @export
#' @examples
#' library(finna)
#' sibelius_data <- search_finna("sibelius")
#' refined_data <- refine_metadata(sibelius_data)
#' analyze_metadata(refined_data)
#'
analyze_metadata <- function(metadata) {
format_distribution <- metadata %>%
count(Formats, sort = TRUE)

year_distribution <- metadata %>%
filter(!is.na(Year)) %>%
count(Year, sort = TRUE)

author_distribution <- metadata %>%
filter(!is.na(Author)) %>%
count(Author, sort = TRUE)

list(
format_distribution = format_distribution,
year_distribution = year_distribution,
author_distribution = author_distribution
)
}
27 changes: 17 additions & 10 deletions R/check_api_access.R
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
#' @title Check access to https://api.finna.fi/api/v1/?openapi
#' @description Check if R has access to resources at https://api.finna.fi/api/v1/?openapi
#' @description Check if R has access to resources at https://api.finna.fi/api/v1/?openapi.
#' This function tests whether R can successfully connect to the Finna API by attempting to download from the API's OpenAPI specification.
#' @export
#' @return a logical.
#' @return a logical indicating if the API is accessible (`TRUE`) or not (`FALSE`).
#'
#' @importFrom httr status_code
#' @importFrom curl curl_download
#'
#' @examples
#' \dontrun{
#' check_api_access()
#' }

check_api_access <- function(){
#' \dontrun{
#' # Check if the API is accessible
#' access <- check_api_access()
#' if (access) {
#' message("Finna API is accessible")
#' } else {
#' message("Finna API is not accessible")
#' }
#' }

check_api_access <- function() {
temp <- tempfile()
http_url <- "https://api.finna.fi/api/v1/?openapi"

suppressWarnings(
try(
curl::curl_download(http_url, temp, quiet = TRUE),
silent = TRUE)
silent = TRUE
)
)

if (is.na(file.info(temp)$size)) {
FALSE
}
else{
} else {
TRUE
}
}
28 changes: 28 additions & 0 deletions R/integrateFinna.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#' Integrate Finna Metadata with Another Dataset
#'
#' Merges Finna metadata with another dataset using a common key (e.g., "Title").
#'
#' @param metadata1 A tibble containing refined Finna metadata.
#' @param metadata2 A tibble containing another dataset to merge with the Finna metadata.
#' @param key A string specifying the key to join by. Defaults to "Title".
#' @return A tibble containing the merged dataset.
#' @import dplyr
#' @export
#' @examples
#' library(dplyr)
#' finna_data <- search_finna("sibelius")
#'
#' # Example other dataset to merge with
#' other_data <- tibble::tibble(
#' Title = c("Sibelius Symphony No. 5", "Finlandia", "Valse Triste"),
#' Rating = c(5, 4, 3)
#' )
#'
#' # Integrate the two datasets by "Title"
#' integrated_data <- integrate_metadata(finna_data, other_data, key = "Title")
#' print(integrated_data)
integrate_metadata <- function(metadata1, metadata2, key = "Title") {
# Perform a full join based on the specified key
integrated <- full_join(metadata1, metadata2, by = key)
return(integrated)
}
29 changes: 29 additions & 0 deletions R/refinemetaData.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#' Refine Finna Metadata
#'
#' Refines the Finna metadata tibble by keeping relevant fields and cleaning up missing values.
#'
#' @param data A tibble containing raw Finna metadata.
#' @return A tibble with selected, cleaned metadata fields.
#' @import dplyr
#' @export
#' @examples
#' library(finna)
#' sibelius_data <- search_finna("sibelius")
#' refine_metadata(sibelius_data)
#'
refine_metadata <- function(data) {
refined <- data %>%
mutate(
Title = if_else(is.na(Title), "Unknown Title", Title),
Author = if_else(is.na(Author), "Unknown Author", Author),
Year = if_else(is.na(Year), "Unknown Year", Year),
Language = if_else(is.na(Language), "Unknown Language", Language),
Formats = if_else(is.na(Formats), "Unknown Format", Formats),
Subjects = if_else(is.na(Subjects), "Unknown Subjects", Subjects),
Library = if_else(is.na(Library), "Unknown Library", Library),
Series = if_else(is.na(Series), "Unknown Series", Series)
) %>%
select(Title, Author, Year, Language, Formats, Subjects, Library, Series)

return(refined)
}
Loading

0 comments on commit f4fc85e

Please sign in to comment.