Created
January 28, 2021 01:45
-
-
Save ryanbthomas/6a49704a0cca18da88084e886bd01d27 to your computer and use it in GitHub Desktop.
PoC of Custom Bookmarking in shiny. This example uses an in memory list to store data, but this could be replaced by calls to web API for caching data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# This is a Shiny web application. You can run the application by clicking | |
# the 'Run App' button above. | |
# | |
# Find out more about building applications with Shiny here: | |
# | |
# http://shiny.rstudio.com/ | |
# | |
library(shiny) | |
library(waiter) | |
runid <- 0 | |
runs <- list() | |
store_run_data <- function(id, x) { | |
new_data <- list(x) | |
names(new_data) <- as.character(id) | |
runs <<- c(runs, new_data) | |
} | |
fetch_run_data <- function(id) { | |
runs[[as.character(id)]] | |
} | |
# Define UI for application that draws a histogram | |
ui <- fluidPage( | |
use_waiter(), | |
# Application title | |
titlePanel("Old Faithful Geyser Data"), | |
# Sidebar with a slider input for number of bins | |
sidebarLayout( | |
sidebarPanel( | |
sliderInput("bins_input", | |
"Number of bins:", | |
min = 1, | |
max = 50, | |
value = 30), | |
actionLink('bookmark', 'Bookmark application', icon = icon('share-alt')) | |
), | |
# Show a plot of the generated distribution | |
mainPanel( | |
plotOutput("distPlot") | |
) | |
) | |
) | |
# Define server logic required to draw a histogram | |
server <- function(input, output, session) { | |
observeEvent(input$bookmark, { | |
# snapshot application state | |
snapshot_state = reactiveValuesToList(input) | |
# only bookmark inputs | |
snapshot_state = snapshot_state[stringr::str_detect(names(snapshot_state), '_input')] | |
# add the current system time to get a unique hash | |
snapshot_state[['time']] = Sys.time() | |
# create a state id | |
runid <<- runid + 1 | |
store_run_data(runid, snapshot_state) | |
# update the query string for the user | |
updateQueryString(paste0('?run=', runid), mode = 'push') | |
}) | |
observeEvent(getQueryString(), once = TRUE, { | |
# get query string | |
waiter_show( | |
html = spin_fading_circles() | |
) | |
current_run <- getQueryString() | |
if(length(current_run) > 0) { | |
# get bookmark from database table | |
bookmark_list <- fetch_run_data(current_run$run) | |
# simulate average query time | |
#Sys.sleep(1) | |
# coerce JSON to R list | |
#bookmark_list = jsonlite::fromJSON(bookmark$STATE_DATA) | |
for(id in names(bookmark_list)) { | |
value = bookmark_list[[id]] | |
if(length(value) == 0) { | |
html_value = '' | |
} else { | |
html_value = value | |
} | |
# update inputs in UI | |
session$sendInputMessage(id, list(value = html_value)) | |
} | |
} | |
waiter_hide() | |
}) | |
output$distPlot <- renderPlot({ | |
# generate bins based on input$bins from ui.R | |
x <- faithful[, 2] | |
bins <- seq(min(x), max(x), length.out = input$bins_input + 1) | |
# draw the histogram with the specified number of bins | |
hist(x, breaks = bins, col = 'darkgray', border = 'white') | |
}) | |
} | |
# Run the application | |
shinyApp(ui = ui, server = server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment