-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Storing state of the app as a bookmark #659
Changes from all commits
28f0885
58606ee
38e8fdd
b7f7ec7
9494e03
6da322f
7bb66c9
ef48e28
95c3b84
0dd87b6
fdd151b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,9 +22,11 @@ | |
## Functions | ||
source("pct_shiny_funs.R", local = T) | ||
|
||
enableBookmarking(store = "url") | ||
|
||
## Packages (Only regularly used packages are loaded into the global space, the others must be installed but are used with the package prefix, e.g. DT::) | ||
available_locally_pkgs <- c("shiny", "leaflet", "sp") | ||
must_be_installed_pkgs <- c("rgdal", "rgeos", "shinyjs") | ||
must_be_installed_pkgs <- c("rgdal", "rgeos", "shinyjs", "httr") | ||
|
||
## Path directories to load data (expect regional data as a sibling of interface_root) | ||
interface_root <- file.path("..", "..") | ||
|
@@ -76,12 +78,41 @@ lsoa_legend_df <- data.frame( | |
labels = c("1-9", "10-49", "50-99", "100-249", | ||
"250-499", "500-999", "1000-1999", "2000+") | ||
) | ||
oldEncodeShinySaveState <- getFromNamespace("encodeShinySaveState", "shiny") | ||
|
||
shortEncodeShinySaveState <- function(state){ | ||
res <- oldEncodeShinySaveState(state) | ||
r <- httr::POST( | ||
"https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyBLGLnQojQm2WQu9Urri2mmVTQolrLuJBc", | ||
body = list(longUrl = paste0("http://www.pct.bike/?", res)), encode = "json" | ||
) | ||
return(httr::content(r)$id) | ||
} | ||
assignInNamespace("encodeShinySaveState", shortEncodeShinySaveState, ns="shiny") | ||
|
||
# # # # # # # # | ||
# shinyServer # | ||
# # # # # # # # | ||
shinyServer(function(input, output, session) { | ||
|
||
# Save current region in a state variable | ||
onBookmark(function(state) { | ||
state$values$current_region <- region$current | ||
}) | ||
|
||
|
||
onRestore(function(state) { | ||
# Restore state's region into a region variable | ||
region$state_region <- state$values$current_region | ||
# Restore map's location and zoom | ||
region$state_lng <- state$input$map_center$lng | ||
region$state_lat <- state$input$map_center$lat | ||
region$state_mzoom <- state$input$map_zoom | ||
|
||
|
||
}) | ||
|
||
|
||
input_purpose <- reactive({ | ||
if(is.null(input$purpose)) { | ||
"commute" | ||
|
@@ -222,7 +253,8 @@ shinyServer(function(input, output, session) { | |
############## | ||
|
||
## Create region, to_plot and (for persistent geographical values) helper | ||
region <- reactiveValues(current = NA, data_dir = NA, geography = NA, repopulate_region = F, purposes_present = NA) | ||
region <- reactiveValues(current = NA, data_dir = NA, geography = NA, repopulate_region = F, purposes_present = NA, | ||
state_region = NA, state_lat = NA, state_lng = NA, state_mzoom = NA) | ||
to_plot <- NULL | ||
helper <- NULL | ||
helper$e_lat_lng <- "" | ||
|
@@ -252,11 +284,18 @@ shinyServer(function(input, output, session) { | |
|
||
# Identify region from URL or use a default | ||
if (is.na(region$current)) { | ||
query <- parseQueryString(session$clientData$url_search) | ||
region$current <- if (isTRUE(query[['r']] %in% regions$region_name)) { | ||
query[['r']] | ||
} else { | ||
"isle-of-wight" | ||
|
||
if(!is.null(region$state_region) && !is.na(region$state_region)){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we need to do both There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about if (is.character(region$state_region)){
region$current <- region$state_region
} |
||
# Restor's state_region to region's current var | ||
region$current <- region$state_region | ||
} | ||
else{ | ||
query <- parseQueryString(session$clientData$url_search) | ||
region$current <- if (isTRUE(query[['r']] %in% regions$region_name)) { | ||
query[['r']] | ||
} else { | ||
"isle-of-wight" | ||
} | ||
} | ||
} | ||
|
||
|
@@ -359,6 +398,18 @@ shinyServer(function(input, output, session) { | |
}, priority = 3) | ||
|
||
|
||
# Once all the variables have been initialized - all observe blocks have run, reset the map view | ||
# (including zoom from the saved state of the app) | ||
onRestored(function(state) { | ||
|
||
leafletProxy("map") %>% setView(., | ||
lng = region$state_lng, | ||
lat = region$state_lat, | ||
zoom = region$state_mzoom | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be done in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it can't be done there. |
||
}) | ||
|
||
|
||
# Only requred to run if the region changes (as that affects purpose) or the purpose changes (as that affects geographies) | ||
observe({ | ||
shinyjs::showElement(id = "loading") | ||
|
@@ -1091,4 +1142,5 @@ shinyServer(function(input, output, session) { | |
|
||
includeHTML(file.path("..", "..", "non_www", "tabs", input_purpose(), "download_national.html")) | ||
}) | ||
|
||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I generally suggest saving API keys in the
.Renviron
file, as described here: https://csgillespie.github.io/efficientR/set-up.html#renviron in this case. Is there no issue with rate limits? Maybe no issue sharing this with the world in that case... Another compromise, in this case between the + of short urls and the + of reducing dependencies. In this case I'd lean slightly towards reducing dependencies although can understand the desire to keep URLs short.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nudge on this @nikolai-b - suggest not publicising the key in the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for spotting @Robinlovelace ! Very late response sorry I just saw this. You can see the commit message is
This API key was locked down to only work from pct.bike and I disabled a few days after posting it here (was just so others could try it) but it just shows what we could do...
Since then the whole goo shortner has been deprecated so to be completely clear this is not the approach I'd suggest it was just to show it was possible to hack the URL to make it shorter, I think using
assignInNamespace
is probably a bad idea!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never come across
assignInMyNamespace
. The standard way for users to handle API keys now is:Would that approach work on the server (if we ever did want to use API keys)?
Late response to a late response!