Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nteetor committed Aug 21, 2019
0 parents commit c8929d5
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
^.*\.Rproj$
^\.Rproj\.user$
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
.DS_Store
13 changes: 13 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Package: blaze
Type: Package
Title: Observe URIs and Routes with 'shiny'
Version: 0.0.1
Author: c(
person("Nathan", "Teetor", email = "[email protected]", role = c("aut", "cre")))
Maintainer: Nathan Teetor <[email protected]>
Description: Simulate paging within a 'shiny' application by observering
and reacting to URI changes.
License: MIT
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.1.1
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(getRoute)
export(observeRoute)
export(pushRoute)
importFrom(shiny,getDefaultReactiveDomain)
10 changes: 10 additions & 0 deletions R/blaze.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#' @importFrom shiny getDefaultReactiveDomain
NULL

#' Route paths and URIs
#'
#' The `blaze` package makes simulating different routes within a shiny
#' application possible. The approach makes use of the `#`, hash, portition of
#' the uri to prevent the application from truly reloading.
#'
"_PACKAGE"
42 changes: 42 additions & 0 deletions R/observers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#' Observe routes
#'
#' Observe a route path and run a handler expression. When the route specified
#' by `path` is browsed to `handler` will be run.
#'
#' @param path A character string specifying a route path, by default `path` is
#' treated as a regular expression, see `regex`.
#'
#' @param handler An expression or function to call when the uri matches `path`.
#'
#' @param env The parent environment of `handler`, defaults to the calling
#' environment.
#'
#' @param quoted One of `TRUE` or `FALSE` specifying if `handler` is a quoted
#' expression. If `TRUE`, the expression must be quoted with `quote()`.
#'
#' @param regex One of `TRUE` or `FALSE` specifying if `path` is treated as a
#' regular expression, defaults to `TRUE`.
#'
#' @param domain A reactive context, defaults to
#' `shiny::getDefaultReactiveDomain()`.
#'
#' @export
observeRoute <- function(path, handler, env = parent.frame(), quoted = FALSE,
regex = TRUE,
domain = getDefaultReactiveDomain()) {
h_expr <- shiny::exprToFunction(handler, env = env, quoted = quoted)

o <- observe({
req(domain$clientData$url_hash)

url <- sub("^#", "", domain$clientData$url_hash)

is_match <- grepl(path, url, fixed = !regex)

req(is_match)

h_expr()
}, domain = domain)

invisible(o)
}
29 changes: 29 additions & 0 deletions R/route.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#' Route utilities
#'
#' Push a route or get the current application route.
#'
#' @param path A character string specifying a new route.
#'
#' @param session A reactive context, defaults to
#' `shiny::getDefaultReactiveDomain()`.
#'
#' @export
pushRoute <- function(path, session = getDefaultReactiveDomain()) {
if (!grepl("^/", path)) {
path <- paste0("/", path)
}

hash <- paste0("#", path)

session$sendCustomMessage("blaze:push", list(
hash = hash
))
}

#' @rdname pushRoute
#' @export
getRoute <- function(session = getDefaultReactiveDomain()) {
hash <- shiny::isolate(session$clientData$url_hash)

sub("^#", "", hash)
}
3 changes: 3 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.onLoad <- function(pkg, lib) {
shiny::addResourcePath("blaze", system.file("www", package = "blaze"))
}
21 changes: 21 additions & 0 deletions blaze.Rproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: Sweave
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
10 changes: 10 additions & 0 deletions inst/www/js/blaze.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
window.addEventListener("DOMContentLoaded", function(e) {
Shiny.addCustomMessageHandler("blaze:push", function(msg) {
if (msg.hash === undefined || msg.hash === null) {
return;
}

window.history.pushState(null, null, msg.hash);
window.dispatchEvent(new HashChangeEvent("hashchange"));
});
});
12 changes: 12 additions & 0 deletions man/blaze-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions man/observeRoute.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions man/pushRoute.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c8929d5

Please sign in to comment.