Skip to content
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

Closed
wants to merge 11 commits into from
16 changes: 13 additions & 3 deletions regions_www/m/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@
## Functions
source("pct_shiny_funs.R", local = T)

## Enable bookmarking at the server side
enableBookmarking(store = "server")
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("..", "..")
Expand Down Expand Up @@ -79,6 +78,17 @@ 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",
Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor

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

Dummy attempt at keeping URL short

Appears wrong on link, exposes our API key

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!

Copy link
Member

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:

usethis::edit_r_environ()
# add new line like KEY=XYZ
# Restart R
Sys.getenv("KEY")

Would that approach work on the server (if we ever did want to use API keys)?

Late response to a late response!

body = list(longUrl = paste0("http://www.pct.bike/?", res)), encode = "json"
)
return(httr::content(r)$id)
}
assignInNamespace("encodeShinySaveState", shortEncodeShinySaveState, ns="shiny")

# # # # # # # #
# shinyServer #
Expand Down