| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/bookmark-state.R |
| 3 |
\name{updateQueryString} |
| 4 |
\alias{updateQueryString} |
| 5 |
\title{Update URL in browser's location bar} |
| 6 |
\usage{ |
| 7 |
updateQueryString( |
| 8 |
queryString, |
| 9 |
mode = c("replace", "push"), |
| 10 |
session = getDefaultReactiveDomain() |
| 11 |
) |
| 12 |
} |
| 13 |
\arguments{ |
| 14 |
\item{queryString}{The new query string to show in the location bar.} |
| 15 |
|
| 16 |
\item{mode}{When the query string is updated, should the current history |
| 17 |
entry be replaced (default), or should a new history entry be pushed onto |
| 18 |
the history stack? The former should only be used in a live bookmarking |
| 19 |
context. The latter is useful if you want to navigate between states using |
| 20 |
the browser's back and forward buttons. See Examples.} |
| 21 |
|
| 22 |
\item{session}{A Shiny session object.} |
| 23 |
} |
| 24 |
\description{ |
| 25 |
This function updates the client browser's query string in the location bar. |
| 26 |
It typically is called from an observer. Note that this will not work in |
| 27 |
Internet Explorer 9 and below. |
| 28 |
} |
| 29 |
\details{ |
| 30 |
For \code{mode = "push"}, only three updates are currently allowed: |
| 31 |
\enumerate{ |
| 32 |
\item the query string (format: \code{?param1=val1¶m2=val2}) |
| 33 |
\item the hash (format: \verb{#hash}) |
| 34 |
\item both the query string and the hash |
| 35 |
(format: \code{?param1=val1¶m2=val2#hash}) |
| 36 |
} |
| 37 |
|
| 38 |
In other words, if \code{mode = "push"}, the \code{queryString} must start |
| 39 |
with either \verb{?} or with \verb{#}. |
| 40 |
|
| 41 |
A technical curiosity: under the hood, this function is calling the HTML5 |
| 42 |
history API (which is where the names for the \code{mode} argument come from). |
| 43 |
When \code{mode = "replace"}, the function called is |
| 44 |
\code{window.history.replaceState(null, null, queryString)}. |
| 45 |
When \code{mode = "push"}, the function called is |
| 46 |
\code{window.history.pushState(null, null, queryString)}. |
| 47 |
} |
| 48 |
\examples{ |
| 49 |
## Only run these examples in interactive sessions |
| 50 |
if (interactive()) { |
| 51 |
|
| 52 |
## App 1: Doing "live" bookmarking |
| 53 |
## Update the browser's location bar every time an input changes. |
| 54 |
## This should not be used with enableBookmarking("server"), |
| 55 |
## because that would create a new saved state on disk every time |
| 56 |
## the user changes an input. |
| 57 |
enableBookmarking("url") |
| 58 |
shinyApp( |
| 59 |
ui = function(req) { |
| 60 |
fluidPage( |
| 61 |
textInput("txt", "Text"), |
| 62 |
checkboxInput("chk", "Checkbox") |
| 63 |
) |
| 64 |
}, |
| 65 |
server = function(input, output, session) { |
| 66 |
observe({ |
| 67 |
# Trigger this observer every time an input changes |
| 68 |
reactiveValuesToList(input) |
| 69 |
session$doBookmark() |
| 70 |
}) |
| 71 |
onBookmarked(function(url) { |
| 72 |
updateQueryString(url) |
| 73 |
}) |
| 74 |
} |
| 75 |
) |
| 76 |
|
| 77 |
## App 2: Printing the value of the query string |
| 78 |
## (Use the back and forward buttons to see how the browser |
| 79 |
## keeps a record of each state) |
| 80 |
shinyApp( |
| 81 |
ui = fluidPage( |
| 82 |
textInput("txt", "Enter new query string"), |
| 83 |
helpText("Format: ?param1=val1¶m2=val2"), |
| 84 |
actionButton("go", "Update"), |
| 85 |
hr(), |
| 86 |
verbatimTextOutput("query") |
| 87 |
), |
| 88 |
server = function(input, output, session) { |
| 89 |
observeEvent(input$go, { |
| 90 |
updateQueryString(input$txt, mode = "push") |
| 91 |
}) |
| 92 |
output$query <- renderText({ |
| 93 |
query <- getQueryString() |
| 94 |
queryText <- paste(names(query), query, |
| 95 |
sep = "=", collapse=", ") |
| 96 |
paste("Your query string is:\n", queryText) |
| 97 |
}) |
| 98 |
} |
| 99 |
) |
| 100 |
} |
| 101 |
} |
| 102 |
\seealso{ |
| 103 |
\code{\link[=enableBookmarking]{enableBookmarking()}}, \code{\link[=getQueryString]{getQueryString()}} |
| 104 |
} |