Rstudio shiny ggvis tooltip on mouse hover

后端 未结 2 833
孤城傲影
孤城傲影 2021-02-03 14:09

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

2条回答
  •  日久生厌
    2021-02-03 14:48

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

    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, "
    ", "Mpg: ",as.character(data$mpg), "
    ", "String: ", as.character(data$long)) }, "hover") %>% bind_shiny("plot", "plot_ui") output$mtc_table <- renderTable({ mtc()[, c("wt", "mpg", "long")] }) }) )

    enter image description here

提交回复
热议问题