rshinyrsconnect

RStudio/Posit workbench how does it create the URL path


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.


Solution

  • 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>