I am trying to highlight (ex. stroke) points on my scatterplot by clicking. So for example I have a tooltip and if the tooltip gives me some important information I want to mark this point. Is there something already available?
I already played a little bit with two tooltips, one prints some information, the other appends the id of the point to a list and I try to add this information to the data and create a new graph with highlighting the ids. Not very handy.
Here is a minimal example:
library(dplyr)
library(ggvis)
library(shiny)
library(ggplot2)
df <- data.frame(x=rnorm(10), y=rnorm(10), id=letters[1:10])
server <- function(input, output) {
movie_tooltip <- function(x) {
x$id
}
vis <- reactive({
df %>%
ggvis(~x, ~y) %>%
layer_points(key := ~id) %>%
add_tooltip(movie_tooltip, "hover")
})
vis %>% bind_shiny("plot1")
observe({
if(input$myBtn > 0){
stopApp()
}
})
}
ui <- fluidPage(
ggvisOutput("plot1"),
actionButton("myBtn", "Press ME!")
)
shinyApp(ui = ui, server = server)
How can I highlight or mark some of the dots?
UPDATE:
I got partly some results which I wanted to present so far. I can highlight a dot but I would also like to "unhighlight" them again on click.
I added a second add_tooltip function and some reactiveValues but it is not possible for me to switch back to unlabeled. It gets in kind of a loop and never stops...
Here is my updated example:
library(dplyr)
library(ggvis)
library(shiny)
library(ggplot2)
df <- data.frame(x=rnorm(10), y=rnorm(10), id=letters[1:10])
server <- function(input, output) {
movie_tooltip <- function(x) {
x$id
}
movie_tooltip2 <- function(x) {
i <- which(df$id == x$id)
# ifelse(values$stroke[i] == 'Yes',
# values$stroke[i] <- 'No',
# values$stroke[i] <- 'Yes')
values$stroke[i] <- "Yes"
return(NULL)
}
values <- reactiveValues(stroke=rep('No',nrow(df)))
vis <- reactive({
df %>%
ggvis(~x, ~y, stroke = ~values$stroke) %>%
layer_points(key := ~id) %>%
add_tooltip(movie_tooltip, "hover") %>%
add_tooltip(movie_tooltip2, "click")
})
vis %>% bind_shiny("plot1")
}
ui <- fluidPage(
ggvisOutput("plot1")
)
shinyApp(ui = ui, server = server)
If I uncomment the three #comments, and comment out this line# values$stroke[i] <- "Yes"
, I stuck in a loop and don't understand it.
I believe what is happening is that by making a change to a reactive object inside of tooltip()
you are invalidating the tooltip itself, and so you're stuck in an infinite loop.
To get around this use isolate()
around the change to the values.
library(dplyr)
library(ggvis)
library(shiny)
library(ggplot2)
df <- data.frame(x=rnorm(10), y=rnorm(10), id=letters[1:10])
server <- function(input, output) {
movie_tooltip <- function(x) {
x$id
}
movie_tooltip2 <- function(x) {
i <- which(df$id == x$id)
isolate(values$stroke[i] <- ifelse(values$stroke[i] == 'Yes',
values$stroke[i] <- 'No',
values$stroke[i] <- 'Yes'))
return(NULL)
}
values <- reactiveValues(stroke=rep('No',nrow(df)))
vis <- reactive({
df %>%
ggvis(~x, ~y, stroke = ~values$stroke) %>%
layer_points(key := ~id) %>%
add_tooltip(movie_tooltip, "hover") %>%
add_tooltip(movie_tooltip2, "click")
})
vis %>% bind_shiny("plot1")
}
ui <- fluidPage(
ggvisOutput("plot1")
)
shinyApp(ui = ui, server = server)