Shiny Application and Reproducible Pitch Assignment

Nsovo Ntuli

2024-02-13

data("mtcars")
head(mtcars, 5)
##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2

UI.R

 library(shiny)

ui <- shinyUI(
  navbarPage("Developing Data Products Assignment",
             tabPanel("Simple Regression",
                      fluidPage(
                        titlePanel("Predict Miles per Gallon (mpg) with Simple Regression Models"),
                        sidebarLayout(
                          sidebarPanel(
                            selectInput("variable", "Select Input for Simple Regression",
                                        c("am", "cyl", "hp", "wt", "disp", "drat", "qsec", "gear", "carb")),
                            checkboxInput("simple_model", "Show simple model", value = FALSE),
                            submitButton("Submit")
                          ),
                          mainPanel()
                        )
                      )
             )
  )
)                       

Server.R

library(ggplot2)
library(olsrr)
## Warning: package 'olsrr' was built under R version 4.3.2
## 
## Attaching package: 'olsrr'
## The following object is masked from 'package:datasets':
## 
##     rivers
data(mtcars)

mtcars$cyl <- factor(mtcars$cyl)
mtcars$vs <- factor(mtcars$vs)
mtcars$gear <- factor(mtcars$gear)
mtcars$carb <- factor(mtcars$carb)
mtcars$am <- factor(mtcars$am, labels = c("Automatic", "Manual"))

shinyServer(function(input, output) {

  full_model <- lm(mpg ~ am + cyl + hp + wt + disp + drat + qsec + gear + carb, data = mtcars)
  best_model <- lm(mpg ~ am + hp + wt + disp + qsec, data = mtcars)
  
  # Rest of your Shiny app code...
})
server <- function(input, output) {
  observeEvent(input$submit_button, {
    if (input$simple_model) {
      model <- lm(mpg ~ am + hp + wt + disp + qsec, data = mtcars)
    } else {
      model <- lm(mpg ~ am + cyl + hp + wt + disp + drat + qsec + gear + carb, data = mtcars)
    }

    # Additional processing or output can be added here

    # Update the plot
    output$regression_plot <- renderPlot({
      # Example plot, replace this with your actual plotting code
      ggplot(mtcars, aes(x = input$variable, y = mpg)) +
        geom_point() +
        geom_smooth(method = "lm")
    })
  })
}