【问题标题】:RStudio/Posit workbench how does it create the URL pathRStudio/Posit workbench 如何创建 URL 路径
【发布时间】:2023-02-01 19:53:04
【问题描述】:

我在浏览器的 RStudio/Posit 工作台上本地运行一个闪亮的应用程序。

该应用程序仅打印其当前 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))

例如,当我运行它时,它显示为:https://POSIT_URL/s/46da136e42a33f0a920f9/p/64dab64d/。 我对最后一点 64dab64d 很感兴趣,想知道这是如何创建的。这取决于端口号并且似乎与我的会话一致。 是否可以在应用程序运行之前生成/预测这个数字?

我怀疑它是一个散列值,但我找不到正确的输入/散列函数。

【问题讨论】:

    标签: r shiny rsconnect


    【解决方案1】:

    不完全是一个答案,而是一个蛮力解决方法:启动应用程序,打印 URL 并再次停止应用程序。 在函数中它看起来像这样

    # retrieves the URL for a shiny app
    get_full_shiny_url <- function(port) {
      server <- function(input, output, session){
        shiny::observe(cat(paste0(
          session$clientData$url_protocol, "//", session$clientData$url_hostname,
          session$clientData$url_pathname, "
    "
        )))
        shiny::stopApp()
      }
      
      capture.output(shiny::shinyApp(shiny::basicPage(), server,
                                     options = list(port = port)))
    }
    get_full_shiny_url(4812)
    #> https://POSIT_URL/s/46da136e42a33f0a920f9/p/64dab64d/
    

    【讨论】:

      猜你喜欢
      • 2023-02-03
      • 2016-08-18
      • 2015-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多