Debian Codegraph @

R/history.R

raw
1
2 #' @include reactive-domains.R
3 NULL
4
5 #' @include reactives.R
6 NULL
7
8 #' Get the query string / hash component from the URL
9 #'
10 #' Two user friendly wrappers for getting the query string and the hash
11 #' component from the app's URL.
12 #'
13 #' These can be particularly useful if you want to display different content
14 #' depending on the values in the query string / hash (e.g. instead of basing
15 #' the conditional on an input or a calculated reactive, you can base it on the
16 #' query string). However, note that, if you're changing the query string / hash
17 #' programmatically from within the server code, you must use
18 #' `updateQueryString(_yourNewQueryString_, mode = "push")`. The default
19 #' `mode` for `updateQueryString` is `"replace"`, which doesn't
20 #' raise any events, so any observers or reactives that depend on it will
21 #' *not* get triggered. However, if you're changing the query string / hash
22 #' directly by typing directly in the browser and hitting enter, you don't have
23 #' to worry about this.
24 #'
25 #' @param session A Shiny session object.
26 #'
27 #' @return For `getQueryString`, a named list. For example, the query
28 #' string `?param1=value1&param2=value2` becomes `list(param1 =
29 #' value1, param2 = value2)`. For `getUrlHash`, a character vector with
30 #' the hash (including the leading `#` symbol).
31 #'
32 #' @seealso [updateQueryString()]
33 #'
34 #' @examples
35 #' ## Only run this example in interactive R sessions
36 #' if (interactive()) {
37 #'
38 #' ## App 1: getQueryString
39 #' ## Printing the value of the query string
40 #' ## (Use the back and forward buttons to see how the browser
41 #' ## keeps a record of each state)
42 #' shinyApp(
43 #' ui = fluidPage(
44 #' textInput("txt", "Enter new query string"),
45 #' helpText("Format: ?param1=val1&param2=val2"),
46 #' actionButton("go", "Update"),
47 #' hr(),
48 #' verbatimTextOutput("query")
49 #' ),
50 #' server = function(input, output, session) {
51 #' observeEvent(input$go, {
52 #' updateQueryString(input$txt, mode = "push")
53 #' })
54 #' output$query <- renderText({
55 #' query <- getQueryString()
56 #' queryText <- paste(names(query), query,
57 #' sep = "=", collapse=", ")
58 #' paste("Your query string is:\n", queryText)
59 #' })
60 #' }
61 #' )
62 #'
63 #' ## App 2: getUrlHash
64 #' ## Printing the value of the URL hash
65 #' ## (Use the back and forward buttons to see how the browser
66 #' ## keeps a record of each state)
67 #' shinyApp(
68 #' ui = fluidPage(
69 #' textInput("txt", "Enter new hash"),
70 #' helpText("Format: #hash"),
71 #' actionButton("go", "Update"),
72 #' hr(),
73 #' verbatimTextOutput("hash")
74 #' ),
75 #' server = function(input, output, session) {
76 #' observeEvent(input$go, {
77 #' updateQueryString(input$txt, mode = "push")
78 #' })
79 #' output$hash <- renderText({
80 #' hash <- getUrlHash()
81 #' paste("Your hash is:\n", hash)
82 #' })
83 #' }
84 #' )
85 #' }
86 #' @export
87 getQueryString <- function(session = getDefaultReactiveDomain()) {
88 parseQueryString(session$clientData$url_search)
89 }
90
91 #' @rdname getQueryString
92 #' @export
93 getUrlHash <- function(session = getDefaultReactiveDomain()) {
94 session$clientData$url_hash
95 }