Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new functions #21

Merged
merged 18 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ Imports:
curl,
jsonlite,
lubridate,
RefManageR
RefManageR,
digest,
frictionless
Suggests:
devtools,
ggplot2,
Expand Down
11 changes: 11 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@

export(GetDataSotkanet)
export(SotkanetCite)
export(SotkanetCleanCache)
export(SotkanetIndicatorMetadata)
export(SotkanetIndicators)
export(SotkanetInteractive)
export(SotkanetRegions)
export(get_sotkanet)
export(sotkanet.csv_query)
export(sotkanet.json_query)
export(sotkanet_read_cache)
export(sotkanet_write_cache)
export(test_connection)
export(try_GET)
export(write_frictionless_metadata)
importFrom(RefManageR,toBiblatex)
importFrom(curl,has_internet)
importFrom(digest,digest)
importFrom(frictionless,add_resource)
importFrom(frictionless,create_package)
importFrom(httr,GET)
importFrom(httr,build_url)
importFrom(httr,content)
Expand All @@ -22,6 +31,8 @@ importFrom(httr,user_agent)
importFrom(jsonlite,fromJSON)
importFrom(lubridate,year)
importFrom(lubridate,ymd)
importFrom(utils,capture.output)
importFrom(utils,menu)
importFrom(utils,person)
importFrom(utils,read.csv2)
importFrom(utils,toBibtex)
91 changes: 45 additions & 46 deletions R/GetDataSotkanet.R
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
#' @title Retrieve Sotkanet Data
#' @description
#' @description
#' Retrieve selected data and combine into a single table.
#' @details
#' THL's open data license and limitation of liability
#' @details
#' THL's open data license and limitation of liability
#'
#' \strong{License}
#'
#' The open data provided by National Institute for Health and Welfare is
#' licensed under CC BY 4.0. This license defines how open data can be utilized.
#'
#' The open data provided by National Institute for Health and Welfare is
#' licensed under CC BY 4.0. This license defines how open data can be utilized.
#' The licensing is based on a decision made by the Director General.
#'
#'
#' \strong{Limitation of Liability}
#'
#' National Institute for Health and Welfare shall not be liable for any loss,
#' legal proceedings, claims, proceedings, demands, costs or damages regardless
#' of their cause or form, which can be directly or indirectly connected to
#' open data or use of open data published by National Institute for Health and
#'
#' National Institute for Health and Welfare shall not be liable for any loss,
#' legal proceedings, claims, proceedings, demands, costs or damages regardless
#' of their cause or form, which can be directly or indirectly connected to
#' open data or use of open data published by National Institute for Health and
#' Welfare.
#' @param indicators Dataset identifier(s)
#' @param years vector of years, for example `2015:2018` or `c(2010, 2012, ...)`
#' @param years vector of years c(2010, 2012, ... )
#' @param genders vector of genders ('male' | 'female' | 'total')
#' @param regions filter by selected regions only (default: all regions)
#' @param region.category filter by one or more of the following 14 valid
#' @param region.category filter by one or more of the following 14 valid
#' regions categories (default: all categories)
#' \itemize{
#' \item "ALUEHALLINTOVIRASTO"
Expand All @@ -42,80 +42,80 @@
#' @param user.agent "User agent" defined by the user. Default is NULL which
#' will then use the package identifier "rOpenGov/sotkanet"
#' @return data.frame
#' @references See citation("sotkanet")
#' @references See citation("sotkanet")
#' @author Maintainer: Leo Lahti \email{leo.lahti@@iki.fi}, Pyry Kantanen
#' @examples \dontrun{dat <- GetDataSotkanet(indicators = 165)}
#' @seealso
#' @seealso
#' For more information about dataset structure, see THL webpage at
#' \url{https://yhteistyotilat.fi/wiki08/pages/viewpage.action?pageId=27557907}
#'
#'
#' THL open data license website: \url{https://yhteistyotilat.fi/wiki08/x/AAadAg}
#'
#'
#' @keywords utilities
#' @export
GetDataSotkanet <- function(indicators = NULL,
years = 1991:2015,
genders = c("total"),
regions = NULL,
GetDataSotkanet <- function(indicators = NULL,
years = 1991:2015,
genders = c("total"),
regions = NULL,
region.category = NULL,
user.agent = NULL) {

if (is.null(indicators)){
message("Parameter 'indicators' is NULL. Please provide at least one indicator.")
return(invisible(NULL))
}

# List all indicators in Sotkanet database
sotkanet_indicators <- SotkanetIndicators(id = indicators,
sotkanet_indicators <- SotkanetIndicators(id = indicators,
type = "table")
sotkanet_regions <- SotkanetRegions(type = "table")

dats <- list()

for (indicator in indicators) {
# Gather URL parts
# parsing the csv file is more straightforward in this context
sotkanet_url <- "https://sotkanet.fi/rest"
sotkanet_uri <- "/1.1/csv"
all_params <- c(indicator, years, genders)
names(all_params) <- c("indicator",
rep("years", length(years)),
rep("years", length(years)),
rep("genders", length(genders)))
all_params <- as.list(all_params)

# Construct URL
url_object <- httr::parse_url(sotkanet_url)
path <- paste(url_object$path, sotkanet_uri, sep = "")
url_object$path <- path
url_object$query <- all_params
final_url <- httr::build_url(url_object)

y <- sotkanet.csv_query(final_url, user.agent = user.agent)

if (is.null(y)){
message(paste(" There was a problem retrieving indicator from", indicator," "))
return(invisible(NULL))
}

dats[[as.character(indicator)]] <- y

}

# Merge all data from the different indicators in a single table
combined_data <- do.call("rbind", dats)

# Add region and indicator information
combined_data$indicator.title.fi <- sotkanet_indicators[match(combined_data$indicator,
combined_data$indicator.title.fi <- sotkanet_indicators[match(combined_data$indicator,
sotkanet_indicators$indicator), "indicator.title.fi"]
combined_data$region.title.fi <- sotkanet_regions[match(combined_data$region,
combined_data$region.title.fi <- sotkanet_regions[match(combined_data$region,
sotkanet_regions$region), "region.title.fi"]
combined_data$region.code <- sotkanet_regions[match(combined_data$region,
sotkanet_regions$region), "region.code"]
combined_data$region.category <- sotkanet_regions[match(combined_data$region,
sotkanet_regions$region), "region.category"]
combined_data$indicator.organization.title.fi <- sotkanet_indicators[match(combined_data$indicator,
combined_data$region.code <- sotkanet_regions[match(combined_data$region,
sotkanet_regions$region), "region.code"]
combined_data$region.category <- sotkanet_regions[match(combined_data$region,
sotkanet_regions$region), "region.category"]
combined_data$indicator.organization.title.fi <- sotkanet_indicators[match(combined_data$indicator,
sotkanet_indicators$indicator), "indicator.organization.title.fi"]

if (!is.null(regions)){
if (regions %in% unique(combined_data$region.title.fi)){
combined_data <- combined_data[which(combined_data$region.title.fi == regions),]
Expand All @@ -125,20 +125,19 @@ GetDataSotkanet <- function(indicators = NULL,
return(invisible(NULL))
}
}

if (!is.null(region.category)){
if (region.category %in% unique(combined_data$region.category)){
combined_data <- combined_data[which(combined_data$region.category == region.category),]
combined_data <- combined_data[which(combined_data$region.category == region.category),]
} else {
message(paste("Input for region.categories not found from dataset:", region.category, "\n",
message(paste("Input for region.categories not found from dataset:", region.category, "\n",
"Please check your parameter input for validity and correctness."))
return(invisible(NULL))
}
}

combined_data

}



12 changes: 7 additions & 5 deletions R/SotkanetCite.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,22 @@ SotkanetCite <- function(id,

format <- tolower(as.character(format))

if(!any(lang %in% c("en", "fi", "sv"))){
stop("The supported languages are English (en), Finnish (fi) and Swedish (sv).")
info <- SotkanetIndicatorMetadata(id)

if(is.null(info)){
stop("The id does not match with any of the datasets")
}

if(!any(id %in% SotkanetIndicators()$indicator)){
stop("The id does not match with any of the datasets.")
if(!any(lang %in% c("en", "fi", "sv"))){
stop("The supported languages are English (en), Finnish (fi) and Swedish (sv).")
}

if(!format %in% c("bibentry", "bibtex", "biblatex")){
warning("The ", format, " is not recognized, will return Biblatex as default.")
format <- "biblatex"
}

info <- SotkanetIndicatorMetadata(id)


urldate <- as.character(Sys.Date())

Expand Down
41 changes: 41 additions & 0 deletions R/SotkanetCleanCache.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#' @title Clean sotkanet Cache
#'
#' @description
#' Delete all .rds files from the sotkanet cache directory.
#'
#' @param cache_dir
#' A path to the cache directory. If `NULL` (default) tries to clean default
#' temporary cache directory.
#'
#' @examples
#' \dontrun{
#' SotkanetCleanCache()
#' }
#' @export
SotkanetCleanCache <- function(cache_dir = NULL){
pitkant marked this conversation as resolved.
Show resolved Hide resolved

if (is.null(cache_dir)){

cache_dir <- file.path(tempdir(), "sotkanet")

cache_dir <- path.expand(cache_dir)
}

if (!dir.exists(cache_dir)){
message("The cache directory does not exist.")
} else if (dir.exists(cache_dir)){
files <- list.files(cache_dir,
pattern = "rds",
full.names = TRUE
)
}

if (length(files) == 0) {
message("The cache folder ", cache_dir, " is empty.")
} else {
unlink(files)
message("Deleted .rds files from ", cache_dir)
}

invisible(TRUE)
}
Loading