如何在Shiny中的ggvis图上引用点击的点 [英] How do I reference a clicked point on a ggvis plot in Shiny

查看:152
本文介绍了如何在Shiny中的ggvis图上引用点击的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用单击点的值进行进一步处理,但不清楚如何引用数据。

I wish to use the values of a clicked point for further processing but am unclear how to reference the data

library(shiny)
library(ggvis)
library(dplyr)

df  <- data.frame(a=c(1,2),b=c(5,3)) 



runApp(list(
  ui = bootstrapPage(
  ggvisOutput("plot")

),

server = function(..., session) {

# function to handle click
getData = function(data,location,session){

if(is.null(data)) return(NULL)

# This returns values to console
print(glimpse(data))
# Observations: 1
# Variables:
# $ a (int) 2
# $ b (int) 3

}

 # create plot
 df %>%
  ggvis(~a, ~b) %>%
  layer_points() %>%
  handle_click(getData) %>%
  bind_shiny("plot")

# further processing

clickedData <- reactive({

 # how do I reference the value 'a' e.g. 2 of the clicked point'
})
}
))

TIA

推荐答案

这是一个可以打印data.frame的工作解决方案。您接近。

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

编辑

免责声明:直到5分钟前,我从未使用过ggvis,所以也许这不是正确的方式,但这是有效的)

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

以下是如何在UI中使用数据

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

这篇关于如何在Shiny中的ggvis图上引用点击的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆