简体   繁体   English

鼠标悬停时Rstudio闪亮的ggvis工具提示

[英]Rstudio shiny ggvis tooltip on mouse hover

In the example below, I have an interactive shiny ggvis plot, but I added a long column that is a long string and for some reason, my hover pop-up shows wt and mpg but does not show long . 在下面的例子,我有一个互动的闪亮ggvis情节,但我添加了一个long列,它是一个长字符串,由于某种原因,我悬停弹出显示wtmpg但不显示long

Also, if the list of elements in the legend is too long, they are hidden at the bottom right corner of the plot. 此外,如果图例中的元素列表太长,它们将隐藏在图的右下角。 Is there any way to stack these in several columns in the legend? 有没有办法在图例中的几列中堆叠这些?

Any ideas? 有任何想法吗?

# ui.R
library(ggvis)
shinyUI(pageWithSidebar(
  div(),
  sidebarPanel(
    sliderInput("n", "Number of points", min = 1, max = nrow(mtcars),
                value = 10, step = 1),
    uiOutput("plot_ui")
  ),
  mainPanel(
    ggvisOutput("plot"),
    tableOutput("mtc_table")
  )
))


# server.R
library(shiny)
library(ggvis)
shinyServer(function(input, output, session) {
  # A reactive subset of mtcars
  mtc <- reactive({
      data = mtcars[1:input$n, ]
      data$long = as.character(paste0("A car with ",data$cyl," cylinders and ",data$gear," gears and ",data$carb, " carburators"))
      data
  })
  # A simple visualisation. In shiny apps, need to register observers
  # and tell shiny where to put the controls
  mtc %>%
    ggvis(~wt, ~mpg) %>%
    layer_points(fill = ~factor(long)) %>%
    add_tooltip(function(data){paste0("Wt: ", data$wt, "<br>", "Mpg: ",as.character(data$mpg), "<br>", "String: ", as.character(data$long))}, "hover") %>%
    bind_shiny("plot", "plot_ui")

   output$mtc_table <- renderTable({
     mtc()[, c("wt", "mpg", "long")]
   })
})

You need to add long as a key currently data$long is null in the anonymous function supplied to add_tooltip : 您需要在提供给add_tooltip的匿名函数中添加long作为键,当前data$long为null:

library(shiny)
library(ggvis)

runApp(list(ui = pageWithSidebar(
  div(),
  sidebarPanel(
    sliderInput("n", "Number of points", min = 1, max = nrow(mtcars),
                value = 10, step = 1),
    uiOutput("plot_ui")
  ),
  mainPanel(
    ggvisOutput("plot"),
    tableOutput("mtc_table")
  )
)
, server= function(input, output, session) {
  # A reactive subset of mtcars
  mtc <- reactive({
    data = mtcars[1:input$n, ]
    data$long = as.character(paste0("A car with ",data$cyl," cylinders and ",data$gear," gears and ",data$carb, " carburators"))
    data
  })
  # A simple visualisation. In shiny apps, need to register observers
  # and tell shiny where to put the controls
  mtc %>%
    ggvis(~wt, ~mpg, key:= ~long) %>%
    layer_points(fill = ~factor(long)) %>%
    add_tooltip(function(data){
      paste0("Wt: ", data$wt, "<br>", "Mpg: ",as.character(data$mpg), "<br>", "String: ", as.character(data$long))
    }, "hover") %>%
    bind_shiny("plot", "plot_ui")

  output$mtc_table <- renderTable({
    mtc()[, c("wt", "mpg", "long")]
  })
})
)

在此输入图像描述

I have been puzzling on this for a while as well. 我一直在困惑这一点。 Seems that you can only get TOOLTIP info that is inside the ggvis() data. 看来,你只能得到TOOLTIP信息那就是ggvis()里面的数据。 So if you have ggvis(~wt, ~mp) you can show wt and mp in the tooltip. 因此,如果你有ggvis(~wt,~mp),你可以在工具提示中显示wt和mp。 If you have ggvis(~wt, ~mpg, fill= ~long) you can show wt, mp , long in the tooltip. 如果你有ggvis(~wt,~mpg,fill = ~long)你可以在工具提示中显示wt,mp,long。 Or data that is in the of the layer_.....(fill = ~long ,stroke = ~name, strokeWidth := 0). 或者是层_的数据。(fill = ~long,stroke = ~name,strokeWidth:= 0)。 (I created ~name from the row names) You need to suppress the stroke and the legend to avoid a visual impact: hide_legend("stroke") %>% (我从行名创建〜名称)你需要抑制笔画和图例以避免视觉冲击:hide_legend(“stroke”)%>%

As far as i can find ggvis limits the amount of info to be shown in the tooltip to what is contains (quite understandable) 据我所知,ggvis限制了工具提示中显示的信息量到包含的内容(非常容易理解)

If we look at the documentation: 如果我们查看文档:

***Usage***
ggvis(data = NULL, ..., env = parent.frame())

***Arguments***
data A data object.

... Property mappings. If not named, the first two mappings are taken to be x and y. 

Common properties are x, y, stroke, fill, opacity, shape

env Environment in which to evaluate properties.

so we can add x, y, stroke, fill, opacity, shape, key (must be unique values), text, font, fontsize etc or whatever is in one of the layer_..... properties. 所以我们可以添加x,y,笔划,填充,不透明度,形状,键(必须是唯一值),文本,字体,字体大小等等或其中一个图层_.....属性。

Would be nice if it was possible to add dataframe data to the ggplot that is not used in the plot but is avialible in the TOOLTIP only. 如果可以将数据帧数据添加到未在绘图中使用但仅在TOOLTIP中可用的 ggplot,那将会很好 If i find a way i'll post it here too 如果我找到了一种方法,我也会在这里发布

I added invisible NAME info only visible in the tooltip in this way(based on the previous example): 我添加了仅在工具提示中以这种方式显示的隐形名称信息(基于前面的示例):

mtc <- reactive({
  if(length(input$n)>0){
    data = mtcars[1:input$n, ]
    data$long = as.character(paste0("A car with ",data$cyl," cylinders and     ",data$gear," gears and ",data$carb, " carburators"))
    data$name <- rownames(data) 

  data %>%
    ggvis(~wt, ~mpg) %>%
    layer_points(fill = ~long ,stroke = ~name, strokeWidth := 0) %>%
    hide_legend("stroke") %>%
    add_tooltip(function(dataT){
      paste0("Car: ",dataT$name, "<br>", "Wt: ", dataT$wt, "<br>",
      "Mpg: ",as.character(dataT$mpg), "<br>", "String: ", as.character(dataT$long))
    }, "hover") %>%
    bind_shiny("plot", "plot_ui")

  data
  }
})

Suggestions for a better and more stable solution are more than welcome ! 对于更好,更稳定的解决方案的建议非常欢迎! ;^) ; ^)


OK, i found a way to show all data in the tooltip that is not in the ggvis plot: (see also: Add data to ggvis tooltip that's contained in the input dataset but not directly in the vis ) 好的,我找到了一种方法来显示工具提示中不在ggvis图中的所有数据:(另请参阅: 向ggvis工具提示添加数据,该数据包含在输入数据集中但不直接在vis中

The key is a unique !!! 关键是独一无二!!! link to another data set and a function outside the ggvis() that returns the data and info to show in the TOOLTIP. 链接到另一个数据集和ggvis() 外部的函数,该函数返回要在TOOLTIP中显示的数据和信息

I changed my code to: 我将代码更改为:

mtc <- reactive({
  if(length(input$n)>0){
    data = mtcars[1:input$n, ]
    data$long = as.character(paste0("A car with ",data$cyl," cylinders and ",data$gear," gears and ",data$carb, " carburators"))
    data$name <- rownames(data) 


  all_values <- function(x) {
    if(is.null(x)) return(NULL)
    row <- data[data$name == x$name, ]
    paste0(names(row), ": ", format(row), collapse = "<br />")
  }

  data %>%
    ggvis(~wt, ~mpg, key := ~name) %>%
    layer_points(fill = ~long) %>%

    add_tooltip(all_values, "hover")  %>%
#       add_tooltip(function(dataT){
#       paste0("Car: ",dataT$name, "<br>", "Wt: ", dataT$wt, "<br>", "Mpg: ",as.character(dataT$mpg), "<br>", "String: ", as.character(dataT$long))
#       }, "hover") %>%
    bind_shiny("plot", "plot_ui")

  data
  }
})

This way you can change the tooltip info in every way you want ! 这样您就可以以您想要的任何方式更改工具提示信息!

在此输入图像描述


AND to TOP things if you can combine the two (Hover -> summary info && Click -> ALL info) in this way: 如果你可以用这种方式将两者结合起来(Hover - >摘要信息&&点击 - >所有信息):

mtc <- reactive({
  if(length(input$n)>0){
    data = mtcars[1:input$n, ]
    data$long = as.character(paste0("A car with ",data$cyl," cylinders and ",data$gear," gears and ",data$carb, " carburators"))
    data$name <- rownames(data) 


  all_values <- function(x) {
    if(is.null(x)) return(NULL)
    row <- data[data$name == x$name, ]
    paste0(names(row), ": ", format(row), collapse = "<br />")
  }

  data %>%
    ggvis(~wt, ~mpg, key := ~name) %>%
    layer_points(fill = ~long) %>%

    add_tooltip(all_values, "click")  %>%
    add_tooltip(function(dataT){
      paste0("Car: ",dataT$name, "<br>", "Wt: ", dataT$wt, "<br>", "Mpg: ",as.character(dataT$mpg), "<br>", "String: ", as.character(dataT$long))
      }, "hover") %>%
    bind_shiny("plot", "plot_ui")

  data
  }
})

Just to be complete ! 只是为了完成!

To be even completer -> the tooltip is just HTML code as string -> so in a way you can you can create whatever HTML page you want to show up. 甚至完成 - >工具提示只是HTML代码作为字符串 - >所以在某种程度上你可以创建你想要显示的任何HTML页面。 You just can not point at it because as soon as you leave the point it will disapear ! 你只是不能指出它,因为一旦你离开这一点它就会消失! (but you can have a click action at the same point to supplement (for instance redirect the page) the hover action.) (但您可以在同一点点击操作以补充(例如重定向页面)悬停操作。)

just a last short example of a image in a tooltip: 只是工具提示中图像的最后一个简短示例:

  add_tooltip(function(img){'<img src="pic_mountain.jpg" alt="Mountain View" style="width:100px;height:100px;">'}, "hover") %>%

(sorry for the long answer) (对不起,答案很长)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM