How to get the current URL for Shiny App?

感情迁移 提交于 2020-12-15 04:32:19

问题


When I run a shiny application on RStudio, my app runs and the URL looks like this:

Listening on http://127.0.0.1:4991

But each time I run again the app, the port changes, I wonder if there is a way to obtain the url as a variable or something like that. I mean, each time that I run the app, I want to obtain the URL and save it in a variable.

Thank you


回答1:


You can specify the port using options:

#example: https://shiny.rstudio.com/articles/basics.html

options(shiny.host = '0.0.0.0')
options(shiny.port = 8888)

ui <- fluidPage(
  
  # App title ----
  titlePanel("Hello Shiny!"),
  
  # Sidebar layout with input and output definitions ----
  sidebarLayout(
    
    # Sidebar panel for inputs ----
    sidebarPanel(
      
      # Input: Slider for the number of bins ----
      sliderInput(inputId = "bins",
                  label = "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
      
    ),
    
    # Main panel for displaying outputs ----
    mainPanel(
      
      # Output: Histogram ----
      plotOutput(outputId = "distPlot")
      
    )
  )
)

# Define server logic required to draw a histogram ----
server <- function(input, output) {
  
  # Histogram of the Old Faithful Geyser Data ----
  # with requested number of bins
  # This expression that generates a histogram is wrapped in a call
  # to renderPlot to indicate that:
  #
  # 1. It is "reactive" and therefore should be automatically
  #    re-executed when inputs (input$bins) change
  # 2. Its output type is a plot
  output$distPlot <- renderPlot({
    
    x    <- faithful$waiting
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    
    hist(x, breaks = bins, col = "#75AADB", border = "white",
         xlab = "Waiting time to next eruption (in mins)",
         main = "Histogram of waiting times")
    
  })
  
}

shinyApp(ui, server)



回答2:


You can get the url with session$clientData:

library(shiny)

ui <- basicPage()

server <- function(input, output, session){
  
  observe({
    print(reactiveValuesToList(session$clientData))
  })
  
}

shinyApp(ui, server)


来源:https://stackoverflow.com/questions/64106740/how-to-get-the-current-url-for-shiny-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!