Skip to content

Instantly share code, notes, and snippets.

@KKulma
Created February 12, 2019 13:36
Show Gist options
  • Save KKulma/e02287b9f83715cebbd3e3f1b1441fab to your computer and use it in GitHub Desktop.
Save KKulma/e02287b9f83715cebbd3e3f1b1441fab to your computer and use it in GitHub Desktop.
The app demonstrates how to fetch group info information from `plot_click` attribute and use it to subset a `dataTableOutput`.
library(shiny)
library(ggplot2)
library(DT)
### The app demonstrates how to fetch group info information from `plot_click` attribute and use it to subset a `dataTableOutput`.
### redoing the SO answer from https://stackoverflow.com/a/38919892/6327771 (example 2)
ui <- fluidPage(
# selectInput("var_y", "Y-Axis", choices = names(iris)),
plotOutput("distPlot", dblclick = "plot_click"),
uiOutput("dynamic"),
dataTableOutput('table')
)
server <- function(input, output) {
output$table <- renderDataTable({
req(input$plot_click)
y <- nearPoints(iris, input$plot_click)['Species']
iris %>%
filter(Species == y$Species)
})
output$distPlot <- renderPlot({
ggplot(iris, aes(Sepal.Width, Petal.Length, group = Species, color = Species)) +
geom_point()
})
output$dynamic <- renderUI({
req(input$plot_click)
verbatimTextOutput("vals")
})
output$vals <- renderPrint({
y <- nearPoints(iris, input$plot_click)['Species']
req(nrow(y) != 0)
y
})
}
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment