悬停在JS中的flexdashboard图上或悬停动作时显示工具提示

马努

我有以下示例flexdashboard:

---
title: "Hover"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
runtime: shiny
---

```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
data(iris)
Column {data-width=650}
-----------------------------------------------------------------------

### Chart A

```{r}
iris %>% group_by(Species) %>% 
  summarise(mean = mean(Sepal.Length)) %>% 
  ggplot(aes(Species, mean)) + geom_col()

当悬停在图形中的条形上并显示其值时,我想要的是一个工具提示。我在SO中阅读了这篇文章,如何在ggplot2中将鼠标悬停时在工具提示上显示y值,但它适用于Shiny。我尝试了这个:

p <- iris %>% group_by(Species) %>% 
  summarise(mean = mean(Sepal.Length))

labels <- sprintf("<strong>%s</strong><br/>Mean: %f", 
                     p$Species, p$mean) %>% 
     lapply(htmltools::HTML)

p %>% ggplot(aes(Species, mean)) + geom_col() + geom_text(aes(label = labels))

这将创建一个带有种类和值的html工具,我没有的是悬停(可能是plot_hover?)来显示工具提示。

任何帮助将不胜感激

问候,

斯蒂芬·洛朗(Stephane Laurent)

这是一种方法。您必须定位到条的顶部中心才能获得工具提示。

---
title: "Untitled"
runtime: shiny
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
---

```{r setup, include=FALSE}
library(flexdashboard)
library(dplyr)
library(ggplot2)
library(shiny)
```

Column {data-width=650}
-----------------------------------------------------------------------

### Chart A

```{r}
dat <- iris %>% group_by(Species) %>% 
  summarise(mean = mean(Sepal.Length))
output[["ggplot"]] <- renderPlot({
  dat %>% 
  ggplot(aes(Species, mean)) + geom_col()
})
output[["hoverinfo"]] <- renderUI({ 
  hover <- input[["plot_hover"]]
  if(is.null(hover)) return(NULL)
  point <- nearPoints(dat, hover, threshold = 50, maxpoints = 1)
  if(nrow(point) == 0) return(NULL)
  left_px <- hover$coords_css$x
  top_px <- hover$coords_css$y
  style <- 
    paste0("position:absolute; z-index:100; background-color: rgba(245, 245, 245, 0.85); ",
           "left:", left_px, 
           "px; top:", top_px, "px;")
  # tooltip created as wellPanel
  tooltip <- paste0(
    "<b> Species: </b>", point[["Species"]], "<br/>",
    "<b> mean: </b>", point[["mean"]]
  )
  wellPanel(
    style = style, p(HTML(tooltip))
  )
}) 
plotOutput("ggplot", hover = hoverOpts("plot_hover"))
div(uiOutput("hoverinfo"), style = "pointer-events: none;")
```

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在C3js上,如何将工具提示悬停在条形图上时显示该条的数据?

鼠标悬停在折线图上时显示工具提示

当鼠标悬停在带有WPF的.NET中的列上时,如何显示描述的工具提示?

将鼠标悬停在选项卡上时显示工具提示

当鼠标悬停在数据点上时如何显示工具提示

将鼠标悬停在列表项上时如何显示工具提示?

在将鼠标悬停在文本上时显示工具提示

悬停在内容上方时如何显示内容和工具提示?

悬停时如何显示工具提示?

在悬停时显示工具提示的HTML元素

工具提示仅在悬停时不显示

将Materialise CSS工具提示悬停在其上方时

悬停在图表上时工具提示闪烁

鼠标悬停在圆环图表中的中心文本不显示工具提示

默认情况下如何显示工具提示而不将鼠标悬停在纯CSS中

检测鼠标悬停在图例上,并在 matplotlib 中显示工具提示(标签/注释)?

如何在图例悬停的Google饼图上显示工具提示?

在散景楔形图上正确显示悬停工具提示

使用 JS 悬停时的工具提示

当鼠标悬停在单元格上时,如何根据单元格中存在的值在DataGridView中显示单元格的工具提示

SwiftUI:如何在悬停时显示工具提示/提示?

如何获取图片的工具提示,仅当我们将鼠标悬停在其上时才会显示。硒中

将鼠标悬停在内容上时,在工具提示中显示图像给我一种闪烁的效果

悬停显示工具提示

是否可以仅在使用 jQuery 将鼠标悬停在选中的单选按钮上时才显示工具提示

将鼠标悬停在按钮pyqt5上时如何显示工具提示图像

当我将鼠标悬停在Kendosparkline上时,为什么工具提示无法正确显示?

在悬停时删除工具提示

光标悬停时的工具提示