Skip to content

Commit

Permalink
0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
antaldaniel committed Jun 11, 2024
1 parent 6899a54 commit dd1dced
Show file tree
Hide file tree
Showing 27 changed files with 692 additions and 86 deletions.
1 change: 1 addition & 0 deletions .github/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.html
48 changes: 48 additions & 0 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
@@ -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
61 changes: 61 additions & 0 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 7 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 3 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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.
53 changes: 53 additions & 0 deletions R/ttl_dataset_write.R
Original file line number Diff line number Diff line change
@@ -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\"^^<xs:decimal>",
#' "\"2\"^^<xs:decimal>"))
#'
#' 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))
}

36 changes: 36 additions & 0 deletions R/ttl_prefix_write.R
Original file line number Diff line number Diff line change
@@ -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("<http://dbpedia.org/ontology/>",
#' "<http://purl.org/dc/terms/>")
#' )
#' 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 )
}
}
86 changes: 86 additions & 0 deletions R/ttl_statements_write.R
Original file line number Diff line number Diff line change
@@ -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\"^^<xs:decimal>",
#' "\"2\"^^<xs:decimal>",
#' '"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?
}
}
Loading

0 comments on commit dd1dced

Please sign in to comment.