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

时间:2021-10-31 04:44:23

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图,但我添加了一个长字符串,这是一个长字符串,由于某种原因,我的悬停弹出窗口显示wt和mpg,但不显示长。

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")]
   })
})

2 个解决方案

#1


11  

You need to add long as a key currently data$long is null in the anonymous function supplied to add_tooltip:

您需要在提供给add_tooltip的匿名函数中添加long作为键,当前数据$ 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")]
  })
})
)

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

#2


11  

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. So if you have ggvis(~wt, ~mp) you can show wt and mp in the tooltip. If you have ggvis(~wt, ~mpg, fill= ~long) you can show wt, mp , long in the tooltip. Or data that is in the of the layer_.....(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") %>%

我也一直困惑于此。似乎您只能获取ggvis()数据中的TOOLTIP信息。因此,如果您有ggvis(~wt,~mp),您可以在工具提示中显示wt和mp。如果你有ggvis(~wt,~mpg,fill = ~long)你可以在工具提示中显示wt,mp,long。或者是层_.....中的数据(fill = ~long,stroke = ~name,strokeWidth:= 0)。 (我从行名创建〜名称)你需要抑制笔画和图例以避免视觉冲击: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. If i find a way i'll post it here too

如果可以将数据帧数据添加到未在绘图中使用但仅在TOOLTIP中可用的ggplot,那将会很好。如果我找到一种方法,我也会在这里发布

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 !

这样您就可以以您想要的任何方式更改工具提示信息!

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


AND to TOP things if you can combine the two (Hover -> summary info && Click -> ALL info) in this way:

如果你可以用这种方式将两者结合起来(Hover - >摘要信息&& Click - > ALL info):

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. 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.)

甚至完成 - >工具提示只是HTML代码作为字符串 - >所以在某种程度上你可以创建你想要显示的任何HTML页面。你只是不能指出它,因为一旦你离开这一点它就会消失! (但您可以在同一点进行点击操作以补充(例如重定向页面)悬停操作。)

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)

(对不起,答案很长)

#1


11  

You need to add long as a key currently data$long is null in the anonymous function supplied to add_tooltip:

您需要在提供给add_tooltip的匿名函数中添加long作为键,当前数据$ 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")]
  })
})
)

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

#2


11  

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. So if you have ggvis(~wt, ~mp) you can show wt and mp in the tooltip. If you have ggvis(~wt, ~mpg, fill= ~long) you can show wt, mp , long in the tooltip. Or data that is in the of the layer_.....(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") %>%

我也一直困惑于此。似乎您只能获取ggvis()数据中的TOOLTIP信息。因此,如果您有ggvis(~wt,~mp),您可以在工具提示中显示wt和mp。如果你有ggvis(~wt,~mpg,fill = ~long)你可以在工具提示中显示wt,mp,long。或者是层_.....中的数据(fill = ~long,stroke = ~name,strokeWidth:= 0)。 (我从行名创建〜名称)你需要抑制笔画和图例以避免视觉冲击: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. If i find a way i'll post it here too

如果可以将数据帧数据添加到未在绘图中使用但仅在TOOLTIP中可用的ggplot,那将会很好。如果我找到一种方法,我也会在这里发布

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 !

这样您就可以以您想要的任何方式更改工具提示信息!

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


AND to TOP things if you can combine the two (Hover -> summary info && Click -> ALL info) in this way:

如果你可以用这种方式将两者结合起来(Hover - >摘要信息&& Click - > ALL info):

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. 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.)

甚至完成 - >工具提示只是HTML代码作为字符串 - >所以在某种程度上你可以创建你想要显示的任何HTML页面。你只是不能指出它,因为一旦你离开这一点它就会消失! (但您可以在同一点进行点击操作以补充(例如重定向页面)悬停操作。)

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)

(对不起,答案很长)