Skip to content

Commit

Permalink
Use raw SQL to work around RMariaDB error with sp's. Return results a…
Browse files Browse the repository at this point in the history
…s tibble
  • Loading branch information
mbarneytu committed Oct 31, 2022
1 parent 92e9c62 commit 1b11f12
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
.RData
.Ruserdata
.Renviron
/data
/data
/rsconnect
config.yml
44 changes: 29 additions & 15 deletions R/mapSites.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
library(shiny)
library(leaflet)
library(DBI)
library(dplyr)

mapSitesUI <- function(id) {
tagList(
h4("Choose a site from the map:"),

tabsetPanel(
id = NS(id,"switcher"),
id = NS(id, "switcher"),
type = "hidden",

tabPanelBody(
value = "mapView",
h4("Choose a site from the map:"),
leafletOutput(NS(id, "map"))
),

Expand All @@ -25,27 +26,31 @@ mapSitesUI <- function(id) {
}

selectSites <- function() {
query <- "CALL sel_site_locations" # returns lat, lon, site_name, site_id
res <- dbGetQuery(pool, query)
# Using raw SQL to work around a weird SP error that happens only on shinyapps.io
# query <- "CALL sel_site_locations" # returns lat, lon, site_name, site_id
query <- "SELECT lat, lon, site_name, site_id FROM site"
res <- as_tibble(dbGetQuery(pool, query))
}

mapSitesServer <- function(id) {
moduleServer(id, function(input, output, session) {

# Load all sites into a dataframe
df <- selectSites()
# Load all sites into a tibble
sites <- selectSites()

# print(sites)

viewDataLink <- actionLink(
inputId = "foo", # This Id isn't used in this case.
inputId = "foo", # id isn't used in this case.
label = "View Data",
# Here we use Shiny.setInputValue() to set up a reactive input.
# (see: shiny.rstudio.com/articles/communicating-with-js.html)
# Here we use Shiny.setInputValue() to set up a reactive input from the
# server. (see: shiny.rstudio.com/articles/communicating-with-js.html)
# Note that the first argument must be preceded by our module's id.
onclick = 'Shiny.setInputValue(\"map1-link\", this.id, {priority: "event"})'
)

output$map <- renderLeaflet({
leaflet(data = df) %>%
leaflet(data = sites) %>%
addProviderTiles(providers$Esri.WorldTopoMap, group = "Topo") %>%
addProviderTiles(providers$Esri.WorldImagery, group = "Satellite") %>%

Expand All @@ -56,19 +61,28 @@ mapSitesServer <- function(id) {
fitBounds(-125.1, 49, -67.1, 25.2) %>%

addMarkers(
lng = df$lon, lat = df$lat,
label = df$site_name,
layerId = df$site_id,
lng = sites$lon, lat = sites$lat,
label = sites$site_name,
layerId = sites$site_id,
popup = paste(
"<b>", df$site_name, "</b></br>",
"<b>", sites$site_name, "</b></br>",
viewDataLink
)
)
})
observeEvent(input$link, {
myPoint <- reactiveVal(input$map_marker_click)

print(myPoint()$id)
# print(myPoint()$id)

mySite <- sites %>% filter(site_id == myPoint()$id)

updateTabsetPanel(inputId = "switcher", selected = "dataView")
output$data <- renderText(paste0("Data for ", mySite$site_name))
})

observeEvent(input$btnReturnToMap, {
updateTabsetPanel(inputId = "switcher", selected = "mapView")
})
})
}
6 changes: 3 additions & 3 deletions rsconnect/shinyapps.io/mbarney/HydroPortal-dev.dcf
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ account: mbarney
server: shinyapps.io
hostUrl: https://api.shinyapps.io/v1
appId: 7104218
bundleId: 6332318
bundleId: 6452350
url: https://mbarney.shinyapps.io/HydroPortal-dev/
when: 1663958918.9442
lastSyncTime: 1663958918.94421
when: 1666989839.63545
lastSyncTime: 1666989839.63546
asMultiple: FALSE
asStatic: FALSE

0 comments on commit 1b11f12

Please sign in to comment.