how to print port number from shiny::runApp()

Hello,

I'm running shiny::runApp() with random port assignment. I can't figure out how to print the port number shiny was assigned to. Hoping someone can help me.

This is how I wrote my app.

app.R

library(shiny)

# Define UI for app that draws a histogram ----
ui <- fluidPage(
  
  # App title ----
  titlePanel("Hello Shiny!")
  )

server <- function(input, output) {
}
shinyApp(ui, server)

and then I ran it with:
R -e "shiny::runApp()"

How do I get the port number the shiny app is listening to?
Thank you!

Updated: to accurately show how I called runApp()

Hello,

By default shiny runs on a random port. You can use the port argument in the runApp function to specify one, or set it in options with the shiny.port option.

You can get the current port the client is using in the session$clientData$url_port object. So in your example:

server <- function(input, output, session) {
  print(isolate(session$clientData$url_port))
}

This would print the port the user is using to the console, but you can save it in a variable if you need to use it in the app.

Hope either of those help!

Thank you very much for your reply.

I was able to get the port number printed out to a file if I'm running the app in rstudio. However, I wasn't if I ran it through the command line.

Apologized for not being precise before, but I actually run the app in the command line like this:
R -e "shiny::runApp()"

and in the server function, I generated the file with port number:

server <- function(input, output, session) {
  write.table(isolate(session$clientData$url_port),file=“port.txt”)
}

and in this way, there is no file generated.

With the port number, I am trying to automatically open firefox with the correct port number.

@olechnwin Could you set a fixed port number?
options(shiny.port = 12345) at the top of the file would let you run it and always send the user to mydomain.com:12345. You could also try to run shiny::runApp(launch.browser = TRUE)

@EliMiller thanks again for your help!

I'm trying to get random port so that different users can run the same app at the same time with different data.

I tried R -e "shiny::runApp(launch.browser=TRUE)", unfortunately I got this error:

Error in utils::browseURL(appUrl) :
  'browser' must be a non-empty character string
Calls: <Anonymous> -> <Anonymous>
Execution halted

and I'm not sure how to fix this.
I guess if I have to, I can specify different port number every time the app is ran.

I think Shiny Server or Connect might be the way to go for having multiple users connect to your application on the same machine. You can install an open-source shiny server and that will handle all of the concurrent sessions for you.

Thank for your suggestions. I'm trying to avoid having to put my app online. There are lots of hoops to jump through to get permission. Plus, the users will be people inside my organization.

That shouldn't be an issue. You could have the hostname set to localhost or set it to a non-routable IP address so only other devices on your network could access it.

@EliMiller, Thanks for your suggestions. I'll look into open-source shiny server.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.