0

How can we get interactive coordinates(x and y) of multiple histograms in shiny. I have tried this code

#server.R
library(xts)  
shinyServer(function(input, output,session) {
output$info <- renderText({
    paste0("x=", input$plot_click$x, "\ny=", input$plot_click$y)
  })


output$plot<- renderPlot({ 
set.seed(3)
Ex <- xts(1:100, Sys.Date()+1:100)
df = data.frame(Ex,matrix(rnorm(100*3,mean=123,sd=3), nrow=100))
df<-df[,-1]
par(mfrow = c(2,2))
for(i in names(df)){
 hist(df[[i]] , main=i,xlab="x",freq=TRUE,label=TRUE,plot = TRUE)  
}

})
})

ui.R

#ui.r
mainPanel(

      tabsetPanel(type="tab",tabPanel("plot", plotOutput("plot",click = "plot_click"), verbatimTextOutput("info"))         

  )

The problem with above code is I get random coordinates of the whole plot like this

x=124.632301932263
y=20.4921068342051

instead I want to get coordinates of individual plots with its corresponding values. For example if I click any place in X1's chart I should get x and y coordinates of that chart . How can I do this?

Eka
  • 14,170
  • 38
  • 128
  • 212

1 Answers1

2

I originally was going to say that this occurs because the click is governed by the pixels of the plot instead of the data, but I am proved wrong here:

Notice that the x and y coordinates are scaled to the data, as opposed to simply being the pixel coordinates. This makes it easy to use those values to select or filter data.

I instead am going to honestly guess that within a graphics device Shiny can't tell the difference between the individual plots, to which a solution would be to create individual devices for each plot:

ui.R

library(shiny)
shinyUI(
  tabsetPanel(type="tab",
              tabPanel("plot", 
                       uiOutput("coords"),
                       uiOutput("plots")
                       )
              )
)

server.R

library(xts)  
set.seed(3)

Ex <- xts(1:100, Sys.Date() + 1:100)
df <- data.frame(Ex, matrix(rnorm(100*3, mean = 123, sd = 3), nrow = 100))
cn <- colnames(df)
df <- df[, cn[cn != "Ex"]]

n_seq <- seq(ncol(df))
shinyServer(function(input, output, session) {

  output$plots <- renderUI({
    plot_output_list <- lapply(n_seq, function(i) {
      plotOutput(paste0("plot", i), click = paste0("plot_click", i),
                 height = 250, width = 300)
    })
  })

  for (i in n_seq) {
    output[[paste0("plot", i)]] <- renderPlot({
      hist(df[[i]] , main = i, xlab = "x", freq = TRUE, label = TRUE)
    })
  }

  output$coords <- renderUI({
    coords_output_list <- lapply(n_seq, function(i) {
      renderText({
        set <- input[[paste0("plot_click", i)]]
        paste0("Plot ", i, ": x=", set$x, "\ny=", set$y)
      })
    })
  })
})
mlegge
  • 6,763
  • 3
  • 40
  • 67
  • Thanks for the code but can you tell me what is hapenning here `lapply(n_seq, function(i) { renderText({ set <- input[[paste0("plot_click", i)]]` – Eka Feb 19 '16 at 04:47
  • 1
    since the click for each plot is established as `input$plot_click`, we are using the named entries of the `input` list to grab the clicks for each plot. e.g. `x <- list(plot_click1 = list(x = 1, y = 2)); x[["plot_click1"]];` – mlegge Feb 19 '16 at 13:45