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

feat: override port via environment variable #4182

Open
JosiahParry opened this issue Feb 1, 2025 · 1 comment
Open

feat: override port via environment variable #4182

JosiahParry opened this issue Feb 1, 2025 · 1 comment

Comments

@JosiahParry
Copy link

My understanding is that the environment variable SHINY_PORT can be used to override the port used by a shiny app. However this doesn't seem to be the case and, instead, leads to a spurious warning when used.

See the following repro. It results in the following warning message:

Warning in runApp(x) :
  Shiny Server v0.3.4 or later is required; please upgrade!

Listening on http://127.0.0.1:9001
library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

  # Application title
  titlePanel("Old Faithful"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      sliderInput("bins",
        "Number of bins:",
        min = 1,
        max = 50,
        value = 30
      )
    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  output$distPlot <- renderPlot({
    # generate bins based on input$bins from ui.R
    x <- faithful[, 2]
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    # draw the histogram with the specified number of bins
    hist(x,
      breaks = bins, col = "darkgray", border = "white",
      xlab = "Waiting time to next eruption (in mins)",
      main = "Histogram of waiting times"
    )
  })
}

Sys.setenv("SHINY_PORT" = "3000")
# Run the application
shinyApp(ui = ui, server = server, options = list(port = 9001))
@gadenbuie
Copy link
Member

My understanding is that the environment variable SHINY_PORT can be used to override the port used by a shiny app.

Did you see this somewhere in the Shiny docs? It'd be useful to track down and to understand.

From a quick code search here, SHINY_PORT isn't used to set the port used by Shiny. It appears to be an environment variable set by Shiny Server for its own purposes and within the R package we use its presence as a signal that the app is being run by Shiny server.

shiny/R/server.R

Lines 510 to 514 in 55b37fd

# Returns TRUE if we're running in Shiny Server or other hosting environment,
# otherwise returns FALSE.
inShinyServer <- function() {
nzchar(Sys.getenv('SHINY_PORT'))
}

The error you encounter is expected from these lines

shiny/R/runapp.R

Lines 203 to 213 in 55b37fd

if (inShinyServer()) {
# If SHINY_PORT is set, we're running under Shiny Server. Check the version
# to make sure it is compatible. Older versions of Shiny Server don't set
# SHINY_SERVER_VERSION, those will return "" which is considered less than
# any valid version.
ver <- Sys.getenv('SHINY_SERVER_VERSION')
if (utils::compareVersion(ver, .shinyServerMinVersion) < 0) {
warning('Shiny Server v', .shinyServerMinVersion,
' or later is required; please upgrade!')
}
}

because Shiny thinks it's running inside Shiny Server and does a version check.

I think there's clearly room for improvement, but it could be tricky to handle in a way that doesn't break assumptions across Shiny packages and services.

Personally, I'd use a different environment variable passed directly to shinyApp() (untested, port might need to be coerced to int/numeric):

shinyApp(ui = ui, server = server, options = list(port = Sys.setenv("APP_PORT", 9001))

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

2 participants