From dd1dced6a46bdb42f997efeb45e404e18f10cab1 Mon Sep 17 00:00:00 2001 From: antaldaniel Date: Tue, 11 Jun 2024 19:14:42 +0200 Subject: [PATCH] 0.1.1 --- .github/.gitignore | 1 + .github/workflows/pkgdown.yaml | 48 +++++++++++ .github/workflows/test-coverage.yaml | 61 ++++++++++++++ DESCRIPTION | 11 ++- NAMESPACE | 4 + NEWS.md | 5 +- R/ttl_dataset_write.R | 53 +++++++++++++ R/ttl_prefix_write.R | 36 +++++++++ R/ttl_statements_write.R | 86 ++++++++++++++++++++ R/ttl_write.R | 66 ++++++++++++---- R/utils.R | 20 +++++ README.Rmd | 37 +++++---- README.md | 92 +++++++++++----------- _pkgdown.yml | 1 - inst/WORDLIST | 9 +++ man/dataset_ttl_write.Rd | 41 ++++++++++ man/getdata.Rd | 20 +++++ man/ttl_prefix_write.Rd | 30 +++++++ man/ttl_statements_write.Rd | 33 ++++++++ man/ttl_write.Rd | 16 ++-- man/validate_tdf.Rd | 12 +++ tests/spelling.R | 3 + tests/testthat/test-ttl_prefix_write.R | 27 +++++++ tests/testthat/test-ttl_statements_write.R | 21 +++++ tests/testthat/test-ttl_write.R | 27 +++++++ tests/testthat/test-utils.R | 16 ++++ tuRtle.Rproj | 2 + 27 files changed, 692 insertions(+), 86 deletions(-) create mode 100644 .github/.gitignore create mode 100644 .github/workflows/pkgdown.yaml create mode 100644 .github/workflows/test-coverage.yaml create mode 100644 R/ttl_dataset_write.R create mode 100644 R/ttl_prefix_write.R create mode 100644 R/ttl_statements_write.R create mode 100644 R/utils.R create mode 100644 inst/WORDLIST create mode 100644 man/dataset_ttl_write.Rd create mode 100644 man/getdata.Rd create mode 100644 man/ttl_prefix_write.Rd create mode 100644 man/ttl_statements_write.Rd create mode 100644 man/validate_tdf.Rd create mode 100644 tests/spelling.R create mode 100644 tests/testthat/test-ttl_prefix_write.R create mode 100644 tests/testthat/test-ttl_statements_write.R create mode 100644 tests/testthat/test-ttl_write.R create mode 100644 tests/testthat/test-utils.R diff --git a/.github/.gitignore b/.github/.gitignore new file mode 100644 index 0000000..2d19fc7 --- /dev/null +++ b/.github/.gitignore @@ -0,0 +1 @@ +*.html diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml new file mode 100644 index 0000000..ed7650c --- /dev/null +++ b/.github/workflows/pkgdown.yaml @@ -0,0 +1,48 @@ +# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help +on: + push: + branches: [main, master] + pull_request: + branches: [main, master] + release: + types: [published] + workflow_dispatch: + +name: pkgdown + +jobs: + pkgdown: + runs-on: ubuntu-latest + # Only restrict concurrency for non-PR jobs + concurrency: + group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }} + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + permissions: + contents: write + steps: + - uses: actions/checkout@v3 + + - uses: r-lib/actions/setup-pandoc@v2 + + - uses: r-lib/actions/setup-r@v2 + with: + use-public-rspm: true + + - uses: r-lib/actions/setup-r-dependencies@v2 + with: + extra-packages: any::pkgdown, local::. + needs: website + + - name: Build site + run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE) + shell: Rscript {0} + + - name: Deploy to GitHub pages 🚀 + if: github.event_name != 'pull_request' + uses: JamesIves/github-pages-deploy-action@v4.4.1 + with: + clean: false + branch: gh-pages + folder: docs diff --git a/.github/workflows/test-coverage.yaml b/.github/workflows/test-coverage.yaml new file mode 100644 index 0000000..fefc52e --- /dev/null +++ b/.github/workflows/test-coverage.yaml @@ -0,0 +1,61 @@ +# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples +# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help +on: + push: + branches: [main, master] + pull_request: + branches: [main, master] + +name: test-coverage + +permissions: read-all + +jobs: + test-coverage: + runs-on: ubuntu-latest + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + + steps: + - uses: actions/checkout@v4 + + - uses: r-lib/actions/setup-r@v2 + with: + use-public-rspm: true + + - uses: r-lib/actions/setup-r-dependencies@v2 + with: + extra-packages: any::covr, any::xml2 + needs: coverage + + - name: Test coverage + run: | + cov <- covr::package_coverage( + quiet = FALSE, + clean = FALSE, + install_path = file.path(normalizePath(Sys.getenv("RUNNER_TEMP"), winslash = "/"), "package") + ) + covr::to_cobertura(cov) + shell: Rscript {0} + + - uses: codecov/codecov-action@v4 + with: + fail_ci_if_error: ${{ github.event_name != 'pull_request' && true || false }} + file: ./cobertura.xml + plugin: noop + disable_search: true + token: ${{ secrets.CODECOV_TOKEN }} + + - name: Show testthat output + if: always() + run: | + ## -------------------------------------------------------------------- + find '${{ runner.temp }}/package' -name 'testthat.Rout*' -exec cat '{}' \; || true + shell: bash + + - name: Upload test results + if: failure() + uses: actions/upload-artifact@v4 + with: + name: coverage-test-failures + path: ${{ runner.temp }}/package diff --git a/DESCRIPTION b/DESCRIPTION index 8a130e5..b22b9fa 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: tuRtle Title: Parse and Export R Data with the Turtle Syntax for the Resource Description Framework. -Date: 2024-01-28 -Version: 0.1.0 +Date: 2024-06-11 +Version: 0.1.1 Authors@R: c(person(given = "Daniel", family = "Antal", email = "daniel.antal@dataobservatory.eu", @@ -17,11 +17,14 @@ BugReports: https://github.com/dataobservatory-eu/tuRtle/issues License: GPL (>= 3) Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.0 +RoxygenNote: 7.3.1 Imports: - dataset + dataset, + utils Suggests: knitr, remotes, + spelling, testthat (>= 3.0.0) Config/testthat/edition: 3 +Language: en-GB diff --git a/NAMESPACE b/NAMESPACE index cf8f962..ec714b1 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,3 +1,7 @@ # Generated by roxygen2: do not edit by hand +export(dataset_ttl_write) +export(ttl_prefix_write) +export(ttl_statements_write) export(ttl_write) +importFrom(utils,data) diff --git a/NEWS.md b/NEWS.md index 67595ee..2cecee0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,4 @@ -# tuRtle 0.1.0 +# tuRtle 0.1.1 -* Initial development version on Zenodo and GitHub. +* 2024-01-28. Initial development version 0.1.0 on Zenodo and GitHub. +* 2024-06-11. Version 0.1.11 has separate functions for writing a namespace prefix and statements, and has extensive unit tests. \ No newline at end of file diff --git a/R/ttl_dataset_write.R b/R/ttl_dataset_write.R new file mode 100644 index 0000000..17337cd --- /dev/null +++ b/R/ttl_dataset_write.R @@ -0,0 +1,53 @@ +#' @title Write a dataset into Turtle serialisation +#' @description +#' Write a dataset into a Turtle serialisation. +#' +#' @param tdf A dataset in exactly three columns. +#' @param ttl_namespace The namespace definitions of the dataset. +#' @param file_path The path to the file that should be written or appended. +#' @param overwrite If the file exists, overwrite it? Defaults to \code{TRUE}. +#' @return A text file with the prefix and the observation serialisations. +#' @examples +#' testtdf <- data.frame (s = c("eg:o1", "eg:01", "eg:02"), +#' p = c("a", "eg-var:", "eg-var"), +#' o = c("qb:Observation", +#' "\"1\"^^", +#' "\"2\"^^")) +#' +#' examplefile <- file.path(tempdir(), "ttl_dataset_write.ttl") +#' +#' dataset_ttl_write(tdf=testtdf, file_path = examplefile) +#' +#' readLines(examplefile) +#' @export + +dataset_ttl_write <- function(tdf, + ttl_namespace = NULL, + file_path = NULL, + overwrite = TRUE) { + + ## load dataset_namespace + default_namespace <- getdata("dataset_namespace", "dataset") + default_namespace <- default_namespace[ + which(default_namespace$prefix %in% c("rdf:", "rdfs:", "owl:", + "qb:", "dcat:", "xsd:")),] + + ## validate dataset + validate_tdf(tdf) + + ## use subject predicate, object names + names(tdf) <- c("s", "p", "o") + + if (is.null(file_path)) file_path <- file.path(tempdir(), "tmp.ttl") + + if (is.null(ttl_namespace)) { + ttl_namespace <- default_namespace + } + + ttl_prefix_write(ttl_namespace = ttl_namespace, + file_path=file_path, + overwrite=overwrite) + + invisible(ttl_observations_write(tdf, file_path)) +} + diff --git a/R/ttl_prefix_write.R b/R/ttl_prefix_write.R new file mode 100644 index 0000000..eac12cc --- /dev/null +++ b/R/ttl_prefix_write.R @@ -0,0 +1,36 @@ +#' @title Write TTL prefix +#' @description Write the namespace prefixes into the header of a Turtle serialisation +#' @param ttl_namespace A namespace definition as a two-column dataset. +#' @param file_path A file_path to write the TTL prefix namespace definitions. +#' @param overwrite Boolean, if the file should be overwritten (if it exists at all.) +#' Defaults to \code{TRUE}. +#' @export +#' @examples +#' ttl_namespace <- data.frame( +#' prefix = c("dbo:", "dcterms:"), +#' uri = c("", +#' "") +#' ) +#' file_path <- tempfile() +#' ttl_prefix_write(ttl_namespace, file_path) +#' readLines(file_path) +#' + +ttl_prefix_write <- function(ttl_namespace, file_path, overwrite=TRUE) { + if ( file.exists(file_path) & overwrite ) file.remove(file_path) + + create_string <- paste( + apply ( ttl_namespace, 1, function(x) { + prefix <- x[[1]] + n <- max(12-nchar(x[[1]]), 1) + separator <- paste(rep (" ", n), collapse="") + paste0("@prefix ", prefix, separator, x[[2]], " .") + }), sep = "", collapse = "\n" + ) + + if (overwrite) { + write(create_string, file=file_path, append = F ) + } else { + write(create_string, file=file_path, append = T ) + } +} diff --git a/R/ttl_statements_write.R b/R/ttl_statements_write.R new file mode 100644 index 0000000..1f46cf0 --- /dev/null +++ b/R/ttl_statements_write.R @@ -0,0 +1,86 @@ +#' @title Write statements into a TTL file +#' @param tdf A dataset in exactly three columns. +#' @param file_path The path to the file that should be written or appended. +#' @param overwrite If the file exists, overwrite it? Defaults to \code{TRUE}. +#' @examples +#' tdf <- data.frame (s = c("eg:01","eg:02", "eg:01", "eg:02", "eg:01" ), +#' p = c("a", "a", "eg-var:", "eg-var:", "rdfs:label"), +#' o = c("qb:Observation", +#' "qb:Observation", +#' "\"1\"^^", +#' "\"2\"^^", +#' '"Example observation"')) +#' +#' examplefile <- file.path(tempdir(), "ttl_dataset_write.ttl") +#' +#' dataset_ttl_write(tdf=tdf, file_path = examplefile) +#' +#' readLines(examplefile) +#' @export + +ttl_statements_write <- function(tdf, file_path, overwrite=TRUE) { + ## tdf must be s, o, p + validate_tdf(tdf) + + if (overwrite) { + reset_text_file(file_path=file_path) + } else { + write("\n", file=file_path, append = TRUE) + } + + unique_subjects <- unique(tdf$s) + + lapply ( unique_subjects, function(x) write_statements(x, tdf, file_path)) + + invisible(TRUE) +} + +#' @keywords internal +get_class_definition <- function(instance) { + return_value <- which(instance$p == "a")[1] + if (length(return_value)==0) stop(instance$s, " has no class definition.") + return_value +} + +#' @keywords internal +write_statements <- function(x, tdf, file_path) { + instance <- tdf[which(tdf$s == x), ] + instance_definition <- get_class_definition(instance) + instance_rows <- 1:nrow(instance) + instance_rows <- instance_rows[!instance_rows %in% instance_definition] + + definition_statement <- paste0(instance$s[instance_definition], + " a ", + instance$o[instance_definition], ";") + + if (length(instance_rows)>0) { + write(x = paste0(definition_statement), + file = file_path, append = T + ) + statements <- c( + #further statements + unique(paste0(" ", instance$p[instance_rows], " ", instance$o[instance_rows], " ;")) + ) + + # last statement must end with a . + last_statement <- statements[length(statements)] + statements[length(statements)] <- paste0(substr(last_statement,1, nchar(last_statement)-1), ".") + write( statements, + file = file_path, append = T + ) + write( "\n", + file = file_path, append = T + ) + + } else { + ## there is only a class definition + write(x = paste0(definition_statement, " ."), + file = file_path, append = T + ) + + write( "\n", + file = file_path, append = T + ) + ## what if no class definition? + } +} diff --git a/R/ttl_write.R b/R/ttl_write.R index 169a13f..7358a3f 100644 --- a/R/ttl_write.R +++ b/R/ttl_write.R @@ -8,28 +8,66 @@ #' @param overwrite If the file exists, overwrite it? Defaults to \code{TRUE}. #' @return A text file with the prefix and the observation serialisations. #' @examples -#' testtdf <- data.frame (s = c("eg:o1", "eg:01", "eg:02"), -#' p = c("a", "eg-var:", "eg-var"), -#' o = c("qb:Observation", -#' "\"1\"^^", -#' "\"2\"^^")) +#' tdf <- data.frame (s = c("eg:01","eg:02", "eg:01", "eg:02", "eg:01" ), +#' p = c("a", "a", "eg-var:", "eg-var:", "rdfs:label"), +#' o = c("qb:Observation", +#' "qb:Observation", +#' "\"1\"^^", +#' "\"2\"^^", +#' '"Example observation"')) #' #' examplefile <- file.path(tempdir(), "ttl_dataset_write.ttl") #' -#' dataset_ttl_write(tdf=testtdf, file_path = examplefile) +#' dataset_ttl_write(tdf=tdf, file_path = examplefile) #' #' readLines(examplefile) #' @export -#' -ttl_write <- function(tdf, ttl_namespace, file_path, overwrite=TRUE) { + +ttl_write <- function(tdf, ttl_namespace=NULL, file_path, overwrite=TRUE) { - getdata <- function() { - e <- new.env() - name <- utils::data("dataset_namespace", package = "dataset", envir = e)[1] - e[[name]] + if (is.null(ttl_namespace)) { + ## load dataset_namespace + default_namespace <- getdata("dataset_namespace", "dataset") + default_namespace <- default_namespace[ + which(default_namespace$prefix %in% c("rdf:", "rdfs:", "owl:", + "qb:", "dcat:", "xsd:")),] + + ttl_namespace <- default_namespace } - dataset_namespace <- getdata() + ## validate dataset + validate_tdf(tdf) + + ## use subject predicate, object names + names(tdf) <- c("s", "p", "o") + + ## If no file_path is given, create a temporary file + if (is.null(file_path)) file_path <- file.path(tempdir(), "tmp.ttl") + + if (overwrite) { + reset_text_file(file_path=file_path) + } + + ttl_prefix_write(ttl_namespace = ttl_namespace, + file_path=file_path, + overwrite=overwrite) + + ttl_statements_write(tdf, file_path, overwrite=FALSE) +} - dataset::dataset_ttl_write(tdf, ttl_namespace=dataset_namespace, file_path, overwrite) +#' @title Validate tdf +#' @description +#' The tdf object must be inherited from data.frame and have exactly three columns. +#' @keywords internal +validate_tdf <- function(tdf) { + + if ( ! inherits(tdf, "data.frame") ) { + stop("ttl_dataset_write(): tdf must be a data.frame (inherited) object") + } + + if ( ncol(tdf) != 3 ) { + stop("ttl_dataset_write(): tdf must have exactly three columns.") + } + + TRUE } diff --git a/R/utils.R b/R/utils.R new file mode 100644 index 0000000..faf4155 --- /dev/null +++ b/R/utils.R @@ -0,0 +1,20 @@ +#' @title Get data +#' @description +#' Get an internal dataset from an R package. +#' @param dataset The name of the dataset. +#' @param package The name of the package. +#' @importFrom utils data +#' @examples +#' getdata("dataset_namespace", package = "dataset") +#' @keywords internal +getdata <- function( dataset, package ) { + e <- new.env() + name <- utils::data("dataset_namespace", package = "dataset", envir = e)[1] + e[[name]] +} + +#' @keywords internal +reset_text_file <- function(file_path) { + if(file.exists(file_path)) file.remove(file_path) + writeLines(text="", file_path) +} diff --git a/README.Rmd b/README.Rmd index cf61393..a4a2dec 100644 --- a/README.Rmd +++ b/README.Rmd @@ -4,7 +4,7 @@ output: github_document -```{r, include = FALSE} +```{r setupknitr, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>", @@ -19,7 +19,7 @@ knitr::opts_chunk$set( [![lifecycle](https://lifecycle.r-lib.org/articles/figures/lifecycle-experimental.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental) [![Project Status: WIP](https://www.repostatus.org/badges/latest/wip.svg)](https://www.repostatus.org/#wip) [![dataobservatory](https://img.shields.io/badge/ecosystem-dataobservatory.eu-3EA135.svg)](https://dataobservatory.eu/) -[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.10576998.svg)](https://zenodo.org/records/10576998) +[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.11582410.svg)](https://zenodo.org/records/10576998) The goal of tuRtle is to parse or export R data with the Turtle syntax for the Resource Description Framework (RDF). This is a very early version that is being co-developed with the [dataset](https://dataset.dataobservatory.eu/) package. @@ -34,21 +34,30 @@ remotes::install_github("dataobservatory-eu/tuRtle", build = FALSE) ## Example -This is a basic example which shows you how to solve a common problem: +Let us organise statements into a table of *s* subject, *p* predicate and *o* object: -```{r example} -library(tuRtle) -testtdf <- data.frame (s = c("eg:o1", "eg:01", "eg:02"), - p = c("a", "eg-var:", "eg-var"), - o = c("qb:Observation", - "\"1\"^^", - "\"2\"^^")) - -examplefile <- file.path(tempdir(), "ttl_dataset_write.ttl") -ttl_write(tdf=testtdf, file_path = examplefile) -readLines(examplefile) +```{r tdfdef, results='asis'} +tdf <- data.frame (s = c("eg:01","eg:02", "eg:01", "eg:02", "eg:01" ), + p = c("a", "a", "eg-var:", "eg-var:", "rdfs:label"), + o = c("qb:Observation", + "qb:Observation", + "\"1\"^^", + "\"2\"^^", + '"Example observation"') + ) + +knitr::kable(tdf) ``` +The Turtle serialisation is this, written into an `example_file`. The parameter `ttl_namespace = NULL` results in using the default prefixes of the dataset package. + +```{r ttl_example} +library(tuRtle) +example_file<- file.path(tempdir(), "example_ttl.ttl") +ttl_write(tdf=tdf, ttl_namespace = NULL, file_path = example_file) + +readLines(example_file) +``` ## Code of Conduct diff --git a/README.md b/README.md index cbfb373..d51a5d1 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ [![Project Status: WIP](https://www.repostatus.org/badges/latest/wip.svg)](https://www.repostatus.org/#wip) [![dataobservatory](https://img.shields.io/badge/ecosystem-dataobservatory.eu-3EA135.svg)](https://dataobservatory.eu/) -[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.10576998.svg)](https://zenodo.org/records/10576998) +[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.11582410.svg)](https://zenodo.org/records/10576998) The goal of tuRtle is to parse or export R data with the Turtle syntax @@ -28,52 +28,56 @@ remotes::install_github("dataobservatory-eu/tuRtle", build = FALSE) ## Example -This is a basic example which shows you how to solve a common problem: +Let us organise statements into a table of *s* subject, *p* predicate +and *o* object: + +``` r +tdf <- data.frame (s = c("eg:01","eg:02", "eg:01", "eg:02", "eg:01" ), + p = c("a", "a", "eg-var:", "eg-var:", "rdfs:label"), + o = c("qb:Observation", + "qb:Observation", + "\"1\"^^", + "\"2\"^^", + '"Example observation"') + ) + +knitr::kable(tdf) +``` + +| s | p | o | +|:------|:-----------|:----------------------| +| eg:01 | a | | +| eg:02 | a | | +| eg:01 | eg-var: | “1”^^ | +| eg:02 | eg-var: | “2”^^ | +| eg:01 | rdfs:label | “Example observation” | + +The Turtle serialisation is this, written into an `example_file`. The +parameter `ttl_namespace = NULL` results in using the default prefixes +of the dataset package. ``` r library(tuRtle) -testtdf <- data.frame (s = c("eg:o1", "eg:01", "eg:02"), - p = c("a", "eg-var:", "eg-var"), - o = c("qb:Observation", - "\"1\"^^", - "\"2\"^^")) - -examplefile <- file.path(tempdir(), "ttl_dataset_write.ttl") -ttl_write(tdf=testtdf, file_path = examplefile) -#> Warning in utils::data(..., envir = e): data set 'dataset_namespace' not found -readLines(examplefile) -#> [1] "@prefix dbo: ." -#> [2] "@prefix dcterms: ." -#> [3] "@prefix eg: ." -#> [4] "@prefix ex-geo: ." -#> [5] "@prefix foaf: ." -#> [6] "@prefix owl: ." -#> [7] "@prefix qb: ." -#> [8] "@prefix rdf: ." -#> [9] "@prefix rdfs: ." -#> [10] "@prefix sdmx-attribute: ." -#> [11] "@prefix sdmx-code: ." -#> [12] "@prefix sdmx-concept: ." -#> [13] "@prefix sdmx-concept: ." -#> [14] "@prefix sdmx-dimension: ." -#> [15] "@prefix sdmx-measure: ." -#> [16] "@prefix sdmx-metadata: ." -#> [17] "@prefix sdmx-subject: ." -#> [18] "@prefix sdmx: ." -#> [19] "@prefix skos: ." -#> [20] "@prefix xsd: ." -#> [21] "" -#> [22] "# -- Observations -----------------------------------------" -#> [23] "" -#> [24] "eg:o1 a qb:Observation ;" -#> [25] " a qb:Observation ;" -#> [26] " ." -#> [27] "eg:01 a qb:Observation ;" -#> [28] " eg-var: \"1\"^^ ;" -#> [29] " ." -#> [30] "eg:02 a qb:Observation ;" -#> [31] " eg-var \"2\"^^ ;" -#> [32] " ." +example_file<- file.path(tempdir(), "example_ttl.ttl") +ttl_write(tdf=tdf, ttl_namespace = NULL, file_path = example_file) + +readLines(example_file) +#> [1] "@prefix owl: ." +#> [2] "@prefix qb: ." +#> [3] "@prefix rdf: ." +#> [4] "@prefix rdfs: ." +#> [5] "@prefix xsd: ." +#> [6] "" +#> [7] "" +#> [8] "eg:01 a qb:Observation;" +#> [9] " eg-var: \"1\"^^ ;" +#> [10] " rdfs:label \"Example observation\" ." +#> [11] "" +#> [12] "" +#> [13] "eg:02 a qb:Observation;" +#> [14] " eg-var: \"2\"^^ ." +#> [15] "" +#> [16] "" ``` ## Code of Conduct diff --git a/_pkgdown.yml b/_pkgdown.yml index 32f7972..4064ae1 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -24,4 +24,3 @@ authors: href: https://reprex.nl/ html: - diff --git a/inst/WORDLIST b/inst/WORDLIST new file mode 100644 index 0000000..3be1f8b --- /dev/null +++ b/inst/WORDLIST @@ -0,0 +1,9 @@ +RDF +WIP +Zenodo +dataobservatory +eg +lifecycle +rdfs +tdf +var diff --git a/man/dataset_ttl_write.Rd b/man/dataset_ttl_write.Rd new file mode 100644 index 0000000..31d6f4c --- /dev/null +++ b/man/dataset_ttl_write.Rd @@ -0,0 +1,41 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ttl_dataset_write.R +\name{dataset_ttl_write} +\alias{dataset_ttl_write} +\title{Write a dataset into Turtle serialisation} +\usage{ +dataset_ttl_write( + tdf, + ttl_namespace = NULL, + file_path = NULL, + overwrite = TRUE +) +} +\arguments{ +\item{tdf}{A dataset in exactly three columns.} + +\item{ttl_namespace}{The namespace definitions of the dataset.} + +\item{file_path}{The path to the file that should be written or appended.} + +\item{overwrite}{If the file exists, overwrite it? Defaults to \code{TRUE}.} +} +\value{ +A text file with the prefix and the observation serialisations. +} +\description{ +Write a dataset into a Turtle serialisation. +} +\examples{ +testtdf <- data.frame (s = c("eg:o1", "eg:01", "eg:02"), + p = c("a", "eg-var:", "eg-var"), + o = c("qb:Observation", + "\"1\"^^", + "\"2\"^^")) + +examplefile <- file.path(tempdir(), "ttl_dataset_write.ttl") + +dataset_ttl_write(tdf=testtdf, file_path = examplefile) + +readLines(examplefile) +} diff --git a/man/getdata.Rd b/man/getdata.Rd new file mode 100644 index 0000000..d8b6ae8 --- /dev/null +++ b/man/getdata.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{getdata} +\alias{getdata} +\title{Get data} +\usage{ +getdata(dataset, package) +} +\arguments{ +\item{dataset}{The name of the dataset.} + +\item{package}{The name of the package.} +} +\description{ +Get an internal dataset from an R package. +} +\examples{ +getdata("dataset_namespace", package = "dataset") +} +\keyword{internal} diff --git a/man/ttl_prefix_write.Rd b/man/ttl_prefix_write.Rd new file mode 100644 index 0000000..c699f06 --- /dev/null +++ b/man/ttl_prefix_write.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ttl_prefix_write.R +\name{ttl_prefix_write} +\alias{ttl_prefix_write} +\title{Write TTL prefix} +\usage{ +ttl_prefix_write(ttl_namespace, file_path, overwrite = TRUE) +} +\arguments{ +\item{ttl_namespace}{A namespace definition as a two-column dataset.} + +\item{file_path}{A file_path to write the TTL prefix namespace definitions.} + +\item{overwrite}{Boolean, if the file should be overwritten (if it exists at all.) +Defaults to \code{TRUE}.} +} +\description{ +Write the namespace prefixes into the header of a Turtle serialisation +} +\examples{ +ttl_namespace <- data.frame( + prefix = c("dbo:", "dcterms:"), + uri = c("", + "") +) +file_path <- tempfile() +ttl_prefix_write(ttl_namespace, file_path) +readLines(file_path) + +} diff --git a/man/ttl_statements_write.Rd b/man/ttl_statements_write.Rd new file mode 100644 index 0000000..0646b3a --- /dev/null +++ b/man/ttl_statements_write.Rd @@ -0,0 +1,33 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ttl_statements_write.R +\name{ttl_statements_write} +\alias{ttl_statements_write} +\title{Write statements into a TTL file} +\usage{ +ttl_statements_write(tdf, file_path, overwrite = TRUE) +} +\arguments{ +\item{tdf}{A dataset in exactly three columns.} + +\item{file_path}{The path to the file that should be written or appended.} + +\item{overwrite}{If the file exists, overwrite it? Defaults to \code{TRUE}.} +} +\description{ +Write statements into a TTL file +} +\examples{ +tdf <- data.frame (s = c("eg:01","eg:02", "eg:01", "eg:02", "eg:01" ), + p = c("a", "a", "eg-var:", "eg-var:", "rdfs:label"), + o = c("qb:Observation", + "qb:Observation", + "\"1\"^^", + "\"2\"^^", + '"Example observation"')) + +examplefile <- file.path(tempdir(), "ttl_dataset_write.ttl") + +dataset_ttl_write(tdf=tdf, file_path = examplefile) + +readLines(examplefile) +} diff --git a/man/ttl_write.Rd b/man/ttl_write.Rd index 3504520..fc1e712 100644 --- a/man/ttl_write.Rd +++ b/man/ttl_write.Rd @@ -4,7 +4,7 @@ \alias{ttl_write} \title{Write a dataset into Turtle serialisation} \usage{ -ttl_write(tdf, ttl_namespace, file_path, overwrite = TRUE) +ttl_write(tdf, ttl_namespace = NULL, file_path, overwrite = TRUE) } \arguments{ \item{tdf}{A dataset in exactly three columns.} @@ -22,15 +22,17 @@ A text file with the prefix and the observation serialisations. Write a dataset into a Turtle serialisation. } \examples{ -testtdf <- data.frame (s = c("eg:o1", "eg:01", "eg:02"), - p = c("a", "eg-var:", "eg-var"), - o = c("qb:Observation", - "\"1\"^^", - "\"2\"^^")) +tdf <- data.frame (s = c("eg:01","eg:02", "eg:01", "eg:02", "eg:01" ), + p = c("a", "a", "eg-var:", "eg-var:", "rdfs:label"), + o = c("qb:Observation", + "qb:Observation", + "\"1\"^^", + "\"2\"^^", + '"Example observation"')) examplefile <- file.path(tempdir(), "ttl_dataset_write.ttl") -dataset_ttl_write(tdf=testtdf, file_path = examplefile) +dataset_ttl_write(tdf=tdf, file_path = examplefile) readLines(examplefile) } diff --git a/man/validate_tdf.Rd b/man/validate_tdf.Rd new file mode 100644 index 0000000..9462393 --- /dev/null +++ b/man/validate_tdf.Rd @@ -0,0 +1,12 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/ttl_write.R +\name{validate_tdf} +\alias{validate_tdf} +\title{Validate tdf} +\usage{ +validate_tdf(tdf) +} +\description{ +The tdf object must be inherited from data.frame and have exactly three columns. +} +\keyword{internal} diff --git a/tests/spelling.R b/tests/spelling.R new file mode 100644 index 0000000..6713838 --- /dev/null +++ b/tests/spelling.R @@ -0,0 +1,3 @@ +if(requireNamespace('spelling', quietly = TRUE)) + spelling::spell_check_test(vignettes = TRUE, error = FALSE, + skip_on_cran = TRUE) diff --git a/tests/testthat/test-ttl_prefix_write.R b/tests/testthat/test-ttl_prefix_write.R new file mode 100644 index 0000000..70fcc4b --- /dev/null +++ b/tests/testthat/test-ttl_prefix_write.R @@ -0,0 +1,27 @@ +ttl_namespace <- data.frame( + prefix = c("dbo:", "dcterms:"), + uri = c("", + "")) + +file_path <- tempfile() + +overwrite_temp_file <- file.path(tempdir(), "ttl_overwrite.ttl") +writeLines(text = "@prefix example: .", con = overwrite_temp_file) +readLines(overwrite_temp_file) + +ttl_prefix_write(ttl_namespace = ttl_namespace[1:2,], overwrite_temp_file, overwrite = F ) +test_that("ttl_prefix_write(... overwrite = FALSE) works", { + expect_equal(readLines(overwrite_temp_file), + c("@prefix example: .", + "@prefix dbo: .", + "@prefix dcterms: .")) +}) + +ttl_prefix_write(ttl_namespace = ttl_namespace[1:2,], + file_path = overwrite_temp_file, overwrite = T ) +test_that("ttl_prefix_write(... overwrite = TRUE) works", { + expect_equal(readLines(overwrite_temp_file), + c("@prefix dbo: .", + "@prefix dcterms: .")) +}) + diff --git a/tests/testthat/test-ttl_statements_write.R b/tests/testthat/test-ttl_statements_write.R new file mode 100644 index 0000000..c69c980 --- /dev/null +++ b/tests/testthat/test-ttl_statements_write.R @@ -0,0 +1,21 @@ +testtdf <- data.frame (s = c("eg:01","eg:02", "eg:01", "eg:02", "eg:01" ), + p = c("a", "a", "eg-var:", "eg-var:", "rdfs:label"), + o = c("qb:Observation", + "qb:Observation", + "\"1\"^^", + "\"2\"^^", + '"Example observation"') + ) + +tdf <- testtdf + +test_ttl_statements_file <- file.path(tempdir(), "test_ttl_statements_write.ttl") +ttl_statements_write(tdf = tdf, file_path = test_ttl_statements_file ) + +test_ttl_statements <- readLines(test_ttl_statements_file) +if (test_ttl_statements[1] == "") test_ttl_statements <- test_ttl_statements[-1] +test_ttl_statements +test_that("ttl_statements_write() works", { + expect_equal(test_ttl_statements[1], "eg:01 a qb:Observation;") + expect_equal(test_ttl_statements[3], " eg-var: \"1\"^^ ;" ) +}) diff --git a/tests/testthat/test-ttl_write.R b/tests/testthat/test-ttl_write.R new file mode 100644 index 0000000..0329b03 --- /dev/null +++ b/tests/testthat/test-ttl_write.R @@ -0,0 +1,27 @@ +testtdf <- data.frame (s = c("eg:01","eg:02", "eg:01", "eg:02", "eg:01" ), + p = c("a", "a", "eg-var:", "eg-var:", "rdfs:label"), + o = c("qb:Observation", + "qb:Observation", + "\"1\"^^", + "\"2\"^^", + '"Example observation"')) + +test_that("validate_tdf() works", { + expect_error(validate_tdf(data.frame( a = 1, b = 2))) + expect_error(validate_tdf(a=c(1,2))) +}) + +tdf <- testtdf + + +test_ttl_write_file <- file.path(tempdir(), "test_ttl_write.ttl") +ttl_write(tdf = tdf, ttl_namespace = NULL, file_path = test_ttl_write_file, overwrite=TRUE) + +test_ttl <- readLines(test_ttl_write_file) +test_ttl + +test_that("ttl_write() works", { + expect_equal(test_ttl[1], "@prefix owl: .") + expect_equal(test_ttl[2], "@prefix qb: ." ) + expect_equal(test_ttl[8], "eg:01 a qb:Observation;" ) +}) diff --git a/tests/testthat/test-utils.R b/tests/testthat/test-utils.R new file mode 100644 index 0000000..d1cdfe1 --- /dev/null +++ b/tests/testthat/test-utils.R @@ -0,0 +1,16 @@ +default_namespace <- getdata("dataset_namespace", package = "dataset") + +test_that("get_data() works", { + expect_equal(names(default_namespace), c("prefix", "uri")) +}) + +test_reset_file <- file.path(tempdir(), "test_reset.ttl") +writeLines(text="reset", test_reset_file) +reset_text_file(test_reset_file) + +test_that("get_data() works", { + expect_equal(readLines(test_reset_file), "") +}) + + + diff --git a/tuRtle.Rproj b/tuRtle.Rproj index 21a4da0..1da8680 100644 --- a/tuRtle.Rproj +++ b/tuRtle.Rproj @@ -15,3 +15,5 @@ LaTeX: pdfLaTeX BuildType: Package PackageUseDevtools: Yes PackageInstallArgs: --no-multiarch --with-keep.source + +SpellingDictionary: en_GB