function - How do I reference a clicked point on a ggvis plot in Shiny -
i wish use values of clicked point further processing unclear how reference 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 handle click getdata = function(data,location,session){ if(is.null(data)) return(null) # returns values console print(glimpse(data)) # observations: 1 # variables: # $ (int) 2 # $ b (int) 3 } # create plot df %>% ggvis(~a, ~b) %>% layer_points() %>% handle_click(getdata) %>% bind_shiny("plot") # further processing clickeddata <- reactive({ # how reference value 'a' e.g. 2 of clicked point' }) } ))
tia
here's working solution prints out 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: never used ggvis in shiny until 5 minutes ago maybe isn't correct way, works)
here's how use data in 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") } ))
Comments
Post a Comment