Skip to content

Conversation

@hadley
Copy link
Member

@hadley hadley commented Jul 4, 2021

Code original written by @jcheng5 for r-lib/gargle#157, but I think it makes the most sense for it to live in Shiny, given that y'all know the most about how to derive this url. I have zero clue how to test this. I made the following simple app, but then couldn't think how to deploy it + dev shiny in such away that it would run on shinyapps or RSC.

library(shiny)
ui <- fluidPage(
  textOutput("url")
)
server <- function(input, output, session) {
  output$url <- renderText(session$getUrl())
}
shinyApp(ui, server)

@hadley hadley requested review from jcheng5 and wch July 4, 2021 19:43
@jcheng5
Copy link
Member

jcheng5 commented Jul 28, 2021

There's actually (more reliable?) data available on clientData, as it's basically the browser telling the server what it's got in the URL bar:

initialValues[".clientdata_url_protocol"] = window.location.protocol;
initialValues[".clientdata_url_hostname"] = window.location.hostname;
initialValues[".clientdata_url_port"] = window.location.port;
initialValues[".clientdata_url_pathname"] = window.location.pathname;
// Send initial URL search (query string) and update it if it changes
initialValues[".clientdata_url_search"] = window.location.search;

library(shiny)

ui <- fluidPage(
  textOutput("url")
)

server <- function(input, output, session) {
  output$url <- renderText({
    paste0(
      session$clientData$url_protocol,
      "//",
      session$clientData$url_hostname,
      ":",
      session$clientData$url_port,
      session$clientData$url_pathname,
      if (nzchar(session$clientData$url_search)) "?",
      session$clientData$url_search
    )
  })
}

shinyApp(ui, server)

I think I did not use this for gargle because I needed the URL while handling an HTTP request (i.e. in the UI), not from the server function.

@CLAassistant
Copy link

CLAassistant commented Nov 8, 2023

CLA assistant check
All committers have signed the CLA.

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

Successfully merging this pull request may close these issues.

3 participants