R-some: R and RDF
22 Jul 2014In our database project, the guy (student) who has to configure the database backend asked me, if I could program (or name) a tool that converts tables into RDF data triplets. At first I thought of writing a perl script, but then had a look around. And guess which was the easiest option: R, the Swiss Army Knife of data-processing and analysis. There is a handy package called rrdf. Here is an example how easy one can generate valid turtle from a data.frame:
require(rrdf)
## generate some data for a fictional mathcourse with students that get a grade
mathcourse <- data.frame(subjects = paste0("s:Student#",1:20),pred0 = "http://example.org/attend",objects = "course:MathCourse",grade=sample(1:6,20,replace = T))
## create an empty RDF store
newstore <- new.rdf()
##define some prefixes
add.prefix(newstore, prefix="s", "http://example.org/Student/")
add.prefix(newstore, prefix="course", "http://example.org/Course/")
## assign the subject MathCourse an object called MathCourseGrade
add.triple(newstore,"course:MathCourse","http://example.org/hasGrade","http://example.org/MathCourseGrade")
## add Students and their grades
apply(mathcourse,1,function(x) add.triple(newstore,x[1],x[2],x[3]))
apply(mathcourse,1,function(x) add.data.triple(newstore,x[1],"http://example.org/MathCourseGrade",data=as.character(x[4]),type="integer"))
Now serialize it in turtle
cat(asString.rdf(newstore))
@prefix course: <http://example.org/Course/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix s: <http://example.org/Student/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
<course:MathCourse> <http://example.org/hasGrade>
<http://example.org/MathCourseGrade> .
<s:Student#1> <http://example.org/MathCourseGrade>
3 ;
<http://example.org/attend> <course:MathCourse> .
<s:Student#7> <http://example.org/MathCourseGrade>
4 ;
<http://example.org/attend> <course:MathCourse> .
<s:Student#12> <http://example.org/MathCourseGrade>
6 ;
<http://example.org/attend> <course:MathCourse> .
.
.
.
But we can also query online SPARQL endpoints. Let’s have a look at restaurants 100m around my office:
##look for food
food <- as.data.frame(sparql.remote("http://linkedgeodata.org/sparql",
'Prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
Prefix ogc: <http://www.opengis.net/ont/geosparql#>
Prefix geom: <http://geovocab.org/geometry#>
Prefix lgdo: <http://linkedgeodata.org/ontology/>
Prefix geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
Select *
From <http://linkedgeodata.org> {
?s
a lgdo:Amenity ;
rdfs:label ?l ;
geo:lat ?lat;
geo:long ?long;
lgdo:cuisine ?food;
geom:geometry [
ogc:asWKT ?g
] .
Filter(<bif:st_intersects> (?g, <bif:st_point> (7.847607, 48.000874), 0.1)) .
}',jena = F),stringsAsFactors = F)
This gives us:
> food$food
[1] "kebab"
> food$label
[1] "Ezo"
Our oriental Pizza baker/Kebap shop around the corner. Awesome. Now let’s show this on a map (using the package RgoogleMaps):
require(RgoogleMaps)
lat <- as.numeric(food$lat[1])
long <- as.numeric(food$long[1])
center <- c(lat,long)
zoom <- 15
MyMap <- GetMap(center=center, zoom=zoom,markers = paste0("&markers=color:red|label:E|", center[1],",",center[2]),destfile = "EZO.png");
PlotOnStaticMap(MyMap,center[1],center[2],destfile = "EZO.png")
TADA: