2

I have been creating a data viewer app using shiny and plotly. I want to make a create a multi dimensional scaling view of my data, and then click on a data point to be able to view the individual point as a barplot. I was inspired by this example.

Here is a minimal almost working example:

The ui.r file

library(shiny)
library(mlbench)
library(plotly)
library(shinythemes)
library(dplyr)

# Load the data
allDat <- iris[,-5]

# ui.R definition
ui <- fluidPage(
  # Set theme
  theme = shinytheme("spacelab"),

  # Some help text
  h2("Inspect each element in iris data set"),
  h4("This a shiny app exploiting coupled events in plotly"),
  tags$ol(
    tags$li("The first chart showcases", tags$code("plotly_click"))
  ),

  # Vertical space
  tags$hr(),

  # First row
  fixedRow(
    column(6, plotlyOutput("Plot1", height = "600px")),
    column(6, plotlyOutput("Plot2", height = "600px"))),

  tags$hr())

The server.r file

# server.R definition
server <- function(input, output){

  d <- dist(allDat) # euclidean distances between the rows
  fit <- cmdscale(d,eig=TRUE, k=2) # k is the number of dim

  # plot solution
  x <- fit$points[,1]
  y <- fit$points[,2]
  plot.df <- data.frame(x=x,y=y,allDat)

  output$Plot1 <- renderPlotly({
    plot_ly(plot.df, x = x, y = y, mode="markers", source = "mds") %>%
      layout(title = "MDS of iris data",
             plot_bgcolor = "6A446F")
  })

  # Coupled event 2
  output$Plot2 <- renderPlotly({

    # Try to get the mouse click data
    event.data <- event_data("plotly_click", source = "mds")

    # If NULL dont do anything
    if(is.null(event.data) == T) return(NULL)

    # I need the row of the data in the allDat data frame
    # pretty sure this is not correct
    ind <- as.numeric(event.data[2])

    p1 <- plot_ly(x = colnames(allDat), y=as.numeric(allDat[ind,]),type="bar")

  })

}

To run this, put these two files in a folder called something, e.g. dataViewer, then run runApp("dataViewer") from the directory that contains the dataViewer folder.

What is the question and what am I seeking?

I do not understand the output that comes from the event_data function. I want to be able to click on a point on the scatter plot and extract the row number of that data point from the allDat data frame, or the plot.df data frame, because it should be the same. Then I want to use that row number to further visualize that specific point in the barplot on the right.

Gumeo
  • 891
  • 1
  • 13
  • 26

1 Answers1

1

I looked into the event.data object, and think the value you are looking for is event.data$pointNumber (which starts with 0 so you need to use event.data$pointNumber + 1 to identify the line).

event.data is a list with four names: curveNumber, pointNumber, x and y.

Xiongbing Jin
  • 11,779
  • 3
  • 47
  • 41
  • thanks for the response! I was looking [here](https://plot.ly/r/shiny-coupled-events/) and there is a comment on this: "pointNumber: Index of the data point being charted. Note that this is linked to curveNumber and starts from 0". So this starts from 0 and is somehow linked to the `curveNumber`, which I have no idea what is. So is this then the index in the data frame used to plot, or some other index? – Gumeo May 30 '16 at 18:18
  • Ok, cheers! Hope this will help others that might run into this! I am new to plotly and shiny, but if I may ask, how did you inspect the event.data object? – Gumeo May 30 '16 at 18:27
  • Edit: I must have miscounted, but it does seem that `pointNumber` starts with 0, so you should use `event.data$pointNumber + 1` to identify the line. – Xiongbing Jin May 30 '16 at 18:28
  • It might then be that the index in the data frame is the `pointNumber` index + 1. – Gumeo May 30 '16 at 18:30
  • 1
    You can use http://shiny.rstudio.com/articles/debugging.html to debug your application, although I simply added a textOutput to display a summary of the object and the names – Xiongbing Jin May 30 '16 at 18:30
  • I figured it out, the index I need is `ind <- event.data$pointNumber + 1` – Gumeo May 30 '16 at 18:44
  • 1
    just for reference for others - it isn't recommended to use `pointNumber` to find rows. Please see this [answer](https://stackoverflow.com/a/46306186/2868715) – asifzuba Mar 16 '18 at 03:13