将R Shiny showNotification移动到屏幕中心 [英] Move R Shiny showNotification to center of screen
问题描述
我正在考虑从Shiny自定义 showNotification()
功能。
I am looking at customizing the showNotification()
functionality from Shiny.
> https://gallery.shinyapps.io/116-notifications/
我希望在屏幕中间而不是右下角生成消息。我不认为这可以在本地设置,但我希望有人对如何完成此操作提出建议。
I would like the message to be generated in the middle of the screen as opposed to the bottom-right. I don't think this can be set natively but I am hoping someone would have a suggestion of how to accomplish this.
推荐答案
您可以使用 tags $ style
覆盖CSS类属性(在这种情况下: .shiny-notification
)。您还可以使用该方法调整宽度和高度等其他属性。
You can use tags$style
to overwrite the CSS class properties (in this case: .shiny-notification
). You could also adjust other properties like width and height with that approach.
css部分为:
.shiny-notification {
position:fixed;
top: calc(50%);
left: calc(50%);
}
将通知设置为屏幕宽度的50%和高度宽度的50%。
that sets the notification to 50% of screen width and 50% height width.
您可以在 ui
中使用以下内容,将 css代码包含在闪亮的中
You can include the css code in shiny by using the following in the ui
function.
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)
})
}
)
下面使用的应用程序模板来自您提供的链接中的代码。
The app template used below i took from the code in the link you provided.
这篇关于将R Shiny showNotification移动到屏幕中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!