Skip to content

Instantly share code, notes, and snippets.

@seankross
Last active May 31, 2017 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seankross/f9ac7af3cc1f67c4672240f5f4e08e8c to your computer and use it in GitHub Desktop.
Save seankross/f9ac7af3cc1f67c4672240f5f4e08e8c to your computer and use it in GitHub Desktop.
library(shiny)
library(dplyr)
delete_last_row <- function(df){
df[1:(nrow(df) - 1),]
}
ui <- basicPage(
plotOutput("plot1", click = "plot_click", dblclick = "plot_dblclick")
)
server <- function(input, output) {
mycars <- mtcars %>%
select(wt, mpg)
output$plot1 <- renderPlot({
if(!is.null(input$plot_click) && input$plot_click$x > max(mtcars$wt)){
mycars <<- add_row(mycars, wt = input$plot_click$x,
mpg = input$plot_click$y)
}
if(!is.null(input$plot_dblclick)){
mycars <<- delete_last_row(mycars)
}
plot(mycars$wt, mycars$mpg, xlim = c(1.5, 8))
abline(v = max(mtcars$wt), lty = 2)
lines(lowess(mycars), col = 2)
})
}
shinyApp(ui, server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment