Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interactive hovering Shiny graphs

Is it possible to create an interactive Shiny graph that when you hover over the graph a subsequent graph appears? Right now I have two graphs in the same panel.

This is the closest logic example I can find, although the subsequent (zoomed) plot is already there (whereas I only want the subsequent graph to appear when the mouse hovers over the main graph) :

like image 321
Una Avatar asked Feb 10 '23 05:02

Una


1 Answers

You can use the hover option in the plotOutput of the ui.R, and couple it with a conditional panel to only display the second plot when the user is hovering over the first one.

I modified the example you posted with these changes:

library(ggplot2)
library(Cairo)   # For nicer ggplot2 output when deployed on Linux
library(shiny)

ui <- fluidPage(
        fluidRow(
                column(width = 8, class = "well",
                       h4("Left plot controls right plot"),
                       fluidRow(
                               column(width = 6,
                                      plotOutput("plot2", height = 300,
                                                 brush = brushOpts(
                                                         id = "plot2_brush",
                                                         resetOnNew = TRUE
                                                 ),
                                                 #add the hover options
                                                 hover = hoverOpts(
                                                         id = "plot2_hover",
                                                         nullOutside = TRUE
                                                 )
                                      )
                               ),
                               column(width = 6,
                                      #the second plot will be hidden if the user's mouse is not on the first one
                                      conditionalPanel(
                                              condition = "input.plot2_hover != null",
                                                 plotOutput("plot3", height = 300)
                                      )
                               )
                       )
                )

        )
)

server <- function(input, output) {
        ranges2 <- reactiveValues(x = NULL, y = NULL)

        output$plot2 <- renderPlot({
                ggplot(mtcars, aes(wt, mpg)) +
                        geom_point()
        })

        output$plot3 <- renderPlot({
                ggplot(mtcars, aes(wt, mpg)) +
                        geom_point() +
                        coord_cartesian(xlim = ranges2$x, ylim = ranges2$y)
        })

        # When a double-click happens, check if there's a brush on the plot.
        # If so, zoom to the brush bounds; if not, reset the zoom.
        observe({
                brush <- input$plot2_brush
                print(input$plot2_hover)
                if (!is.null(brush)) {
                        ranges2$x <- c(brush$xmin, brush$xmax)
                        ranges2$y <- c(brush$ymin, brush$ymax)

                } else {
                        ranges2$x <- NULL
                        ranges2$y <- NULL
                }
        })

}

shinyApp(ui, server)
like image 93
NicE Avatar answered Feb 11 '23 22:02

NicE