【问题标题】:Reactively changing colour of an infobox, upon a click or hover over单击或悬停在信息框上时,响应式更改信息框的颜色
【发布时间】:2015-08-13 10:07:12
【问题描述】:

我想在shinyshinydashboard 中使用reactiveValueobserveobserveEvent 框架,以便能够在单击时响应地更改信息框的颜色。

我还希望它在将鼠标悬停在 infoBox 上时在弹出框中显示带有一些文本的图像。

作为可复现示例的代码基础,请参阅this

但代码如下:

 library(shinydashboard)

  ui <- dashboardPage(
    dashboardHeader(title = "Info boxes"),
    dashboardSidebar(),
    dashboardBody(
      # infoBoxes with fill=FALSE
      fluidRow(
        # A static infoBox
        infoBox("New Orders", 10 * 2, icon = icon("credit-card")),
        # Dynamic infoBoxes
        infoBoxOutput("progressBox"),
        infoBoxOutput("approvalBox")
      ),

      # infoBoxes with fill=TRUE
      fluidRow(
        infoBox("New Orders", 10 * 2, icon = icon("credit-card"), fill = TRUE),
        infoBoxOutput("progressBox2"),
        infoBoxOutput("approvalBox2")
      ),

      fluidRow(
        # Clicking this will increment the progress amount
        box(width = 4, actionButton("count", "Increment progress"))
      )
    )
  )

  server <- function(input, output) {
    output$progressBox <- renderInfoBox({
      infoBox(
        "Progress", paste0(25 + input$count, "%"), icon = icon("list"),
        color = "purple"
      )
    })
    output$approvalBox <- renderInfoBox({
      infoBox(
        "Approval", "80%", icon = icon("thumbs-up", lib = "glyphicon"),
        color = "yellow"
      )
    })

    # Same as above, but with fill=TRUE
    output$progressBox2 <- renderInfoBox({
      infoBox(
        "Progress", paste0(25 + input$count, "%"), icon = icon("list"),
        color = "purple", fill = TRUE
      )
    })
    output$approvalBox2 <- renderInfoBox({
      infoBox(
        "Approval", "80%", icon = icon("thumbs-up", lib = "glyphicon"),
        color = "yellow", fill = TRUE
      )
    })
  }

  shinyApp(ui, server)

这可能吗?

【问题讨论】:

  • 嗨,h.l.m.当然有可能。最后,Shiny 正在生成 html,您可以根据需要添加尽可能多的 HTML / Javascript / JQuery / CSS。只要您不使用 Shiny Server 运行它,该服务器本身就要求很多,让您几乎空手而归。参见例如:stackoverflow.com/questions/23599268/…chrisbeeley.net/?p=481,但要准备好深潜或坚持 SHINY 方式。

标签: javascript html r shiny


【解决方案1】:

你想做的事完全可以用 CSS 和 JavaScript 来完成,而不是闪亮的。这是一种可能的解决方案(有很多方法可以实现您想要的)。

您将鼠标悬停在任何信息框上都会变为灰色,当您单击它时,它会变为不同的灰色。当您将鼠标悬停在第一个信息框(左上角)上时,它还会显示一个带有图像的弹出窗口。 为了解决如何在悬停/单击时更改背景颜色的问题,我只是添加了一些 CSS。为了在悬停时弹出一个显示图像的弹出窗口,我使用了 Bootstrap 的弹出窗口。很简单,希望对你有帮助

library(shinydashboard)

mycss <- "
.info-box:hover,
.info-box:hover .info-box-icon {
  background-color: #aaa !important;
}
.info-box:active,
.info-box:active .info-box-icon {
  background-color: #ccc !important;
}
"

withPopup <- function(tag) {
  content <- div("Some text and an image",
                 img(src = "http://thinkspace.com/wp-content/uploads/2013/12/member-logo-rstudio-109x70.png"))
  tagAppendAttributes(
    tag,
    `data-toggle` = "popover",
    `data-html` = "true",
    `data-trigger` = "hover",
    `data-content` = content
  )
}

ui <- dashboardPage(
  dashboardHeader(title = "Info boxes"),
  dashboardSidebar(),
  dashboardBody(
    tags$head(tags$style(HTML(mycss))),
    tags$head(tags$script("$(function() { $(\"[data-toggle='popover']\").popover(); })")),
    # infoBoxes with fill=FALSE
    fluidRow(
      # A static infoBox
      withPopup(infoBox("New Orders", 10 * 2, icon = icon("credit-card"))),
      # Dynamic infoBoxes
      infoBoxOutput("progressBox"),
      infoBoxOutput("approvalBox")
    ),

    # infoBoxes with fill=TRUE
    fluidRow(
      infoBox("New Orders", 10 * 2, icon = icon("credit-card"), fill = TRUE),
      infoBoxOutput("progressBox2"),
      infoBoxOutput("approvalBox2")
    ),

    fluidRow(
      # Clicking this will increment the progress amount
      box(width = 4, actionButton("count", "Increment progress"))
    )
  )
)

server <- function(input, output) {
  output$progressBox <- renderInfoBox({
    infoBox(
      "Progress", paste0(25 + input$count, "%"), icon = icon("list"),
      color = "purple"
    )
  })
  output$approvalBox <- renderInfoBox({
    infoBox(
      "Approval", "80%", icon = icon("thumbs-up", lib = "glyphicon"),
      color = "yellow"
    )
  })

  # Same as above, but with fill=TRUE
  output$progressBox2 <- renderInfoBox({
    infoBox(
      "Progress", paste0(25 + input$count, "%"), icon = icon("list"),
      color = "purple", fill = TRUE
    )
  })
  output$approvalBox2 <- renderInfoBox({
    infoBox(
      "Approval", "80%", icon = icon("thumbs-up", lib = "glyphicon"),
      color = "yellow", fill = TRUE
    )
  })
}

shinyApp(ui, server)

【讨论】:

  • 看起来非常好,谢谢!几件事......为什么悬停只适用于左上角的信息框?不同的信息框会出现不同的图像吗?悬停将颜色更改为灰色看起来不错,但是有没有一种方法可以在您单击信息框时永久更改颜色或增加信息框中的值?
  • 关于前几个问题:如果您阅读代码并查看我的版本和您的版本之间的变化,您会明白为什么只有左上角的框有点击效果。如果您查看对infoBox 的所有调用,您会发现对于第一个调用,我将它包装在对withPopup 的调用中(这是我在代码中定义的函数)。是的,您可以制作差异图像,现在withPopup 使用硬编码图像,您可以将图像源作为参数传递。如果您希望颜色永久更改或增加值,则需要一些更复杂的 Javascript...
  • 看起来您正在尝试做一些需要一些基本 javascript 和 CSS 知识的事情。我强烈建议您花几个小时来学习 JS 和 CSS 基础知识,您将能够使应用程序更加强大和灵活。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
相关资源
最近更新 更多