如何在Shiny R中添加工具提示?

67

我希望在我的ShinyR应用程序中使用提示工具。有没有简单的方法可以实现这一点?目前,我正在创建一个密度地图,并且我想要一个简单的提示工具,当将鼠标悬停在年份滑块上时,显示“单击此处以浏览年份”。

用户界面:

library(shiny)
shinyUI(pageWithSidebar(
  headerPanel("Density Map"),
  sidebarPanel(
    sliderInput("slider_year", "YEAR:", 
                min = 2001, max = 2011, value = 2009, 
                format="####", locale="us"
    )
  )
 ),

  mainPanel(  
    plotOutput("event_heatmap_map", width = "100%", height = "100%")
  )
))


服务器代码:

library(shiny)
library(ggmap)
library(ggplot2)
mydata <- read.csv("/var/shiny-server/www/dMetrics.csv")
shinyServer(function(input, output) {
    output$event_heatmap_map <- renderPlot(width = "auto", height = 640,{

        slice_year <- mydata[mydata$YEAR==input$slider_year,]
        map <- get_map(c(lon = -55.3632715, lat = 31.7632836), zoom = 3, source = 'google', maptype = c("terrain"), messaging = FALSE, color = 'color')
        world <- ggmap(map)
        world <- world + stat_density2d(data = slice_year, aes(x = WEST, y = NORTH, fill = ..level.., alpha = ..level..), show_guide = FALSE, geom = "polygon", na.rm = TRUE) + scale_fill_gradient(name="Density", low="maroon", high="yellow", guide = 'colorbar')
        plot(world)
    })
})

感谢您的帮助。


1
你能提供更多的信息吗?https://dev59.com/eG025IYBdhLWcg3whGSx#5963610 - harkmug
3个回答

72

我认为你应该可以替换这个:

sliderInput("slider_year", "YEAR:", 
            min = 2001, max = 2011, value = 2009, 
            format="####", locale="us"
)

用这个:

tags$div(title="Click here to slide through years",
    sliderInput("slider_year", "YEAR:", 
                min = 2001, max = 2011, value = 2009, 
                format="####", locale="us"
    )
)

非常感谢您的快速回复。Shiny R真的很酷。我还在使用与Shiny一起的Google Motion Chart。您能否再为我提供一个方法来添加/更改其中的工具提示,这样可以帮我一个忙吗?我在Ui.r中没有显示任何内容,在server.r中我放了一些代码,如下所示: output$view_gviz <- renderGvis({ chart <- gvisMotionChart(visdata, idvar="Storm", timevar="Year",......). 再次感谢您的帮助。 - Sabin
如果有人能给我提供一个类似于这个帖子的提示,我将不胜感激: post - Sabin
但是有没有一种方法可以使标题滚动,以便当工具提示大于某个大小时? - bokov
<div>标签将封装的输入对象发送到另一行。是否有一种方法可以使其保持内联?例如,使用除了<div>之外的标签? - RDavey

54

这是一种稍微更简单、更优雅的方法。

library(shinyBS) # Additional Bootstrap Controls

## From ui.R: Adds a tooltip to element with inputId = "someInput" 
## with text, "This is an input.", that appears to the left on hover.
bsTooltip(id = "someInput", title = "This is an input", 
          placement = "left", trigger = "hover")

## From server.R: Add the same tooltip as above
addTooltip(session, id = "someInput", title = "This is an input.",
           placement = "left", trigger = "hover")

您可以在 ui.Rserver.R 中添加工具提示,另外您也可以使用弹出框。

2
我不确定添加另一个库是否更容易或更优雅,尽管shinyBS仍然很棒。 - dca

1

现在可以在 plot_ly 周围包装 ggplot 可视化,而 plotly 具有本地组件(精确命名为提示),它将满足您寻找的目的:

示例来自:https://plotly.com/ggplot2/interactive-tooltip/
library(plotly)
#install.packages("gapminder")
library(gapminder)

p <- ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, color = continent, text =      paste("country:", country))) +
     geom_point(alpha = (1/3)) + scale_x_log10()  

fig <- ggplotly(p)

fig

此外,还可以参考https://plotly-r.com/controlling-tooltips.html以获得更详细的解释。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接