Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

css - Move R Shiny showNotification to center of screen

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...