1

I am running a shiny app locally on RStudio/Posit workbench in the browser.

The app just prints its current URL

library(shiny)
ui <- basicPage(
  verbatimTextOutput("url")
)

server <- function(input, output, session){
  output$url <- renderText({
    cd <- session$clientData
    url <-  paste0(cd$url_protocol, "//", cd$url_hostname, cd$url_pathname)
    cat(sprintf("Running on\n  %s\n", url))
    url
  })
}
shinyApp(ui, server, options = list(port = 4218))

For example when I run it it reads: https://POSIT_URL/s/46da136e42a33f0a920f9/p/64dab64d/. I am interested in the last bit 64dab64d and was wondering how this is created. It depends on the port number and seems to be consistent to my session. Is it possible to generate/predict this number before the app is run?

I suspect it is a hashed value but I couldnt find the right inputs/hash function.

David
  • 9,216
  • 4
  • 45
  • 78

2 Answers2

1

So, no need to start and stop the app.

From R you can call:

rstudioapi::translateLocalUrl(
    url = "http://localhost:8765",
    absolute = TRUE
    )

This will return the full externally accessible URL that maps to your localhost address, including the hashed port part.

In newer builds of posit workbench there is also a command line tool available that you can call from vscode or jupyterlab: /usr/lib/rstudio-server/bin/rserver-url -l <port number>

M Malcher
  • 26
  • 2
0

Not entirely an answer but a brute-force workaround: start the app, print the URL and stop the app again. In a function it looks like this

# retrieves the URL for a shiny app
get_full_shiny_url <- function(port) {
  server <- function(input, output, session){
    shiny::observe(cat(paste0(
      session$clientData$url_protocol, "//", session$clientData$url_hostname,
      session$clientData$url_pathname, "\n"
    )))
    shiny::stopApp()
  }
  
  capture.output(shiny::shinyApp(shiny::basicPage(), server,
                                 options = list(port = port)))
}
get_full_shiny_url(4812)
#> https://POSIT_URL/s/46da136e42a33f0a920f9/p/64dab64d/
David
  • 9,216
  • 4
  • 45
  • 78