Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ggplot2 to plotly to shiny to box/lasso select to DT
#plotly box or lasso select linked to
# DT data table
# using Wage data
# the out group: is sex:Male, region:Middle Atlantic +
library(ggplot2)
library(plotly)
library(dplyr)
library(ISLR)
library(shiny)
library(DT)
#reactive app
ui <- fluidPage(
# Set theme
# theme = shinytheme("spacelab"),
fluidRow(
column(12, plotlyOutput("plot", height = "600px")),
column(12,DT::dataTableOutput('data_table'))
# column(12, verbatimTextOutput("text"))
)
)
server <- function(input, output){
output$plot <- renderPlotly({
req(data())
p<-ggplot(data = data()$data, mapping = aes(x = age, y = wage)) +
geom_point() + theme_bw()
obj<-data()$sel
if(nrow(obj)!=0) {
p<-p + geom_point(data=obj,color="red",size=4)
}
ggplotly(p,source="master")
})
#selected
selected<-reactive({
# event_data("plotly_click", source = "master")
event_data("plotly_selected", source = "master")
})
output$text <- renderPrint({
list(selection=selected(),
dims=data()$sel)
})
output$data_table<-DT::renderDataTable(
data()$sel, filter = 'top', options = list(
pageLength = 5, autoWidth = TRUE
)
)
#reactive data
data<-reactive({
tmp<-Wage
sel<-tryCatch(Wage[(selected()$pointNumber+1),,drop=FALSE] , error=function(e){NULL})
list(data=tmp,sel=sel)
})
}
shinyApp(ui,server)
other attached packages:
[1] DT_0.2 shiny_0.13.2 ISLR_1.0 dplyr_0.5.0
[5] plotly_3.6.0 ggplot2_2.1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.