mirror of
https://github.com/rstudio/shiny.git
synced 2026-01-10 23:48:01 -05:00
Compare commits
7 Commits
bindAfterR
...
randomPort
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cc13ba80cc | ||
|
|
bb3f324b56 | ||
|
|
84d53e57e4 | ||
|
|
f8d048a742 | ||
|
|
0697dc2b72 | ||
|
|
55349d7770 | ||
|
|
2c753fae8f |
@@ -84,6 +84,7 @@ export(fileInput)
|
||||
export(fillCol)
|
||||
export(fillPage)
|
||||
export(fillRow)
|
||||
export(findPort)
|
||||
export(fixedPage)
|
||||
export(fixedPanel)
|
||||
export(fixedRow)
|
||||
|
||||
87
R/server.R
87
R/server.R
@@ -724,7 +724,7 @@ isRunning <- function() {
|
||||
#' }
|
||||
#' @export
|
||||
runApp <- function(appDir=getwd(),
|
||||
port=getOption('shiny.port'),
|
||||
port=getOption('shiny.port', findPort(host = host)),
|
||||
launch.browser=getOption('shiny.launch.browser',
|
||||
interactive()),
|
||||
host=getOption('shiny.host', '127.0.0.1'),
|
||||
@@ -794,12 +794,12 @@ runApp <- function(appDir=getwd(),
|
||||
if (arg %in% names(appOps)) appOps[[arg]] else default
|
||||
}
|
||||
|
||||
if (missing(host))
|
||||
host <- findVal("host", host %OR% '0.0.0.0')
|
||||
if (missing(port))
|
||||
port <- findVal("port", port)
|
||||
port <- findVal("port", port %OR% findPort(host = host))
|
||||
if (missing(launch.browser))
|
||||
launch.browser <- findVal("launch.browser", launch.browser)
|
||||
if (missing(host))
|
||||
host <- findVal("host", host)
|
||||
if (missing(quiet))
|
||||
quiet <- findVal("quiet", quiet)
|
||||
if (missing(display.mode))
|
||||
@@ -807,8 +807,6 @@ runApp <- function(appDir=getwd(),
|
||||
if (missing(test.mode))
|
||||
test.mode <- findVal("test.mode", test.mode)
|
||||
|
||||
if (is.null(host) || is.na(host)) host <- '0.0.0.0'
|
||||
|
||||
workerId(workerId)
|
||||
|
||||
if (inShinyServer()) {
|
||||
@@ -883,44 +881,6 @@ runApp <- function(appDir=getwd(),
|
||||
|
||||
require(shiny)
|
||||
|
||||
# determine port if we need to
|
||||
if (is.null(port)) {
|
||||
|
||||
# Try up to 20 random ports. If we don't succeed just plow ahead
|
||||
# with the final value we tried, and let the "real" startServer
|
||||
# somewhere down the line fail and throw the error to the user.
|
||||
#
|
||||
# If we (think we) succeed, save the value as .globals$lastPort,
|
||||
# and try that first next time the user wants a random port.
|
||||
|
||||
for (i in 1:20) {
|
||||
if (!is.null(.globals$lastPort)) {
|
||||
port <- .globals$lastPort
|
||||
.globals$lastPort <- NULL
|
||||
}
|
||||
else {
|
||||
# Try up to 20 random ports
|
||||
while (TRUE) {
|
||||
port <- p_randomInt(3000, 8000)
|
||||
# Reject ports in this range that are considered unsafe by Chrome
|
||||
# http://superuser.com/questions/188058/which-ports-are-considered-unsafe-on-chrome
|
||||
# https://github.com/rstudio/shiny/issues/1784
|
||||
if (!port %in% c(3659, 4045, 6000, 6665:6669, 6697)) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Test port to see if we can use it
|
||||
tmp <- try(startServer(host, port, list()), silent=TRUE)
|
||||
if (!inherits(tmp, 'try-error')) {
|
||||
stopServer(tmp)
|
||||
.globals$lastPort <- port
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# Invoke user-defined onStop callbacks, before the application's internal
|
||||
# onStop callbacks.
|
||||
on.exit({
|
||||
@@ -1062,7 +1022,7 @@ stopApp <- function(returnValue = invisible()) {
|
||||
#' }
|
||||
#' @export
|
||||
runExample <- function(example=NA,
|
||||
port=NULL,
|
||||
port=getOption('shiny.port', findPort(host = host)),
|
||||
launch.browser=getOption('shiny.launch.browser',
|
||||
interactive()),
|
||||
host=getOption('shiny.host', '127.0.0.1'),
|
||||
@@ -1126,7 +1086,7 @@ runExample <- function(example=NA,
|
||||
#' runGadget(shinyApp(ui, server))
|
||||
#' }
|
||||
#' @export
|
||||
runGadget <- function(app, server = NULL, port = getOption("shiny.port"),
|
||||
runGadget <- function(app, server = NULL, port = getOption("shiny.port", findPort()),
|
||||
viewer = paneViewer(), stopOnCancel = TRUE) {
|
||||
|
||||
if (!is.shiny.appobj(app)) {
|
||||
@@ -1221,6 +1181,41 @@ browserViewer <- function(browser = getOption("browser")) {
|
||||
}
|
||||
}
|
||||
|
||||
#' Find an open TCP port
|
||||
#'
|
||||
#' Finds a random available TCP port for listening on.
|
||||
#'
|
||||
#' @param min Minimum port number.
|
||||
#' @param max Maximum port number.
|
||||
#' @param host see [httpuv::randomPort()].
|
||||
#' @param n Number of ports to try before giving up.
|
||||
#' @param cache if `TRUE`, use the last random port if it's available.
|
||||
#'
|
||||
#' @details This function automatically excludes some ports
|
||||
#' which are considered unsafe by web browsers.
|
||||
#'
|
||||
#' @seealso [httpuv::randomPort()]
|
||||
#' @export
|
||||
#' @examples
|
||||
#'
|
||||
#' findPort()
|
||||
#' findPort()
|
||||
#' findPort(cache = FALSE)
|
||||
findPort <- function(min = 3000L, max = 8000L,
|
||||
host = getOption("shiny.host", "127.0.0.1"),
|
||||
n = 20, cache = TRUE) {
|
||||
if (cache && !is.null(.globals$lastPort)) {
|
||||
tmp <- try(startServer(host, .globals$lastPort, list()), silent = TRUE)
|
||||
if (!inherits(tmp, 'try-error')) {
|
||||
stopServer(tmp)
|
||||
return(.globals$lastPort)
|
||||
}
|
||||
}
|
||||
.globals$lastPort <- withPrivateSeed(httpuv::randomPort(min, max, host, n))
|
||||
.globals$lastPort
|
||||
}
|
||||
|
||||
|
||||
# Returns TRUE if we're running in Shiny Server or other hosting environment,
|
||||
# otherwise returns FALSE.
|
||||
inShinyServer <- function() {
|
||||
|
||||
@@ -204,7 +204,8 @@ sd_section("Utility functions",
|
||||
"onStop",
|
||||
"diskCache",
|
||||
"memoryCache",
|
||||
"reexports"
|
||||
"reexports",
|
||||
"findPort"
|
||||
)
|
||||
)
|
||||
sd_section("Plot interaction",
|
||||
|
||||
36
man/findPort.Rd
Normal file
36
man/findPort.Rd
Normal file
@@ -0,0 +1,36 @@
|
||||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/server.R
|
||||
\name{findPort}
|
||||
\alias{findPort}
|
||||
\title{Find an open TCP port}
|
||||
\usage{
|
||||
findPort(min = 3000L, max = 8000L, host = getOption("shiny.host",
|
||||
"127.0.0.1"), n = 20, cache = TRUE)
|
||||
}
|
||||
\arguments{
|
||||
\item{min}{Minimum port number.}
|
||||
|
||||
\item{max}{Maximum port number.}
|
||||
|
||||
\item{host}{see \code{\link[httpuv:randomPort]{httpuv::randomPort()}}.}
|
||||
|
||||
\item{n}{Number of ports to try before giving up.}
|
||||
|
||||
\item{cache}{if \code{TRUE}, use the last random port if it's available.}
|
||||
}
|
||||
\description{
|
||||
Finds a random available TCP port for listening on.
|
||||
}
|
||||
\details{
|
||||
This function automatically excludes some ports
|
||||
which are considered unsafe by web browsers.
|
||||
}
|
||||
\examples{
|
||||
|
||||
findPort()
|
||||
findPort()
|
||||
findPort(cache = FALSE)
|
||||
}
|
||||
\seealso{
|
||||
\code{\link[httpuv:randomPort]{httpuv::randomPort()}}
|
||||
}
|
||||
@@ -4,11 +4,11 @@
|
||||
\alias{runApp}
|
||||
\title{Run Shiny Application}
|
||||
\usage{
|
||||
runApp(appDir = getwd(), port = getOption("shiny.port"),
|
||||
launch.browser = getOption("shiny.launch.browser", interactive()),
|
||||
host = getOption("shiny.host", "127.0.0.1"), workerId = "",
|
||||
quiet = FALSE, display.mode = c("auto", "normal", "showcase"),
|
||||
test.mode = getOption("shiny.testmode", FALSE))
|
||||
runApp(appDir = getwd(), port = getOption("shiny.port", findPort(host =
|
||||
host)), launch.browser = getOption("shiny.launch.browser",
|
||||
interactive()), host = getOption("shiny.host", "127.0.0.1"),
|
||||
workerId = "", quiet = FALSE, display.mode = c("auto", "normal",
|
||||
"showcase"), test.mode = getOption("shiny.testmode", FALSE))
|
||||
}
|
||||
\arguments{
|
||||
\item{appDir}{The application to run. Should be one of the following:
|
||||
|
||||
@@ -4,10 +4,10 @@
|
||||
\alias{runExample}
|
||||
\title{Run Shiny Example Applications}
|
||||
\usage{
|
||||
runExample(example = NA, port = NULL,
|
||||
launch.browser = getOption("shiny.launch.browser", interactive()),
|
||||
host = getOption("shiny.host", "127.0.0.1"), display.mode = c("auto",
|
||||
"normal", "showcase"))
|
||||
runExample(example = NA, port = getOption("shiny.port", findPort(host =
|
||||
host)), launch.browser = getOption("shiny.launch.browser",
|
||||
interactive()), host = getOption("shiny.host", "127.0.0.1"),
|
||||
display.mode = c("auto", "normal", "showcase"))
|
||||
}
|
||||
\arguments{
|
||||
\item{example}{The name of the example to run, or \code{NA} (the default) to
|
||||
|
||||
@@ -4,8 +4,8 @@
|
||||
\alias{runGadget}
|
||||
\title{Run a gadget}
|
||||
\usage{
|
||||
runGadget(app, server = NULL, port = getOption("shiny.port"),
|
||||
viewer = paneViewer(), stopOnCancel = TRUE)
|
||||
runGadget(app, server = NULL, port = getOption("shiny.port",
|
||||
findPort()), viewer = paneViewer(), stopOnCancel = TRUE)
|
||||
}
|
||||
\arguments{
|
||||
\item{app}{Either a Shiny app object as created by
|
||||
|
||||
Reference in New Issue
Block a user