I was just hoping to figure out how i can use handle_hover() or handle_click() to return a value outside of the environment of the visualization.
Ultimately i want to use this to return a key value so i can link two graphs onto a shiny application.
Using the example from the documentation, I have:
mtcars$id <- seq_len(nrow(mtcars))
hoveron<-function(data,...){
testval<<-str(data)
testval
}
mtcars %>% ggvis(~mpg, ~wt,key:=~id) %>% layer_points() %>%
handle_hover(hoveron)
This returns
'data.frame': 1 obs. of 3 variables:
$ id : int 16
$ mpg: num 10.4
$ wt : num 5.42"
In the console
What would i need to do in order to return testval= 16
Many thanks!
I do not know if this will help, as it is not clear where do you want to return the key value. But here is a simple demonstration.
There are 2 plots in this shiny app. The second plot's points are green but if you hover on a point of the first plot whose id is 25 ( you can see the id's printed on the console ), the second plot's points turn red. I used the needed_val <- reactiveValues()
to keep track if the testval is 25. You can access this value from another reactive()
context.
The ui.R -
library(ggvis)
library(shiny)
shinyUI(
mainPanel(
ggvisOutput("plot"),
ggvisOutput("plot2")
)
)
server.R -
library(shiny)
library(dplyr)
library(ggvis)
shinyServer(function(input, output, session) {
needed_val <- reactiveValues(testval = 11)
mtcars$id <- seq_len(nrow(mtcars))
hoveron<-function(data, ...){
needed_val$testval <- as.numeric(data$id)
str(needed_val$testval)
}
mtcars %>% ggvis(~mpg, ~wt,key:=~id) %>% layer_points() %>%
handle_hover(hoveron) %>% bind_shiny("plot")
dat2 <- reactive({
if(needed_val$testval == 25){
color <- "red"
}
else{
color <- "green"
}
color
})
vis <- reactive({
mtcars %>% ggvis(~mpg, ~wt) %>% layer_points(fill := dat2())
}) %>% bind_shiny("plot2")
})
Let me know if its helpful.