#| '!! shinylive warning !!': |
#| shinylive does not work in self-contained HTML documents.
#| Please set `embed-resources: false` in your metadata.
#| label: lm-values
#| viewerHeight: 700
#| viewerWidth: "100%"
#| standalone: true
library(shiny)
library(ggplot2)
library(fivethirtyeight)
library(bslib)
library(dplyr)
# Create movie_scores from fandango dataset
movie_scores <- fandango %>%
rename(
critics = rottentomatoes,
audience = rottentomatoes_user
)
# Calculate actual regression line parameters
actual_model <- lm(audience ~ critics, data = movie_scores)
actual_intercept <- coef(actual_model)[1]
actual_slope <- coef(actual_model)[2]
# Set default values slightly off from actual values
default_intercept <- round(actual_intercept + 10) # Add 10 to be "slightly wrong"
default_slope <- round(actual_slope - 0.2, 2) # Subtract 0.2 to be "slightly wrong"
ui <- page_sidebar(
# title = "Linear Regression: Movie Scores Explorer",
theme = bs_theme(
version = 5,
bootswatch = "default",
base_font_size = "22px" # Increased base font size
),
# Left sidebar with inputs
sidebar = sidebar(
width = "35%", # Increased width for more space
tags$style(HTML("
.irs-min, .irs-max, .irs-single, .irs-from, .irs-to, .irs-grid-text {
font-size: 20px !important;
}
.irs-grid-pol {
height: 6px !important;
}
.irs-bar, .irs-line {
height: 12px !important;
}
.irs-handle {
width: 24px !important;
height: 24px !important;
top: 26px !important;
}
.control-label {
font-size: 24px !important;
font-weight: bold !important;
margin-bottom: 10px !important;
}
")),
sliderInput(
"intercept",
"Intercept:",
min = 0,
max = 100,
value = default_intercept,
step = 1,
width = "100%"
),
tags$div(style = "margin-top: 30px;"), # Add spacing between sliders
sliderInput(
"slope",
"Slope:",
min = -2,
max = 2,
value = default_slope,
step = 0.05,
width = "100%"
)
),
# Main panel with plot
card(
full_screen = TRUE,
plotOutput("regressionPlot", height = "500px")
)
)
server <- function(input, output) {
# Create the plot
output$regressionPlot <- renderPlot({
# User defined regression line
user_intercept <- input$intercept
user_slope <- input$slope
# Create simple plot
ggplot(movie_scores, aes(x = critics, y = audience)) +
geom_point(alpha = 0.7, size = 3) + # Larger points
geom_abline(intercept = user_intercept, slope = user_slope,
color = "red", linewidth = 1.5) + # Thicker line
labs(
x = "Critics Score",
y = "Audience Score"
) +
theme_minimal(base_size = 16) + # Larger theme elements
theme(
axis.title = element_text(size = 18, face = "bold"),
axis.text = element_text(size = 16)
)
})
}
shinyApp(ui = ui, server = server)