Skip to content

Instantly share code, notes, and snippets.

@RAPLER
Forked from wch/DESCRIPTION
Created October 12, 2015 01:47
Show Gist options
  • Save RAPLER/ed8a7aba4122e0983610 to your computer and use it in GitHub Desktop.
Save RAPLER/ed8a7aba4122e0983610 to your computer and use it in GitHub Desktop.
Type: Shiny
Title: Client data and query string
License: MIT
Author: Winston Chang <[email protected]>
AuthorUrl: http://www.rstudio.com/
Tags: clientdata query-string
DisplayMode: Showcase
shinyServer(function(input, output, session) {
# Print out clientData, which is a reactiveValues object.
# This object is list-like, but it is not a list.
output$summary <- renderText({
# Find the names of all the keys in clientData
cnames <- names(session$clientData)
# Apply a function to all keys, to get corresponding values
allvalues <- lapply(cnames, function(name) {
item <- session$clientData[[name]]
if (is.list(item)) {
list_to_string(item, name)
} else {
paste(name, item, sep=" = ")
}
})
paste(allvalues, collapse = "\n")
})
# Parse the GET query string
output$queryText <- renderText({
query <- parseQueryString(session$clientData$url_search)
# Return a string with key-value pairs
paste(names(query), query, sep = "=", collapse=", ")
})
})
list_to_string <- function(obj, listname) {
if (is.null(names(obj))) {
paste(listname, "[[", seq_along(obj), "]] = ", obj,
sep = "", collapse = "\n")
} else {
paste(listname, "$", names(obj), " = ", obj,
sep = "", collapse = "\n")
}
}
shinyUI(fluidPage(
titlePanel("Client data and query string example"),
fluidRow(
column(8,
h3("session$clientdata values"),
verbatimTextOutput("summary"),
h3("Parsed URL query string"),
verbatimTextOutput("queryText")
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment