#: standalone: true
library(shiny)
library(ggplot2)
set.seed(894)
x = sort(rnorm(20, 5, 5))
y = sort(rnorm(20, 5, 10))
lmod = lm(y ~ x)
summary(lmod)
dat <- data.frame(x = x, y = y)
ui <- fluidPage(
sidebarPanel(
sliderInput("intercept", "intercept", min = -10, max = 10, value = lmod$coefficients[1],step=0.5),
sliderInput("slope", "slope", min = 0.3, max = 5, value = lmod$coefficients[2], step=0.2)),
mainPanel(
plotOutput("plot")
)
)
server <- function(input, output, session) {
output$plot <- renderPlot(
width = 600,
height = 600,
res = 96,
{
ggplot(dat, aes(x = x, y = y)) +
geom_point() +
geom_abline(slope = input$slope, intercept = input$intercept) +
coord_fixed()+
geom_rect(inherit.aes = F, alpha = 0.2,
aes(ymin = pmin(y, (input$intercept + (input$slope*x))),
ymax = pmax(y, (input$intercept + (input$slope*x))),
xmin = pmin(x, x - (y-(input$intercept + (input$slope*x)))),
xmax = pmax(x, x - (y-(input$intercept + (input$slope*x))))))
}
)
}
# Run the application
shinyApp(ui = ui, server = server)