C9W4 Shiny Application

  • This application studies Sepal measurements (Width and Length) from Edgar Anderson's Iris Data
  • It plots all 150 points and returns the Slope and Intercept values of regression line of user-selected (click and drag highlighted) points
  • Application Link: https://sdsng.shinyapps.io/C9W4/
  • R Codes can also be found on Github: https://github.com/sdsng/C9W4-ShinyApp

R Code for Shiny Application - server.R

library(shiny)
shinyServer(function(input, output) {
        model <- reactive({
                brushed_data <- brushedPoints(iris, input$brush1,
                xvar = "Sepal.Length", yvar = "Sepal.Width")
                if(nrow(brushed_data) < 2){
                        return(NULL) }
                lm(Sepal.Width ~ Sepal.Length, data = brushed_data) })
        output$slopeOut <- renderText({ 
                if(is.null(model())){
                        "No Model Found"
                } else {
                        model()[[1]][2] 
                        }
        })
###

R Code for Shiny Application - server.R (cont.)

###
        output$intOut <- renderText({ 
                if(is.null(model())){
                        "No Model Found"
                } else {
                        model()[[1]][1] }
        })
        output$plot1 <- renderPlot({
                plot(iris$Sepal.Length, iris$Sepal.Width, 
                xlab = "Sepal Length", ylab = "Sepal Width", 
                main = "Sepal Measurements", cex = 1.5, pch = 16, bty = "n")
                if(!is.null(model())){
                        abline(model(), col = "blue", lwd = 2) }
        })
})

R Code for Shiny Application - ui.R

library(shiny) 

shinyUI(fluidPage(
        titlePanel("Visualize Many Models"), 
        sidebarLayout(
                sidebarPanel( 
                        h3("Slope"), 
                        textOutput("slopeOut"), 
                        h3("Intercept"), 
                        textOutput("intOut")
                ), 
                mainPanel(
                        plotOutput("plot1", 
                                   brush = brushOpts(
                                           id = "brush1"
                                           )
)       )       )      ))

END