Move R Shiny showNotification to the center of the screen

I am looking at setting up a function showNotification()from Shiny.

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

I would like the message to be generated in the center of the screen, and not in the lower right corner. I do not think that this can be installed initially, but I hope that someone will have a suggestion on how to do this.

+6
source share
1 answer

You can use tags$styleto overwrite the properties of the CSS class (in this case:) .shiny-notification. You can also customize other properties such as width and height with this approach.

The CSS part will be:

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

50% 50% .

CSS , ui.

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)
    })
  }
)

, , , .

+14

Source: https://habr.com/ru/post/1677687/


All Articles