Does nearPoints() and brushedPoints() functions in Shiny work on bar-graphs?

102 views
Skip to first unread message

SivaSankara Reddy Bommireddy

unread,
Jun 24, 2015, 11:36:53 AM6/24/15
to shinyap...@googlegroups.com

I am trying to use nearPoints() function in Shiny with bar-graphs(both basic and ggplotpackages). But when the page is rendered, none of the click, dbclick, hover are not working with the bar-graphs.

My aim is to show either a dataTable or verbatimTextOutput (containing the rows of the clicked bar) when a bar on the bar-graph is clicked. I can do the above mentioned operations on a scattered plot but not on a bar graph.

The examples mentioned in the Shiny documentation page only showed the scattered plots examples. I just want to know if the nearPoints() and brushedPoints() (similar function for a range on the plot) functions work with bar-graphs or not ?

Tareef Kawaf

unread,
Jun 24, 2015, 11:57:37 AM6/24/15
to SivaSankara Reddy Bommireddy, shiny-...@googlegroups.com
Moving this to the shiny-discuss discussion list instead.

--
You received this message because you are subscribed to the Google Groups "ShinyApps Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shinyapps-use...@googlegroups.com.
To post to this group, send email to shinyap...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shinyapps-users/6179b24e-0604-4f04-9e9b-5f3fe7111482%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Winston Chang

unread,
Jun 24, 2015, 9:11:37 PM6/24/15
to SivaSankara Reddy Bommireddy, shinyap...@googlegroups.com
The nearPoints() function itself won't work with bar graphs, since it uses both the x and y distance. But you can use the x position and calculate yourself which bar the mouse is over. Here's an example from http://shiny.rstudio.com/articles/plot-interaction-advanced.html, adapted slightly to have bar graphs generated by ggplot2:


library(ggplot2)

ui <- fluidPage(
  fluidRow(
    plotOutput("plot1", height = 300, width = 300,
      click = "plot1_click",
    )
  ),
  verbatimTextOutput("x_value"),
  verbatimTextOutput("selected_rows")
)

server <- function(input, output) {
  output$plot1 <- renderPlot({
    ggplot(ToothGrowth, aes(supp)) + geom_bar(stat = "bin")
  })

  # Print the name of the x value
  output$x_value <- renderPrint({
    if (is.null(input$plot1_click$x)) return()

    lvls <- levels(ToothGrowth$supp)
    lvls[round(input$plot1_click$x)]
  })
  
  # Print the rows of the data frame which match the x value
  output$selected_rows <- renderPrint({
    if (is.null(input$plot1_click$x)) return()
    
    keeprows <- round(input$plot1_click$x) == as.numeric(ToothGrowth$supp)
    ToothGrowth[keeprows, ]
  })
}

shinyApp(ui, server)

-Winston

--
Reply all
Reply to author
Forward
0 new messages