-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- adds a c() method to concat rdf objects - rdf_query() now coerces return type if it recognizes the data URI and can match it to an R type (a few XMLSchema types are recognized, otherwise defaults to character string). - drop support for trig, which redland bindings fails to recognize - fix support for turtle by changing mime=type for raptor - add_rdf now coerces object to string type automatically - experimental work to serialize arbitrary data.frames to RDF
- Loading branch information
Showing
12 changed files
with
312 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
## Utilities to coerce return type, if recognized | ||
r_class <- function(x){ | ||
switch(gsub("<http://www.w3.org/2001/XMLSchema#(.*)>", "xs:\\1", x), | ||
"xs:decimal" = "numeric", | ||
"xs:string" = "character", | ||
"xs:string" = "factor", | ||
"xs:boolean" = "logical", | ||
"xs:integer" = "integer", | ||
"xs:date" = "Date", | ||
"xs:dateTime" = "POSIXct", | ||
"character" | ||
) | ||
} | ||
get_values <- function(x) gsub('\"(([^\\^])+)\"\\^*(.*)', "\\1", x) | ||
get_types <- function(x) out <- gsub('\"(([^\\^])+)\"\\^*(.*)', "\\3", x) | ||
|
||
|
||
#' @importFrom methods as | ||
type_by_datauri <- function(x){ | ||
types <- get_types(x) | ||
r_types <- vapply(get_types(x), r_class, character(length(1))) | ||
df <- data.frame(value = get_values(x), class = r_types) | ||
apply(df, 1, function(x) as(x[1], x[2])) | ||
} | ||
|
||
|
||
rectangularize_query_results <- function(out){ | ||
vars <- unique(names(out)) | ||
X <- lapply(vars, function(v) | ||
## Strip ^^TYPE typing | ||
#gsub('\"(([^\\^])+)\"\\^*.*', | ||
# "\\1", | ||
type_by_datauri(as.character(out[names(out) == v ]))) | ||
names(X) <- vars | ||
as.data.frame(X, stringsAsFactors=FALSE) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
as_rdf <- function(df, base_uri = NULL) UseMethod("as_rdf") | ||
|
||
as_rdf.data.frame <- function(df, base_uri = NULL){ | ||
|
||
subjectType <- "blank" | ||
if(is.null(base_uri)){ | ||
base_uri <- paste0(gsub("[^a-zA-Z]","",deparse(substitute(df))), ":") | ||
subjectType <- "uri" | ||
} | ||
|
||
x <- tibble::rowid_to_column(df, "subject") | ||
suppressWarnings( | ||
x <- tidyr::gather(x, key = predicate, value = object, -subject) | ||
) | ||
## Add a column for the data type | ||
col_classes <- data.frame(datatype = vapply(df, xs_class, character(1))) | ||
col_classes <- tibble::rownames_to_column(col_classes, "predicate") | ||
x <- dplyr::inner_join(x, col_classes, "predicate") | ||
|
||
rdf <- rdf() | ||
for(i in seq_along(x$subject)){ | ||
rdf <- rdf_add(rdf, | ||
subject = paste0(base_uri,as.character(x$subject[[i]])), | ||
predicate = paste0(base_uri, x$predicate[[i]]), | ||
object = as.character(x$object[[i]]), | ||
subjectType = subjectType, | ||
datatype_uri = x$datatype[[i]]) | ||
} | ||
rdf | ||
} | ||
|
||
xs_class <- function(x){ | ||
gsub("^xs:", | ||
"http://www.w3.org/2001/XMLSchema#", | ||
switch(class(x), | ||
"numeric" = "xs:decimal", | ||
"character" = "xs:string", | ||
"factor" = "xs:string", | ||
"logical" = "xs:boolean", | ||
"integer" = "xs:integer", | ||
"Date" = "xs:date", | ||
"POSIXct" = "xs:dateTime", | ||
NULL | ||
)) | ||
} | ||
|
||
|
||
as_rdf.list <- function(x){ | ||
|
||
} | ||
|
||
cars <- mtcars[1:4, 1:4] %>% rownames_to_column("Model") | ||
|
||
|
||
x1 <- as_rdf(iris) | ||
x2 <- as_rdf(cars) | ||
rdf <- c(x1,x2) | ||
|
||
|
||
sparql <- | ||
'SELECT ?Sepal_Length ?Sepal_Width ?Petal_Length ?Petal_Width ?Species | ||
WHERE { | ||
?s <iris:Sepal.Width> ?Sepal_Width . | ||
?s <iris:Sepal.Length> ?Sepal_Length . | ||
?s <iris:Petal.Width> ?Petal_Width . | ||
?s <iris:Petal.Length> ?Petal_Length . | ||
?s <iris:Species> ?Species . | ||
}' | ||
tmp <- rdf_query(rdf, sparql) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.