显示R闪亮应用程序的部署时间



我有一个闪亮的应用程序,将重新部署大约每星期的shinyapps。

在应用程序的首页,我想显示应用程序最后部署的时间。

我认为这样做是可能的:

library(shiny)
deployment_time <- lubridate::now()
ui <- fluidPage(
p(glue::glue("Deployment time {deployment_time}"))
)
server <- function(input, output) {
}
shinyApp(ui = ui, server = server)

这背后的原因是deployment_time是与服务器一起设置的,所以应该只在部署应用程序时运行一次,而不是在用户以后查看应用程序时运行。

然而,我观察到的行为是,在几次加载应用程序后,部署时间将更新为当前时间,这表明这段代码实际上在某个时刻重新运行。

有什么想法是怎么回事,我怎么能设置一个部署时间保持固定,而不必手动在脚本中设置日期?

Thanks in advance:)

我会将最后一次部署日期存储在本地文件中,该文件与应用程序代码一起上传到Shiny Server。

下面是一个最低限度可复制的例子。

部署记录第一个函数只在部署应用程序时运行。您可以花一些时间将这个函数插入到部署脚本中,以便它在将文件上传到服务器之前写入时间。

#' Record the date of app deployment.
record_deployment_date <-
function(deployment_history_file = "deployment_history.txt") {
# make sure the file exists...
if (!file.exists(deployment_history_file)) {
file.create(deployment_history_file)
}

# record the time
deployment_time <- Sys.time()
cat(paste0(deployment_time, "n"),
file = deployment_history_file,
append = TRUE)
}

然后,您将有另一个功能来访问最后记录的部署日期。

#' Return the last recorded deployment date of the application.
load_deployment_date <-
function(deployment_history_file = "deployment_history.txt") {
deployment_history <- readLines(deployment_history_file)

# return the most recent line
deployment_history[[length(deployment_history)]]
}

最小应用示例

最后,您可以调用前面的函数并将加载的文本插入到renderText函数中,以显示您的最后部署日期。

ui <- fluidPage(mainPanel(tags$h1("My App"),
textOutput("deploymentDate")))
server <- function(input, output, session) {
output$deploymentDate <- renderText({
paste0("Deployment Time: ", load_deployment_date())
})
}
shinyApp(ui, server)

你自然会想要改变你的deployment_history.txt文件的位置,自定义你的时间格式,等等。您还可以更进一步,包括部署版本。但是,这是入门所需的最小信息。

最新更新