Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Allow to pass URL parameters to runApp() and/or shinyApp() #3546

Open
ColinFay opened this issue Nov 9, 2021 · 3 comments

Comments

@ColinFay
Copy link
Contributor

ColinFay commented Nov 9, 2021

Context

I have a shiny app that behaves differently based on url parameters passed to it

Pain point

When building this app, every time I relaunch the app, I need to refresh the browser with the url parameters included (either manually, or via a development widget built just for that purpose).

Feature Request

I'd love to be able to do something like:

runApp( urlParams = c(x = "xyz", a = "abcd") )

From reading the source code, it seems that this can be done here in runApp:

appUrl <- paste("http://", browseHost, ":", port, sep = "")

with something like

appUrl <- paste("http://", browseHost, ":", port, sep = "")
if (!is.null(urlParams){
appUrl <- paste(
  appUrl, 
  "?",
	paste(
      names(urlParams),
      urlParams, 
      sep = "=", 
      collapse = "&"
    )
}

Let me know what you think, happy to do a PR!

Edit: this might be better to have it inside shinyApp() instead of runApp()

@ColinFay ColinFay changed the title [Feature Request] Allow to pass URL parameters to runApp() [Feature Request] Allow to pass URL parameters to runApp() or shinyApp() Nov 9, 2021
@ColinFay ColinFay changed the title [Feature Request] Allow to pass URL parameters to runApp() or shinyApp() [Feature Request] Allow to pass URL parameters to runApp() and/or shinyApp() Nov 9, 2021
@jcheng5
Copy link
Member

jcheng5 commented Nov 16, 2021

Hmmm, I was going to suggest shiny::devmode(TRUE) which enables autoreload (browser automatically reloads when you save .R file)... but I assume you're using golem and probably autoreload doesn't work with packages?

@ismirsehregal
Copy link
Contributor

I recently gave a related answer here.

I think using updateQueryString would be an option:

library(shiny)

ui <- fluidPage(
  selectInput("selection", "Selection", choices = LETTERS[1:3], selected = "A")
)

server <- function(input, output, session) {
  observeEvent(input$selection, {
    newURI <- paste0(
      session$clientData$url_protocol,
      "//",
      session$clientData$url_hostname,
      ":",
      session$clientData$url_port,
      session$clientData$url_pathname,
      "#",
      input$selection
    )
    updateQueryString(newURI, mode = "replace", session)
  })
}

shinyApp(ui, server)

@ismirsehregal
Copy link
Contributor

ismirsehregal commented Jan 23, 2022

Another option is to pass a custom function to launch.browser - from ?runApp:

launch.browser
If true, the system's default web browser will be launched automatically after the app is started. Defaults to true in interactive sessions only. This* value of this parameter can also be a function to call with the application's URL.

*typo in the docs

An example:

library(shiny)

ui <- fluidPage(uiOutput("queryString"))

server <- function(input, output, session) {
  output$queryString <- renderUI({
    HTML(paste0(
      parseQueryString(session$clientData$url_search),
      collapse = "<br>"
    ))
  })
}

app <- shinyApp(ui, server)

runApp(
  appDir = app,
  port = 3838,
  launch.browser = function(appUrl) {
    url <- paste0(appUrl, "/?param1=val1&param2=val2#hash")
    invisible(.Call("rs_shinyviewer", url, getwd(), "browser", NULL, PACKAGE = "(embedding)"))
    # utils::browseURL(url) # alternative
  },
  host = "0.0.0.0"
)

Nevertheless, a more user-friendly way to do this would be highly appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants