In this case I suggest to consider reading the data in via {sf}
package instead of whatever are you using now.
sf
plays nicely with both databases and leaflet, and returns data as a (modified) data frame. Filtering is very easy.
For a example consider this piece of code. What it does is spins up a local sqlite database and creates in it a table called lines
. I did not have the time to create some lines, so I grabbed a piece of code from a somewhat unrelated blog post Lines from Points · Jindra Lacko This part of the reprex is needlessly complicated, but please bear with me
Once the local database is spun up and populated I demonstrate the ease of querying it via SQL and displaying a result consisting of two rows (raleigh 2 wilmington + raleigh 2 greensboro) in a leaflet object.
library(sf)
library(dplyr)
library(DBI) # generic DBI interface
library(RSQLite) # to spin up a local database
library(leaflet)
cities <- data.frame(name = c("Raleigh", "Greensboro", "Wilmington"),
id = 1:3,
x = c(-78.633333, -79.819444, -77.912222),
y = c(35.766667, 36.08, 34.223333)) %>%
st_as_sf(coords = c("x", "y"), crs = 4326)
# a function to draw lines from points
# see https://www.jla-data.net/eng/lines-from-points/
points_to_lines <- function(data, ids, names, order_matters = TRUE) {
# dataframe of combinations - based on row index
idx <- expand.grid(start = seq(1, nrow(data), 1),
end = seq(1, nrow(data), 1)) %>%
# no line with start & end being the same point
dplyr::filter(start != end) %>%
# when order doesn't matter just one direction is enough
dplyr::filter(order_matters | start > end)
# cycle over the combinations
for (i in seq_along(idx$start)) {
# line object from two points
wrk_line <- data[c(idx$start[i], idx$end[i]), ] %>%
st_coordinates() %>%
st_linestring() %>%
st_sfc()
# a single row of results dataframe
line_data <- data.frame(
start = pull(data, ids)[idx$start[i]],
end = pull(data, ids)[idx$end[i]],
label = paste(pull(data, names)[idx$start[i]],
"-",
pull(data, names)[idx$end[i]]),
geometry = wrk_line
)
# bind results rows to a single object
if (i == 1) {
res <- line_data
} else {
res <- dplyr::bind_rows(res, line_data)
} # /if - saving results
} # /for
# finalize function result
res <- sf::st_as_sf(res, crs = sf::st_crs(data))
res
} # /function
old_lines <- points_to_lines(cities, "id", "name")
# spin up a sqlite database
con <- DBI::dbConnect(RSQLite::SQLite(), "go-tar-heels.sqlite")
# populate the database with some lines data
sf::st_write(old_lines, con, "lines")
# here you can double check the file via a SQL client of your choice
# I suggest dbeaver, but it is not obligatory :)
# now the interesting part!
new_lines <- sf::st_read(dsn = con,
query = "select *
from lines
where start = 1")
# clean up after yourself
DBI::dbDisconnect(con)
class(new_lines) # this will be sf data frame
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
addPolylines(data = new_lines,
color = "red",
label = ~label)