Retrieving the row and column information after user clicks on a heatmap

618 views
Skip to first unread message

Jiasen Yang

unread,
Aug 7, 2015, 4:41:52 PM8/7/15
to Shiny - Web Framework for R
Hi everyone,

I'm currently working on a Shiny app which requires the user to select a square in a heat map and the app retrieves the corresponding row and column information.

More specifically, I have a data frame with categorical variables X and Y and a quantitative response variable Z. I used ggplot and geom_tile() to plot a heat map with X and Y on the axes and Z as the filling variable. After displaying the heat map, I would like the user to be able to click on a square and the app stores the selected X and Y values corresponding to the selected entry in the data frame.

Related attempts:

-- The information I need is exactly like the highlighted information shown in this d3heatmap example http://rpubs.com/jcheng/nba1 . However, I would like the app to use the selected row and column information in further analysis, and I don't know how to return the information internally instead of printing.

-- I have been able to retrieve the coordinates of the clicked point using the nearPoints() function, but I couldn't transform the coordinates back to the row and column information of the data frame.

A solution using any package (base R/ggplot2/d3heatmap...) is acceptable. I would greatly appreciate any help or suggestions!

Thanks,
Jiasen

Jonah

unread,
Aug 7, 2015, 5:51:25 PM8/7/15
to Shiny - Web Framework for R
In your attempt with nearPoints(), what is it that prevents you from finding the row/column information corresponding to that point?

Jiasen Yang

unread,
Aug 10, 2015, 12:16:10 AM8/10/15
to Shiny - Web Framework for R
Here is the output i get by printing the results from nearPoints() after clicking on a square. I can get the coordinates, but I need to obtain the row and column names corresponding to the square I clicked.

input$plot_click:

$x

[1] 0.8087657

$y

[1] 0.3461

$mapping

named list
()

$domain

$domain$left

[1] -0.04

$domain$right

[1] 1.04

$domain$bottom

[1] -0.04

$domain$top

[1] 1.04

$range

$range$left

[1] 0

$range$right

[1] 397

$range$bottom

[1] 399

$range$top

[1] -1

$log

$log$x

NULL


$log$y

NULL


$
.nonce

[1] 0.6781342

Jonah

unread,
Aug 10, 2015, 10:46:44 AM8/10/15
to Shiny - Web Framework for R
In the doc for nearPoints it says this under Details:

It is also possible for this function to return all rows from the input data frame, but with an additional column selected_, which indicates which rows of the input data frame are selected by the brush (TRUE for selected, FALSE for not-selected). This is enabled by setting allRows=TRUE option. If this is used, the resulting data frame will not be sorted by distance to the mouse event.

Would having the selected_ column be helpful?  

Jiasen Yang

unread,
Aug 10, 2015, 4:45:05 PM8/10/15
to Shiny - Web Framework for R
Thanks for all the help!

Instead of using the nearPoints() function, I finally achieved the interactive effect by just rounding the click coordinates and looking up the levels of the categorical axes.

Peter N.

unread,
Oct 4, 2016, 4:26:44 PM10/4/16
to Shiny - Web Framework for R
Hi Jiasen,
How did you exactly do the interactive  http://rpubs.com/jcheng/nba1

My basic problem is nearPoints() does not give back "correct" coordinfo from geom_tile()+facet_gid

Thanks for your help!
V.
Reply all
Reply to author
Forward
0 new messages