How to Pass Parameters to a Shiny App via Url

How do you pass parameters to a shiny app via URL

You'd have to update the input yourself when the app initializes based on the URL. You would use the session$clientData$url_search variable to get the query parameters. Here's an example, you can easily expand this into your needs

library(shiny)

shinyApp(
ui = fluidPage(
textInput("text", "Text", "")
),
server = function(input, output, session) {
observe({
query <- parseQueryString(session$clientData$url_search)
if (!is.null(query[['text']])) {
updateTextInput(session, "text", value = query[['text']])
}
})
}
)

how pass multiple parameters to shiny app via URL to updateSelectInput?

Shiny App: How to Pass Multiple Tokens/Parameters through URL

The standard delimeter for tokens passed through url to shiny app is the & symbol.

Example shiny app code:

server <- function(input, output, session) {
observe({
query <- parseQueryString(session$clientData$url_search)
if (!is.null(query[['paramA']])) {
updateTextInput(session, "InputLabel_A", value = query[['paramA']])
}
if (!is.null(query[['paramB']])) {
updateTextInput(session, "InputLabel_A", value = query[['paramB']])
}
})
# ... R code that makes your app produce output ..
}

Coresponding URL example:
http://localhost.com/?paramA=hello&?paramB=world

Reference: parseQueryString Docs

Although it seems as though your question is how to parse string tokens from a single passed paramater, no? Decided to include this answer anyway because this was the first result on google for how to pass multiple params through url to shiny app and I'm sure others will find useful.

Constructing URL search parameters in Shiny app?

Your UI is good, the issue with the updateSelectInput, use selected rather than value and include choices.

Minimal working example:

library(shiny)

facilities <- seq(1:5)

ui <- fluidPage(

selectInput("selected_facility", "Select facility", choices = facilities)

)

server <- function(input, output, session) {

observe({

#Get URL query
query <- parseQueryString(session$clientData$url_search)

#Ignore if the URL query is null
if (!is.null(query[['selected_facility']])) {

#Update the select input
updateSelectInput(session, "selected_facility", selected = query[['selected_facility']], choices = facilities)

}

})

}

shinyApp(ui, server)

To test, run your shiny app, click 'Open in Browser' and append your query to the URL, e.g.

127.0.0.1:6054/?selected_facility=4

Pass URL parameter to shiny plot function

Try this. Note that n is defined as a per-session global variable, and notice the global assignment operator <<-

library(shiny)

shinyApp(
ui = fluidPage(
mainPanel(
plotOutput("plot")
)
),
server = function(input, output, session) {
n <- 5
observe({
query <- parseQueryString(session$clientData$url_search)
if (!is.null(query[['text']])) {
n <<- query[['text']]
}
})
output$plot <- renderPlot({
# Add a little noise to the cars data
plot(cars[sample(nrow(cars), n), ])
})
}
)

make shiny app UI dependent on URL parameter

Four options below:

  1. Dependent on URL, renderUI
  2. Dependent on window width, renderUI
  3. Dependent on window width, conditionalPanel (does not work properly)
  4. Dependent on window width, shinyjs

Option 1: Dependent on URL, renderUI

It is possible to make it dependent on the URL, see for example here. Here is an example implementation:

library(shinyWidgets)
library(shiny)

nazwy=c('Warszawa', 'Krakow', 'Gdansk')

ui<-fluidPage(
uiOutput('myUI')
)


server <- function(input,output,session) {

output$myUI <- renderUI({
query <- parseQueryString(session$clientData$url_search)
if (!is.null(query$mini)) {
if(query$mini==TRUE)
{
pickerInput(inputId = "miasto", choices = nazwy,multiple = TRUE)
}
else
{
checkboxGroupInput('miasto', 'choose: ',nazwy)
}
}
else
{
checkboxGroupInput('miasto', 'choose: ',nazwy)
}
})
}

shinyApp(ui,server,options=list(port=7777))

try both http://127.0.0.1:7777/ and http://127.0.0.1:7777/?mini=TRUE.


Option 2: Dependent on window width, renderUI

If you would want to make it dependent on the window width, here is a possible solution:

library(shinyWidgets)
library(shiny)

nazwy=c('Warszawa', 'Krakow', 'Gdansk')

ui<-fluidPage(
tags$head(tags$script('
var dimension = [0, 0];
$(document).on("shiny:connected", function(e) {
dimension[0] = window.innerWidth;
dimension[1] = window.innerHeight;
Shiny.onInputChange("dimension", dimension);
});
$(window).resize(function(e) {
dimension[0] = window.innerWidth;
dimension[1] = window.innerHeight;
Shiny.onInputChange("dimension", dimension);
});
')),
uiOutput('myUI')
)


server <- function(input,output) {
output$myUI <- renderUI({
req(input$dimension)
if (input$dimension[1]<800) {
pickerInput(inputId = "miasto", choices = nazwy,
selected=isolate(selected_cities()),multiple = TRUE)
} else {
checkboxGroupInput('miasto', 'choose: ',
choices=nazwy, selected=isolate(selected_cities()))
}
})

#store selected value to pass on resizing
selected_cities<-reactive(input$miasto)

}

shinyApp(ui,server)

option 3: Window width + conditionalPanel.
NOTE: Does not work as expected.

   library(shinyWidgets)
library(shiny)

nazwy=c('Warszawa', 'Krakow', 'Gdansk')

ui<-fluidPage(
tags$head(tags$script('var dimension = [0, 0];
$(document).on("shiny:connected", function(e) {
dimension[0] = window.innerWidth;
dimension[1] = window.innerHeight;
Shiny.onInputChange("dimension", dimension);
});
$(window).resize(function(e) {
dimension[0] = window.innerWidth;
dimension[1] = window.innerHeight;
Shiny.onInputChange("dimension", dimension);
});
')),
conditionalPanel(condition = 'input.dimension[0]>1000',
pickerInput(inputId = "miasto", choices = nazwy,multiple = TRUE)
),
conditionalPanel(condition = 'input.dimension[0]<=1000',
checkboxGroupInput('miasto', 'choose: ',nazwy))
)


server <- function(input,output) {

}

shinyApp(ui,server)

Option 4: window width + shinyjs

library(shinyWidgets)
library(shiny)
library(shinyjs)

nazwy=c('Warszawa', 'Krakow', 'Gdansk')

ui<-fluidPage(
tags$head(tags$script('var dimension = [0, 0];
$(document).on("shiny:connected", function(e) {
dimension[0] = window.innerWidth;
dimension[1] = window.innerHeight;
Shiny.onInputChange("dimension", dimension);
});
$(window).resize(function(e) {
dimension[0] = window.innerWidth;
dimension[1] = window.innerHeight;
Shiny.onInputChange("dimension", dimension);
});
')),
div(id='div1',pickerInput(inputId = "miasto", choices = nazwy,multiple = TRUE)),
shinyjs::hidden(div(id='div2',checkboxGroupInput('miasto', 'choose: ',nazwy))),
useShinyjs()
)


server <- function(input,output) {
observeEvent(input$dimension,ignoreNULL=T,{
if (input$dimension[1]>1000) {
shinyjs::show('div1')
shinyjs::hide('div2')
} else {
shinyjs::show('div2')
shinyjs::hide('div1')
}
})
}

shinyApp(ui,server)

How to use query parameters in Shiny

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.



Related Topics



Leave a reply



Submit