如何将一个大的、带颜色的图标添加到闪亮模态框的中央

huangapple go评论69阅读模式
英文:

How to add a large, colored icon to the center of a shiny modal

问题

我想创建一个看起来像可怕警告的闪亮模态框。

以下是我目前的进展:

library(shiny)

ui = basicPage(
  actionButton("show", "显示模态对话框")
)

server = function(input, output) {
  observeEvent(input$show, {
    showModal(modalDialog(
      title = icon("bomb", lib = "glyphicon"),
      "这是一条重要消息!"
    ))
  })
}

shinyApp(ui, server)

如何使图标变得更大、居中,并且是橙色或红色的警告颜色?我正在使用bslib进行主题设置,所以理想情况下警告颜色应该与主题相关。

英文:

I want to create a shiny modal that looks like a scary warning.

Here is what I have so far:

library(shiny)


ui = basicPage(
  actionButton("show", "Show modal dialog")
)

server = function(input, output) {
  observeEvent(input$show, {
    showModal(modalDialog(
      title = icon("bomb"),
      "This is an important message!"
    ))
  })
}

shinyApp(ui, server)

which produces this

如何将一个大的、带颜色的图标添加到闪亮模态框的中央

How can I make the icon way bigger, centered, and a warning color like orange or red? I'm using bslib for theming so ideally the warning color would be tied to the theme.

答案1

得分: 1

这基于FontAwesome,允许调整大小,详见https://fontawesome.com/docs/web/style/size。

演示:

shiny::icon("bomb")
shiny::icon("bomb", class="fa-2xl")
shiny::icon("bomb", class="fa-10x", style="color: Tomato;")

分别是:

如何将一个大的、带颜色的图标添加到闪亮模态框的中央

如何将一个大的、带颜色的图标添加到闪亮模态框的中央

如何将一个大的、带颜色的图标添加到闪亮模态框的中央


至于居中,如果您不怕将页面上所有的<h4>元素都居中,可以使用以下方法:

library(shiny)
ui = basicPage(
  tags$style(HTML("h4 { text-align: center; }")),
  actionButton("show", "Show modal dialog")
)
server = function(input, output) {
  observeEvent(input$show, {
    showModal(modalDialog(
      title = icon("bomb", class="fa-10x", style="color: Tomato;"),
      "这是一条重要消息!"
    ))
  })
}
shinyApp(ui, server)

如何将一个大的、带颜色的图标添加到闪亮模态框的中央

英文:

This is based on FontAwesome, which allows sizing, see https://fontawesome.com/docs/web/style/size.

Demo:

shiny::icon(&quot;bomb&quot;)
shiny::icon(&quot;bomb&quot;, class=&quot;fa-2xl&quot;)
shiny::icon(&quot;bomb&quot;, class=&quot;fa-10x&quot;, style=&quot;color: Tomato;&quot;)

Respectively:

如何将一个大的、带颜色的图标添加到闪亮模态框的中央

如何将一个大的、带颜色的图标添加到闪亮模态框的中央

如何将一个大的、带颜色的图标添加到闪亮模态框的中央


As for centering it, if you're not afraid to center all &lt;h4&gt; elements on the page, this works:

library(shiny)
ui = basicPage(
  tags$style(HTML(&quot;h4 { text-align: center; }&quot;)),
  actionButton(&quot;show&quot;, &quot;Show modal dialog&quot;)
)
server = function(input, output) {
  observeEvent(input$show, {
    showModal(modalDialog(
      title = icon(&quot;bomb&quot;, class=&quot;fa-10x&quot;, style=&quot;color: Tomato;&quot;),
      &quot;This is an important message!&quot;
    ))
  })
}
shinyApp(ui, server)

如何将一个大的、带颜色的图标添加到闪亮模态框的中央

答案2

得分: 1

你可以使用一个Sweet Alert,但你只有四种图标可供选择。

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  actionButton(
    inputId = "btn",
    label = "启动警告 Sweet Alert",
    icon = icon("check")
  )
)

server <- function(input, output, session) {
  
  observeEvent(input$btn, {
    show_alert(
      title = "重要!!",
      text = "阅读此消息...",
      type = "warning"
    )
  })
  
}

shinyApp(ui, server)
英文:

You can use a sweet alert, but you'll have only four choices for the icon.

library(shiny)
library(shinyWidgets)

ui &lt;- fluidPage(
  actionButton(
    inputId = &quot;btn&quot;,
    label = &quot;Launch a warning sweet alert&quot;,
    icon = icon(&quot;check&quot;)
  )
)

server &lt;- function(input, output, session) {
  
  observeEvent(input$btn, {
    show_alert(
      title = &quot;Important !!&quot;,
      text = &quot;Read this message...&quot;,
      type = &quot;warning&quot;
    )
  })
  
}

shinyApp(ui, server)

如何将一个大的、带颜色的图标添加到闪亮模态框的中央

huangapple
  • 本文由 发表于 2023年6月2日 02:01:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76384548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定