How do I reference a clicked point on a ggvis plot in Shiny

邮差的信 提交于 2019-12-04 19:32:28

Here's a working solution that just prints out the data.frame. You're close.

df <- data.frame(a = 1:5, b = 101:105)

runApp(shinyApp(
  ui = fluidPage(
    ggvisOutput("ggvis")
  ),
  server = function(input, output, session) {
    clickFunc <- function(data, location, session) {
      cat(str(data))
    }

    df %>%
      ggvis(~ a, ~b) %>% 
      layer_points %>%
      handle_click(clickFunc) %>%
      bind_shiny("ggvis")
  }
))

EDIT:
(disclaimer: I never used ggvis in shiny until 5 minutes ago so maybe this isn't the correct way, but this works)

Here's how to use the data in your UI

df <- data.frame(a = 1:5, b = 101:105)

runApp(shinyApp(
  ui = fluidPage(
    div("a:", textOutput("aVal", inline = TRUE)),
    div("b:", textOutput("bVal", inline = TRUE)),
    ggvisOutput("ggvis")
  ),
  server = function(input, output, session) {
    clickFunc <- function(data, location, session) {
      session$output$aVal <- renderText({ data$a })
      session$output$bVal <- renderText({ data$b })
    }

    df %>%
      ggvis(~ a, ~b) %>% 
      layer_points %>%
      handle_click(clickFunc) %>%
      bind_shiny("ggvis")
  }
))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!