show with app
library(shiny)
library(dplyr)
library(ggplot2)

ui <-
  fluidPage(
    checkboxGroupInput(inputId = "cyls",
                       label = "Number of cylinders:",
                       choices = c(4, 6, 8),
                       selected = c(4, 6, 8)),
    
    plotOutput("plot"),
    
    textOutput("text")
  )

server <-
  function(input, output) {
    
    dat <-
      reactive ({
        validate(need(try(length(input$cyls) > 0),
                      "Please make at least one selection for cylinders.")
        )
        mtcars %>% filter(cyl %in% input$cyls)
      })
    
    coefs <-
      reactive({
        lm(mpg ~ wt, data = dat()) %>% coef()
      })
    
    output$plot <-
      renderPlot({
        ggplot(dat(), aes(wt, mpg)) +
          geom_point() +
          geom_abline(intercept = coefs()[1], slope = coefs()[2], linetype = 2)
      })
    
    output$text <-
      renderText({
        paste("The line has intercept", 
              round(coefs()[1], 2), 
              "and slope", 
              round(coefs()[2], 2)
        )      
      })
    
  }

shinyApp(ui = ui, server = server, options = list(display.mode = "showcase"))