19

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.

1 Answer 1

44

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.

The css part would be:

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

that sets the notification to 50% of screen width and 50% height width.

You can include the css code in shiny by using the following in the ui function.

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

A full reproducible app is below:

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.

5
  • Perfect Mr BigDataScientist!
    – J. Doe.
    May 22, 2017 at 14:37
  • 3
    This way actually the upper left corner is at the center. To really center the nofification you can use calc(50% - 150px) – if your width is 300px. I also would apply the css to #shiny-notification-panel, the container that containes all notifications. This way the multiple notifications still show as a stack. In your solution the next notification hides the old one.
    – nnn
    Sep 25, 2019 at 16:16
  • Thanks for your comment. I will take a look at home, sounds interesting. You want to add it as an additional answer or should I make an edit here? Sep 25, 2019 at 16:47
  • See also from the RStudio team: github.com/rstudio/shiny/issues/2988
    – Antoine
    Mar 2, 2021 at 21:28
  • @nnn I did apply the same CSS to #shiny-notification-panel, but the notifications are overlaid, not shown as a stack. Any tip?
    – Antoine
    Mar 2, 2021 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.