将 R 闪亮显示通知移动到屏幕中央



我正在考虑从Shiny自定义showNotification()功能。

https://gallery.shinyapps.io/116-notifications/

我希望消息在屏幕中间而不是右下角生成。我不认为这可以在本地设置,但我希望有人能对如何完成此操作提出建议。

您可以使用tags$style来覆盖CSS类属性(在本例中:.shiny-notification)。您还可以使用这种方法调整其他属性,例如宽度和高度。

css 部分将是:

.shiny-notification {
position:fixed;
top: calc(50%);
left: calc(50%);
}

将通知设置为屏幕宽度的 50% 和高度宽度的 50%。

您可以通过在ui函数中使用以下内容在shiny 中包含 css 代码

tags$head(
tags$style(
HTML(CSS-CODE....)
)
)

一个完整的可重现应用程序如下:

library(shiny)
shinyApp(
ui = fluidPage(
tags$head(
tags$style(
HTML(".shiny-notification {
position:fixed;
top: calc(50%);
left: calc(50%);
}
"
)
)
),
textInput("txt", "Content", "Text of message"),
radioButtons("duration", "Seconds before fading out",
choices = c("2", "5", "10", "Never"),
inline = TRUE
),
radioButtons("type", "Type",
choices = c("default", "message", "warning", "error"),
inline = TRUE
),
checkboxInput("close", "Close button?", TRUE),
actionButton("show", "Show"),
actionButton("remove", "Remove most recent")
),
server = function(input, output) {
id <- NULL
observeEvent(input$show, {
if (input$duration == "Never")
duration <- NA
else 
duration <- as.numeric(input$duration)
type <- input$type
if (is.null(type)) type <- NULL
id <<- showNotification(
input$txt,
duration = duration, 
closeButton = input$close,
type = type
)
})
observeEvent(input$remove, {
removeNotification(id)
})
}
)

下面使用的应用程序模板取自您提供的链接中的代码。

最新更新