Revisión | c2dc1be9243fb86daebdcc332f02d30c5189d32a (tree) |
---|---|
Tiempo | 2025-01-17 21:32:00 |
Autor | Lorenzo Isella <lorenzo.isella@gmai...> |
Commiter | Lorenzo Isella |
This script run queries on data.europa.eu and return the result as a tibble.
@@ -0,0 +1,83 @@ | ||
1 | +# Load necessary libraries | |
2 | +library(httr) | |
3 | +library(jsonlite) | |
4 | +library(dplyr) | |
5 | +library(purrr) | |
6 | + | |
7 | +# Function to query the data.europa.eu SPARQL endpoint and return results as a list | |
8 | +query_data_europa_list <- function(query, endpoint = "https://data.europa.eu/sparql") { | |
9 | + # Send the query using POST and the httr package | |
10 | + response <- POST( | |
11 | + url = endpoint, | |
12 | + body = list(query = query), | |
13 | + encode = "form" | |
14 | + ) | |
15 | + | |
16 | + # Check if the status code is 200 (successful) | |
17 | + if (status_code(response) == 200) { | |
18 | + # Parse and return the JSON content as a list | |
19 | + result <- content(response, "parsed", type = "application/json") | |
20 | + return(result$results$bindings) # Return the "bindings" part of the result as a list | |
21 | + } else { | |
22 | + stop("Query failed with status code: ", status_code(response)) | |
23 | + } | |
24 | +} | |
25 | + | |
26 | +# Function to convert a list of SPARQL bindings into a tibble | |
27 | +convert_list_to_tibble <- function(bindings_list) { | |
28 | + if (length(bindings_list) == 0) { | |
29 | + return(tibble()) # Return an empty tibble if the list is empty | |
30 | + } | |
31 | + | |
32 | + # Extract fields using purrr::map() and create a tibble | |
33 | + tibble( | |
34 | + dataset = map_chr(bindings_list, ~ .x$dataset$value), | |
35 | + title = map_chr(bindings_list, ~ .x$title$value), | |
36 | + description = map_chr(bindings_list, ~ .x$description$value) | |
37 | + ) | |
38 | +} | |
39 | + | |
40 | +# Example SPARQL query to get datasets related to "climate change" | |
41 | +query_climate_change <- " | |
42 | +PREFIX dcat: <http://www.w3.org/ns/dcat#> | |
43 | +PREFIX dcterms: <http://purl.org/dc/terms/> | |
44 | + | |
45 | +SELECT ?dataset ?title ?description | |
46 | +WHERE { | |
47 | + ?dataset a dcat:Dataset ; | |
48 | + dcterms:title ?title ; | |
49 | + dcterms:description ?description . | |
50 | + FILTER(CONTAINS(LCASE(?title), 'climate change')) | |
51 | +} | |
52 | +LIMIT 5 | |
53 | +" | |
54 | + | |
55 | +# Query to fetch all datasets (example query) | |
56 | +query_all_datasets <- " | |
57 | +PREFIX dcat: <http://www.w3.org/ns/dcat#> | |
58 | +PREFIX dcterms: <http://purl.org/dc/terms/> | |
59 | + | |
60 | +SELECT ?dataset ?title ?description | |
61 | +WHERE { | |
62 | + ?dataset a dcat:Dataset ; | |
63 | + dcterms:title ?title ; | |
64 | + dcterms:description ?description . | |
65 | +} | |
66 | +LIMIT 10 | |
67 | +" | |
68 | + | |
69 | +# Run the queries and return the results as a list | |
70 | +result_climate_change_list <- query_data_europa_list(query_climate_change) | |
71 | +result_all_datasets_list <- query_data_europa_list(query_all_datasets) | |
72 | + | |
73 | +# Convert list results into tibbles | |
74 | +tibble_climate_change <- convert_list_to_tibble(result_climate_change_list) | |
75 | +tibble_all_datasets <- convert_list_to_tibble(result_all_datasets_list) | |
76 | + | |
77 | +# Display the tibbles | |
78 | +cat("Tibble for Climate Change Related Datasets:\n") | |
79 | +print(tibble_climate_change) | |
80 | + | |
81 | +cat("\nTibble for All Datasets (First 10):\n") | |
82 | +print(tibble_all_datasets) | |
83 | + |