在 Shiny 应用程序中获取实际域名(服务器名称)

get the actuel domain name (server name) in Shiny app

我有 3 个服务器,devtestprod。我的 Shiny 代码应该从 dev 部署到 prod.

现在的问题: 在 ui.R 中,我通过 href = 'https://dev.com/start/' 引用了另一个名为 start 的站点。是否可以自动获取域名devtestprod?比如,`href = 'https://what is the actuall domain.com/start/'

附录:正如 DanielR 回答的那样,可以使用 session$clientData$url_hostname,但是我的问题是我需要 dashboardHeader 中的主机名。 ui.R 中我需要动态 href 的地方是:

dashboardPage(
  dashboardHeader(title = "KRB",
                  
                  titleWidth = 150,
                  
                  tags$li(a(href ='https://dev.com/start/

您可以在服务器函数中使用 session$clientData$url_hostname 获取主机名。参见 https://shiny.rstudio.com/articles/client-data.html

这是一个小应用程序:

library(shiny)
ui <- fluidPage(
    uiOutput('urlui')
)
server <- function(input, output, session) {
    output$urlui <- renderUI({
        htmltools::a('my link',
                     href=paste0('http://', session$clientData$url_hostname))
    })
}
shinyApp(ui = ui, server = server)

Now the problem: In the ui.R I refere via href = 'https://dev.com/start/' to another site named start. Is it possible to get the domain name, dev, test and prod automatically?

对于您想在此处实现的目标,您不需要获取实际的主机名,如果您可以只使用相对 URL 而不是完整的绝对主机名作为开头。

而不是

tags$li(a(href ='https://dev.com/start/' …

使用

tags$li(a(href ='/start/' …

带有前导斜线的相对 URL 指的是域根目录,因此这应该自动解析为 https://[hostname]/start/,而您不必确定 [hostname] 在这种情况下实际是什么.当浏览器根据当前显示的主文档的地址解析相对 URLs 时,它基本上会为您完成该部分。