问题
I have two user groups in a DB. I want to query the DB to show a checkboxGroup in a Shiny app, with only one of the groups, depending on a URL query parameter.
For example, with a URL query parameters like this:
http://127.0.0.1:4014/?groupId=1
ideally, I would like to get that groupId value and use it to filter the user group:
res <- dbSendQuery(con, paste('SELECT user_id, user_name
FROM info i , enrolment e
WHERE i.user_id = e.fk_user_id
AND e.fk_group_id = ', groupId))
dataf <- dbFetch(res)
userlist <- as.list(setNames(dataf$user_id, dataf$user_name))
and then show the content of that userlist within Shiny, as a checkboxGroup, like so:
ui <- fluidPage( column(3, checkboxGroupInput("users",
label = h3("User Names"),
choices = userlist)))
Is it possible to pass such URL query parameters -as groupId in my example- to a Shiny app? (My code is working OK if I hard-code the groupId)
回答1:
http://shiny.rstudio.com/articles/client-data.html
Here's what you're looking for:
groupId <- parseQueryString(session$clientData$url_search)
Just remember to add
shinyServer(function(input, output, session)
It can look something like this at the end of the day:
ui <- bootstrapPage(
h3("groupId"),
verbatimTextOutput("queryText")
)
server <- function(input, output, session) {
output$queryText <- renderText({
query <- parseQueryString(session$clientData$url_search)
paste(query, sep = "", collapse=", ")
})
}
shinyApp(ui = ui, server = server)
Looking at your gist, you need to put your database objects/variables in the server part.
server(function(input, output, session) {
source("keys.R")
con <- dbConnect( MySQL(), user=login, password=pass, db=database, host=host)
Next, still in the server part, I would put, in either a observer or reactive expression, your dbGetQuery combined with the groupId parameter, and store the output.
This way you can apply that output to a checkboxGroupInput in the UI.
来源:https://stackoverflow.com/questions/35784667/how-to-use-query-parameters-in-shiny