Reactive in nearPoints?



I have a question about using a reactive DF in the nearPoints function. I can’t seem to find any info about it and can’t get it to work. (I’m relatively new to shiny and have beginner-to-moderate experience in R.)

I have a reactive that spits out a DF used for a ggplot2 (point & line plot). I am then trying to use nearPoints to get the rows of DF that a user selects (mouse click) but nothing is being returned. (Eventually, I want to use that data to produce another plot.)

in a tabsetPanel

plotOutput( "the_plot", height = 500, click = "plot_click" ),

It produces a row of column titles correctly but always shows:

"<0 rows> (or 0-length row.names)" 

when I click on a point in the plot (unless allRows = TRUE is used in the server (see below)).


DF <- reactive({ makes_some_magic_ happen & return(df) })
output$the_plot <- renderPlot({ validate( some-things ) 
    ggplot( DF(), aes(blahblah) ) + 
      geom_point() + 
      geom_line() + 

output$click_info <- renderPrint({ str(input$plot_click) })

produces the expected output (the input$plot_click list), but

output$click_info <- renderPrint({ nearPoints(DF(), input$plot_click) })

gives <0 rows> ... and

output$click_info <- renderPrint({ nearPoints(DF(), input$plot_click, allRows = TRUE) })

shows the selected_ column as always FALSE no matter which point I click.

Thanks in advance.


@freshmd Welcome to Rstudio Community! Just so you know, it is pretty hard to follow/debug your code when you only post bits and pieces of it and replace your actual code with things like make_some_magic_happen and aes(blahblah). If you create a reprex then you are much more likely to be able to get the help you need.


Thank you @tbradley. I was trying to be brief because I thought that it was basically a yes or no question. (I.e., Is using a reactive in nearPoints valid?) I added the code as an after thought to try and show what I was doing.

I apologize for the poorly formatted code. Please allow me some time to make changes that don’t use our private data. Writing code is a minor part of my job so I can’t spend much time on it.

Would it be best to make a new post with properly formatted code?


You can just post in this thread with the reproducible example. It may well be a yes or no answer, but it is still much easier to decipher what is going on with a full minimal example.