Skip to content

Instantly share code, notes, and snippets.

@dalejbarr
Last active May 5, 2019 23:42
Show Gist options
  • Save dalejbarr/26aa69cddde0100ac6087c9c0da2b919 to your computer and use it in GitHub Desktop.
Save dalejbarr/26aa69cddde0100ac6087c9c0da2b919 to your computer and use it in GitHub Desktop.
library("shiny")
library("ggplot2")
n_obs <- 500L
## Define UI for application that draws a histogram
ui <- fluidPage(
## Application title
titlePanel("Visualizing Raw Data vs Residuals"),
## Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("subj_a",
"Mean RT for Subject A:",
min = 500,
max = 900,
value = 600),
sliderInput("subj_b",
"Mean RT for Subject B:",
min = 500,
max = 900,
value = 650),
sliderInput("subj_c",
"Mean RT for Subject C:",
min = 500,
max = 900,
value = 700),
sliderInput("subj_d",
"Mean RT for Subject D:",
min = 500,
max = 900,
value = 750),
sliderInput("subj_e",
"Mean RT for Subject E:",
min = 500,
max = 900,
value = 800),
hr(),
checkboxInput("reveal", "Reveal subject distributions"),
actionButton("resample", "New Sample")
),
## Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot"),
textOutput("sw_raw"),
hr(),
plotOutput("residPlot"),
textOutput("sw_resid")
)
)
)
## Define server logic required to draw a histogram
server <- function(input, output) {
dat <- reactive({
input$resample
means <- c(input$subj_a, input$subj_b, input$subj_c,
input$subj_d, input$subj_e)
d1 <- data.frame(subject = rep(LETTERS[1:5], each = n_obs),
s_i = rep(means, each = n_obs),
e_ij = rnorm(n_obs * 5L, mean = 0, sd = 50))
d1$y_ij = d1$s_i + d1$e_ij
d1
})
output$distPlot <- renderPlot({
## generate bins based on input$bins from ui.R
if (input$reveal) {
ggplot(dat(), aes(y_ij, fill = subject)) +
geom_histogram(alpha = .2) +
ggtitle("Raw Data")
} else {
ggplot(dat(), aes(y_ij)) + geom_histogram() +
ggtitle("Raw Data")
}
})
output$residPlot <- renderPlot({
ggplot(dat(), aes(e_ij)) + geom_histogram() +
ggtitle("Residuals")
})
output$sw_raw <- renderPrint({
`(raw data)` <- dat()[["y_ij"]]
shapiro.test(`(raw data)`)
})
output$sw_resid <- renderPrint({
`(residuals)` <- dat()[["e_ij"]]
shapiro.test(`(residuals)`)
})
}
## Run the application
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment