From 642223b24e994a179f9ec7603d1f5ca31d91aeef Mon Sep 17 00:00:00 2001 From: Lisa Steinmann Date: Sun, 1 Sep 2024 13:33:04 +0200 Subject: [PATCH] add basic query function for chronontology --- DESCRIPTION | 5 +++-- NAMESPACE | 1 + R/query_chronontology.R | 35 +++++++++++++++++++++++++++++++++++ README.md | 3 +++ man/query_chronontology.Rd | 21 +++++++++++++++++++++ 5 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 R/query_chronontology.R create mode 100644 man/query_chronontology.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 21a6bb3..535dd0d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -16,7 +16,8 @@ Description: This package provides convenience functions that are helpful License: GPL (>= 3) Depends: R (>= 3.3), - jsonlite + jsonlite, + crul Suggests: covr, dplyr, @@ -27,5 +28,5 @@ Config/testthat/edition: 3 Encoding: UTF-8 LazyData: true Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 VignetteBuilder: knitr diff --git a/NAMESPACE b/NAMESPACE index 399dfbb..f9cd029 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,6 +6,7 @@ export(duplicate_by) export(from_chronontology) export(group_periods) export(make_chrongler_conc) +export(query_chronontology) export(ungroup_periods) importFrom(jsonlite,fromJSON) importFrom(utils,read.csv) diff --git a/R/query_chronontology.R b/R/query_chronontology.R new file mode 100644 index 0000000..4be45c3 --- /dev/null +++ b/R/query_chronontology.R @@ -0,0 +1,35 @@ +#' Basic Query for iDAI.chronontology +#' +#' +#' +#' @param value (chr) The value you want to search for in all chronontology-periods. +#' +#' @return A list of results as handed out by iDAI.chronontology. +#' +#' @export +#' +#' @examples +#' roman_periods <- query_chronontology(value = "roman") +#' names <- unlist(lapply(roman_periods, function(x) x$resource$names$en)) +query_chronontology <- function(value = "roman") { + stopifnot(is.character(value)) + + url <- "https://chronontology.dainst.org" + headers <- list(`Content-Type` = "application/json", + Accept = "application/json") + ChronClient <- crul::HttpClient$new(url = url, headers = headers) + + response <- ChronClient$get(path = paste0('data/period/?q=', value, '&size=1')) + response <- response$parse("UTF-8") + list <- jsonlite::fromJSON(response, FALSE) + + if (list$total > length(list$results)) { + response <- ChronClient$get(path = paste0('data/', type, '/?q=', value, '&size=', list$total)) + response <- response$parse("UTF-8") + list <- jsonlite::fromJSON(response, FALSE) + } + + result <- list$results + + return(result) +} diff --git a/README.md b/README.md index b4590ae..6de5e42 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,11 @@ # chrongler wrangles categorical chronological data + This package provides convenience functions that are helpful for working with archaeological or historical data, where chronological information is stored as a 'period' (categorical values that may indicate chronology in one way or another). The functions are purely for formatting and 'wrangling' such data, and do not contain any means of analysis or further processing. # Installation + chrongler is not on CRAN. You can install the current version from GitHub using: ``` r remotes::install_github("lsteinmann/chrongler", build_vignettes = TRUE) @@ -23,6 +25,7 @@ Using chrongler (and a concordance of periods and their grouping and absolute da * download the data for one period from [iDAI.chronontology](https://chronontology.dainst.org/) (`from_chronontology()`, it's a bit of a work in progress...) The inaccurately named "example_workflow"-vignette explains the functions and especially setting up the concordance in detail, see: + ``` r browseVignettes("chrongler") ``` diff --git a/man/query_chronontology.Rd b/man/query_chronontology.Rd new file mode 100644 index 0000000..f7dce51 --- /dev/null +++ b/man/query_chronontology.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/query_chronontology.R +\name{query_chronontology} +\alias{query_chronontology} +\title{Basic Query for iDAI.chronontology} +\usage{ +query_chronontology(value = "roman") +} +\arguments{ +\item{value}{(chr) The value you want to search for in all chronontology-periods.} +} +\value{ +A list of results as handed out by iDAI.chronontology. +} +\description{ +Basic Query for iDAI.chronontology +} +\examples{ +roman_periods <- query_chronontology(value = "roman") +names <- unlist(lapply(roman_periods, function(x) x$resource$names$en)) +}