Skip to content

Instantly share code, notes, and snippets.

@faragocsaba
Created March 15, 2021 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faragocsaba/c81c8f345915913022c15e656d74cab3 to your computer and use it in GitHub Desktop.
Save faragocsaba/c81c8f345915913022c15e656d74cab3 to your computer and use it in GitHub Desktop.
Interactive model
library(shiny)
shinyServer(function(input, output) {
model <- reactive({
brushed_data <- brushedPoints(trees, input$brush1, xvar = "Girth", yvar = "Volume")
if (nrow(brushed_data) < 2) {
return(NULL)
}
lm(Volume ~ Girth, data = brushed_data)
})
output$slopeOut <- renderText({
if (is.null(model())) {
"No Model Found"
} else {
model()[[1]][2]
}
})
output$intOut <- renderText({
if (is.null(model())) {
"No Model Found"
} else {
model()[[1]][1]
}
})
output$plot1 <- renderPlot({
plot(trees$Girth, trees$Volume, xlab = "Girth",
ylab = "Volume", main = "Tree Measurements",
cex = 1.5, pch = 16, bty = "n")
if (!is.null(model())) {
abline(model(), col = "blue", lwd = 2)
}
})
})
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"
))
)
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment