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

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

server <-
  function(input, output) {
    
    dat <-
      reactive ({
        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"))