Sahiti Baddula
28 September 2026
A Shiny app that predicts a car's miles per gallon from four specifications you choose.
Buyers comparing cars want a single question answered: how much fuel will this car actually use? Specifications are easy to find, but turning them into an expected MPG requires a model.
The mtcars dataset gives us 32 cars with both specifications and measured fuel
economy, so the relationship can be estimated directly:
Both weight and horsepower are strongly negatively related to fuel economy — but neither alone tells the whole story.
The app fits one linear model to all 32 cars, using weight, horsepower, cylinder count and transmission type:
fit <- lm(mpg ~ wt + hp + factor(cyl) + factor(am), data = mtcars)
round(summary(fit)$adj.r.squared, 3)
[1] 0.84
That explains 84% of the variation in fuel economy. A worked prediction for a 2,500 lb, 110 hp, 4-cylinder manual car:
newcar <- data.frame(wt = 2.5, hp = 110, cyl = 4, am = 1)
round(predict(fit, newcar, interval = "prediction"), 1)
fit lwr upr
1 25.7 20.5 31
So: 25.7 MPG, with a 95% prediction interval of 20.5 to 31.0.
Input — four widgets in the sidebar: two sliders (weight, horsepower) and two radio button groups (cylinders, transmission), plus a checkbox to toggle the prediction interval.
Server calculation — a reactive expression rebuilds the input data frame on every
change and calls predict() on the fitted model. Nothing is precomputed.
Reactive output — the predicted MPG, its prediction interval, a percentile comparison against the dataset, a scatterplot placing your car among the 32 real cars, and a table of the five most similar actual vehicles.
The app also warns when your inputs fall outside the observed data, so users know when a prediction is an extrapolation rather than an interpolation.
The app answers the question in under a second, with no R knowledge required — move a slider, read the number.
Three things that make it useful:
Full documentation is built into the app's second tab — no external links needed.