First export the mtcars dataset. Rename rownames to “names”. Then load it into the app
library(shiny)
library(plotly)
library(reactable)
ui <- fluidPage(
fileInput(inputId = "cars_data",
label = "Upload cars df"),
plotlyOutput("plot"),
verbatimTextOutput("selecting"),
reactableOutput("selected_table")
)
server <- function(input, output, session) {
data_reactive <-
reactive({
rio::import(input$cars_data$datapath)
})
output$plot <- renderPlotly({
req(input$cars_data$datapath)
(ggplot(data_reactive(), aes(x = mpg, y = wt, customdata = names)) +
geom_point()) %>%
ggplotly() %>%
layout(dragmode = "select") %>%
event_register("plotly_selecting") %>%
layout(font = list(family = "Avenir",
size = 15,
color = "white")) %>%
style(hoverlabel = list(bordercolor = "transparent",
font = list(family = "Avenir", ## bizarre that you have to duplicat font settings
color = "white",
size = 15)) )
})
output$selecting <- renderPrint({
req(data_reactive())
selecting <- event_data("plotly_selecting")
print(selecting$customdata)
})
output$selected_table <- renderReactable({
req(data_reactive())
data_reactive() %>%
filter(names %in% c( event_data("plotly_selecting")$customdata )) %>%
reactable::reactable()
})
}
shinyApp(ui,
server)