File: rdf_intro.R

package info (click to toggle)
r-cran-rdflib 0.2.9%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 596 kB
  • sloc: xml: 66; sh: 13; makefile: 2
file content (186 lines) | stat: -rw-r--r-- 6,852 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
## ----include=FALSE------------------------------------------------------------
options(rdf_print_format = "nquads")
is_linux <- Sys.info()["sysname"] == "Linux"
knitr::opts_chunk$set(eval=FALSE)

## ----message = FALSE, warning=FALSE-------------------------------------------
#  library(rdflib)
#  library(dplyr)
#  library(tidyr)
#  library(tibble)
#  library(jsonld)

## -----------------------------------------------------------------------------
#  car_triples <-
#  mtcars %>%
#    rownames_to_column("Model") %>%
#    gather(attribute,measurement, -Model)

## ----echo=FALSE---------------------------------------------------------------
#  DT::datatable(car_triples)

## -----------------------------------------------------------------------------
#  car_triples <-
#  mtcars %>%
#    rownames_to_column("Model") %>%
#    rowid_to_column("subject") %>%
#    gather(predicate, object, -subject)

## ----echo=FALSE---------------------------------------------------------------
#  DT::datatable(car_triples)

## -----------------------------------------------------------------------------
#  iris_triples <- iris %>%
#    rowid_to_column("subject") %>%
#    gather(key = predicate, value = object, -subject)

## ----echo=FALSE---------------------------------------------------------------
#  DT::datatable(iris_triples)

## -----------------------------------------------------------------------------
#  iris_triples <- iris %>%
#    rowid_to_column("subject") %>%
#    mutate(subject = paste0("http://example.com/", "iris#", subject)) %>%
#    gather(key = predicate, value = object, -subject)
#  

## ----echo=FALSE---------------------------------------------------------------
#  DT::datatable(iris_triples)

## ----mesage = FALSE-----------------------------------------------------------
#  iris_triples <- iris %>%
#    rowid_to_column("subject") %>%
#    mutate(subject = paste0("http://example.com/", "iris#", subject)) %>%
#    gather(key = predicate, value = object, -subject) %>%
#    mutate(predicate = paste0("http://example.com/", "iris#", predicate))
#  

## ----echo=FALSE---------------------------------------------------------------
#  DT::datatable(iris_triples)

## -----------------------------------------------------------------------------
#  rdf <- rdf()

## -----------------------------------------------------------------------------
#  base <- paste0("http://example.com/", "iris#")
#  
#  rdf %>%
#    rdf_add(subject = paste0(base, "obs1"),
#            predicate = paste0(base, "Sepal.Length"),
#            object = 5.1)
#  
#  rdf

## -----------------------------------------------------------------------------
#  rdf <- rdf()
#  rdf %>% rdf_add("",
#                  "iris:Sepal.Length",
#                  object = 5.1)
#  rdf

## -----------------------------------------------------------------------------
#  rdf_serialize(rdf, "rdf.nq", format = "nquads")

## -----------------------------------------------------------------------------
#  doc <- system.file("extdata/example.rdf", package="redland")
#  rdf <- rdf_parse(doc, format = "rdfxml")
#  rdf

## -----------------------------------------------------------------------------
#  options(rdf_print_format = "rdfxml")
#  rdf

## -----------------------------------------------------------------------------
#  options(rdf_print_format = "turtle")
#  rdf

## -----------------------------------------------------------------------------
#  options(rdf_print_format = "jsonld")
#  rdf

## -----------------------------------------------------------------------------
#  rdf_serialize(rdf, "example.json", "jsonld") %>%
#    jsonld_compact(context = '{"@vocab": "http://purl.org/dc/elements/1.1/"}')

## -----------------------------------------------------------------------------
#  ex <- system.file("extdata/person.json", package="rdflib")
#  cat(readLines(ex), sep = "\n")
#  #jsonld_compact(ex, "{}")
#  

## -----------------------------------------------------------------------------
#  options(rdf_print_format = "nquads")
#  rdf <- rdf_parse(ex, "jsonld")
#  rdf

## -----------------------------------------------------------------------------
#  jsonld_flatten(ex, context = "https://schema.org/")

## -----------------------------------------------------------------------------
#  jsonld_flatten(ex) %>%
#    jsonld_frame('{"@type": "https://schema.org//Person"}') %>%
#    jsonld_compact(context = "https://schema.org/")

## -----------------------------------------------------------------------------
#  as_rdf.list <- function(x, context = "https://schema.org/"){
#    if(length(x) == 1) x <- x[[1]]
#    x[["@context"]] <- context
#    json <- jsonlite::toJSON(x, pretty = TRUE, auto_unbox = TRUE, force = TRUE)
#    rdflib::rdf_parse(json, "jsonld")
#  }

## -----------------------------------------------------------------------------
#  context <- list("https://schema.org/",
#                  list(schema = "https://schema.org//",
#                       given = "givenName",
#                       family = "familyName",
#                       title = "name",
#                       year = "datePublished",
#                       note = "softwareVersion",
#                       comment = "identifier",
#                       role = "https://www.loc.gov/marc/relators/relaterm.html"))
#  

## -----------------------------------------------------------------------------
#  options(rdf_print_format = "nquads") # go back to the default
#  
#  
#  R <- citation("rdflib")
#  rdf <- as_rdf.list(R, context)
#  rdf

## -----------------------------------------------------------------------------
#  #source(system.file("examples/as_rdf.R", package="rdflib"))
#  source(system.file("examples/tidy_schema.R", package="rdflib"))
#  
#  ## Testing: Digest some data.frames into RDF and extract back
#   cars <- mtcars %>% rownames_to_column("Model")
#   x1 <- as_rdf(iris, NULL, "iris:")
#   x2 <- as_rdf(cars, NULL, "mtcars:")
#   rdf <- c(x1,x2)

## -----------------------------------------------------------------------------
#  sparql <-
#    'SELECT  ?Species ?Sepal_Length ?Sepal_Width ?Petal_Length  ?Petal_Width
#  WHERE {
#   ?s <iris:Species>  ?Species .
#   ?s <iris:Sepal.Width>  ?Sepal_Width .
#   ?s <iris:Sepal.Length>  ?Sepal_Length .
#   ?s <iris:Petal.Length>  ?Petal_Length .
#   ?s <iris:Petal.Width>  ?Petal_Width
#  }'
#  
#  iris2 <- rdf_query(rdf, sparql)

## ----echo=FALSE---------------------------------------------------------------
#  DT::datatable(iris2)

## -----------------------------------------------------------------------------
#  sparql <- tidy_schema("Species",  "Sepal.Length", "Sepal.Width", prefix = "iris")
#  
#  rdf_query(rdf, sparql)

## ----include=FALSE------------------------------------------------------------
#  unlink("rdf.nq")
#  unlink("example.json")