Skip to content

Instantly share code, notes, and snippets.

@tgirke
Last active July 26, 2019 13:37
Show Gist options
  • Save tgirke/8b9abe202c59bca72012ddeb79303e56 to your computer and use it in GitHub Desktop.
Save tgirke/8b9abe202c59bca72012ddeb79303e56 to your computer and use it in GitHub Desktop.
Shiny app example with reactive URL to query database with IDs
###############################################
## Shiny App Example with Reactive URL Input ##
###############################################
## Author: Yuzhu Duan & Thomas Girke
## Last update: 06-Jul-17
## Usage: save this file under name app.R to your shiny app directory, e.g. reactiveUrl
## Run in parent directory the following from within R:
## > library(shiny)
## > runApp(reactiveUrl)
## The toy database df1 in this example is created on the fly
## Example URL for testing: 127.0.0.1:5726/?symbol=g7
## Note, the value 5726 in URL might need to be changed depending on your session
## Query IDs in the URL can be g1 - g10
library(shiny); library(tidyverse)
## Toy database
df1 <- bind_cols(data_frame(ids1=paste0("g", 1:10)), as_data_frame(matrix(1:40, 10, 4, dimnames=list(1:10, paste0("CA", 1:4)))))
## User interface
ui <- fluidPage(
titlePanel("Query table/db by key value"),
sidebarPanel(
textInput("symbol", "Protein ID", "")
),
mainPanel(
dataTableOutput('mytabular')
)
)
## Server instructions
server <- function(input, output, session) {
observe({
query <- parseQueryString(session$clientData$url_search)
for (i in 1:(length(reactiveValuesToList(input)))) {
nameval = names(reactiveValuesToList(input)[i])
valuetoupdate = query[[nameval]]
if (!is.null(query[[nameval]])) {
if (is.na(as.numeric(valuetoupdate))) {
updateTextInput(session, nameval, value = valuetoupdate)
}
else {
updateTextInput(session, nameval, value = as.numeric(valuetoupdate))
}
}
}
output$mytabular <- renderDataTable({
slice(df1, match(valuetoupdate, df1$ids1))
})
})
}
## Run shiny app
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment