Interactive base R plot in shiny


#1

Has anyone converted a base R plot for a data frame into an interactive plot to identify specific points? The base R plot shows all variables against each other in a well formatted view with the names of the variables on the diagonal. Ideally, if you clicked on a specific point in any plot, you could show the raw data for that point. I've done this before for various ggplots before but not with the base R plot for a data frame. I could probably rebuild the base R plot with ggplot but figured I'd see if anyone has approached this type of problem before.

Basic example of an app:

library(shiny)

ui <- fluidPage(
    
    plotOutput("plot", click = "plot_click")
    
)
server <- function(input, output, session) {
    
    output$plot <- renderPlot({
        plot(mtcars)
    })
    
}

shinyApp(ui, server)

#2

The actual elements of the plot (in base or ggplot2) aren't interactive on their own (to my knowledge), but the various interactive charting libraries don't all require ggplot (I think the ggplotly() stuff is just one part of the plotly R library. Using shiny without one of those additional libraries, you can certainly use base R plotting, though I don't think you'll have anything to the effect of "plot click" by default.

The shiny app in the gallery here even lets you toggle between base and ggplot:
https://shiny.rstudio.com/gallery/plot-interaction-advanced.html


#3

identify and locator work with base graphics, but I have no idea if or how it would work in a shiny app. I haven't used either of those since the Dark Ages. Even then, I did it once and swore off ever doing it again.


#4

Thanks for the quick responses. The elements in the rendered base plot can be interactive by adding click = "plot_click" to the plotOutput (even in the shiny gallery example, this is the case). The problem is those click events return the X-Y position of the click, which is useful for a single plot but when rendering a data frame plot, the position is not as useful (or at least I have yet to figure out how to transform those positions into the actual point).

For example, turning the below plot (i.e. plot(mtcars)) into an interactive plot is straight forward in shiny. However, when clicking on a specific point (e.g. an outlier), identifying which point that is in the data frame is what I am trying to solve.


#5

Right. That's why I mentioned plotly and the like. I think rbokeh, and highcharter do this as well. You might want to take a look at the htmlwidgets gallery:
http://www.htmlwidgets.org/showcase_rbokeh.html

Nathan Yau (http://flowingdata.com/) is definitely a base-user, and he does lots of interactives, but IIRC, most of them involve d3 and JavaScript.

Definitely a lot of interactive options discussed in the thread below, as well:

Looks like pairsD3 is pretty much built for your use case:


#6

Awesome, thanks for the pairsD3 recommendation. This looks promising.