This post walks through the steps to create a simple R-Shiny app that fetches data from a SPARQL endpoint and displays it in a table. This can be particularly useful for visualizing linked data in a user-friendly interface.
Compete Scripts
Complete scripts for each connection method are available via this Github repository.
Prerequisites
Before we start, ensure you have R and RStudio installed on your machine. Additionally, you need to install the following R packages, depending on your connection preference to Virtuoso:
HTTP(S)
install.packages("shiny")
install.packages("httr")
install.packages("readr")
ODBC Driver
install.packages("RODBC")
JDBC Driver
install.packages("RJDBC")
Step 1: Setting Up the Shiny App
First, let’s create a new R script file in RStudio and save it as app.R. This file will contain both the UI and server logic for our Shiny app.
Step 2: Defining the User Interface (UI)
The UI of our Shiny app will be straightforward, consisting of a title panel and a main panel to display the table output. Add the following code to your app.R file:
library(shiny)
# Define UI
ui <- fluidPage(
titlePanel("SPARQL Data Viewer"),
mainPanel(
tableOutput("sparqlTable")
)
)
Step 3: Defining the Server Logic
Depending on your chosen connection method (HTTP, ODBC, or JDBC), you’ll need to define the server logic accordingly. Below are the instructions for each method:
Using HTTP (SPARQL)
library(httr)
library(readr)
# Define server logic
server <- function(input, output) {
# SPARQL endpoint URL
sparql_url <- "http://linkeddata.uriburner.com/sparql/?default-graph-uri=&query=SELECT+*%0D%0AFROM+%3Curn%3Aanalytics%3E%0D%0AWHERE%0D%0A%7B%0D%0A+%3Fs+%3Fp+%3Fo.%0D%0A%7D&should-sponge=&format=text%2Fcsv&timeout=30000"
# Fetch the data
response <- GET(sparql_url)
content <- content(response, "text")
data <- read_csv(content)
# Output the data as a table
output$sparqlTable <- renderTable({
data
})
}
Using ODBC (SQL, SPARQL)
library(RODBC)
# Define server logic for ODBC
server_odbc <- function(input, output) {
# ODBC DSN
dsn <- "your_dsn_name"
# Connect to the ODBC DSN
conn <- odbcConnect(dsn)
# SPARQL query
query <- "SELECT * FROM <urn:analytics> WHERE { ?s ?p ?o. }"
# Execute the query
data <- sqlQuery(conn, query)
# Close the connection
close(conn)
# Output the data as a table
output$sparqlTable <- renderTable({
data
})
}
Using JDBC (SQL, SPARQL)
If you choose to use a JDBC connection, add the following code to your app.R file:
library(RJDBC)
# Define server logic for JDBC
server_jdbc <- function(input, output) {
# JDBC driver and connection details
jdbc_driver <- JDBC(driverClass = "virtuoso.jdbc4.Driver", classPath = "/path/to/virtjdbc4.jar")
jdbc_url <- "jdbc:virtuoso://your_virtuoso_host:1111"
jdbc_user <- "your_username"
jdbc_password <- "your_password"
# Connect to the JDBC DSN
conn <- dbConnect(jdbc_driver, jdbc_url, jdbc_user, jdbc_password)
# SPARQL query
query <- "SPARQL SELECT * FROM <urn:analytics> WHERE { ?s ?p ?o. }"
# Execute the query
data <- dbGetQuery(conn, query)
# Close the connection
dbDisconnect(conn)
# Output the data as a table
output$sparqlTable <- renderTable({
data
})
}
Step 4: Running the Shiny App
Finally, we’ll add the code to run the Shiny app. Depending on the connection method you chose, update the server function call accordingly. Add the following code at the bottom of your app.R file:
# Run the application
shinyApp(ui = ui, server = server)
Step 5: Launching the App
To run the app, click the “Run App” button in RStudio or use the following command in the R console:
shiny::runApp("path/to/your/app.R")
Conclusion
In this tutorial, we’ve created a simple R-Shiny app that fetches data from a SPARQL endpoint and displays it in a table. This setup can be expanded to include more complex queries and interactive features, providing a powerful tool for linked data visualization and analysis.