An introduction to shiny apps
Jack Dolgin
Statistics
Teaching
Running surveys
Upload and perform eye-tracking analyses without coding
Upload and analyze EEG data without coding
Build pseudo-websites for studies
Analyze brain data
Interface
Server
Interface
Server
# Hint: https://gallery.shinyapps.io/077-widget-slider/
sliderInput("timeRange",
label = "Time range to plot",
min = -199,
max = 598,
value = c(-100, 400))
function(input, output){
}
sliderInput("timeRange",
label = "Time range to plot",
min = -199,
max = 598,
value = c(-100, 400))
# tidyverse is already loaded for you
library(tidyverse)
# imports data and helper function
source("setup_helper.R")
function(input, output){
# Delete this comment, replace
# with missing line
# This line calls ggplot(data, aes) and
# other helper functions for plotting
graph_setup +
# Replace the two underscores with the
# correct values
coord_cartesian(xlim = _____, ____)
}
# The dataset and most of this code are taken from https://github.com/craddm/ERPdemo/blob/master/ERPdemo/app.R
# You won't have to make any changes to this script
dataset <- read_csv(
"https://raw.githubusercontent.com/craddm/ERPdemo/master/ERPdemo/data/levCatObjNon.csv",
c("Object", "Non-Object",
"Time", "Subject")) %>%
mutate(
Difference = Object - `Non-Object`) %>%
gather(condition, amplitude,
-Time, -Subject) %>%
mutate(
effectType = factor(if_else(
condition == "Difference", "Difference",
"Main"
)),
condition = factor(
condition,
levels = c("Object", "Non-Object",
"Difference"),
labels = c("Object", "Non-Object",
"Difference")
)
)
dataset$Subject <- factor(dataset$Subject)
dataset$condition <- factor(dataset$condition)
graph_setup <- ggplot(data = dataset,
aes(Time, amplitude)) +
list(
scale_color_brewer(palette = "Set1"),
theme_minimal(),
stat_summary(
data = dataset,
fun = mean,
geom = "line",
size = 1,
aes(colour = condition)
),
scale_colour_discrete(
limits = levels(dataset$condition)),
labs(
x = "Time (ms)",
y = expression(paste(
"Amplitude (",mu,"V)")),
colour = ""),
geom_vline(xintercept = 0,
linetype = "dashed" ),
geom_hline(yintercept = 0,
linetype = "dashed"),
theme(
axis.text = element_text(size = 14),
axis.title = element_text(size = 14),
legend.text = element_text(size=14))
)
fluidPage(
sliderInput("timeRange",
label = "Time range to plot",
min = -199,
max = 598,
value = c(-100, 400)),
plotOutput("main_plot"))
# tidyverse is already loaded for you
library(tidyverse)
# imports data and helper function
source("setup_helper.R")
function(input, output){
output$main_plot <- renderPlot({
# This line calls ggplot(data, aes) and
# other helper functions for plotting
graph_setup +
coord_cartesian(
xlim = c(input$timeRange[1],
input$timeRange[2]))
})
}
# The dataset and most of this code are taken from https://github.com/craddm/ERPdemo/blob/master/ERPdemo/app.R
# You won't have to make any changes to this script
dataset <- read_csv(
"https://raw.githubusercontent.com/craddm/ERPdemo/master/ERPdemo/data/levCatObjNon.csv",
c("Object", "Non-Object",
"Time", "Subject")) %>%
mutate(
Difference = Object - `Non-Object`) %>%
gather(condition, amplitude,
-Time, -Subject) %>%
mutate(
effectType = factor(if_else(
condition == "Difference", "Difference",
"Main"
)),
condition = factor(
condition,
levels = c("Object", "Non-Object",
"Difference"),
labels = c("Object", "Non-Object",
"Difference")
)
)
dataset$Subject <- factor(dataset$Subject)
dataset$condition <- factor(dataset$condition)
graph_setup <- ggplot(data = dataset,
aes(Time, amplitude)) +
list(
scale_color_brewer(palette = "Set1"),
theme_minimal(),
stat_summary(
data = dataset,
fun = mean,
geom = "line",
size = 1,
aes(colour = condition)
),
scale_colour_discrete(
limits = levels(dataset$condition)),
labs(
x = "Time (ms)",
y = expression(paste(
"Amplitude (",mu,"V)")),
colour = ""),
geom_vline(xintercept = 0,
linetype = "dashed" ),
geom_hline(yintercept = 0,
linetype = "dashed"),
theme(
axis.text = element_text(size = 14),
axis.title = element_text(size = 14),
legend.text = element_text(size=14))
)
fluidPage(
sliderInput("timeRange",
label = "Time range to plot",
min = -199,
max = 598,
value = c(-100, 400)),
plotOutput("main_plot"))
# tidyverse is already loaded for you
library(tidyverse)
# imports data and helper functions
source("setup_helper.R")
function(input, output){
output$main_plot <- renderPlot({
# This line calls ggplot(data, aes) and
# other helper functions for plotting
graph_setup +
coord_cartesian(
xlim = c(input$timeRange[1],
input$timeRange[2]))
})
}
# The dataset and most of this code are taken from https://github.com/craddm/ERPdemo/blob/master/ERPdemo/app.R
# You won't have to make any changes to this script
dataset <- read_csv(
"https://raw.githubusercontent.com/craddm/ERPdemo/master/ERPdemo/data/levCatObjNon.csv",
c("Object", "Non-Object",
"Time", "Subject")) %>%
mutate(
Difference = Object - `Non-Object`) %>%
gather(condition, amplitude,
-Time, -Subject) %>%
mutate(
effectType = factor(if_else(
condition == "Difference", "Difference",
"Main"
)),
condition = factor(
condition,
levels = c("Object", "Non-Object",
"Difference"),
labels = c("Object", "Non-Object",
"Difference")
)
)
dataset$Subject <- factor(dataset$Subject)
dataset$condition <- factor(dataset$condition)
graph_setup <- ggplot(data = dataset,
aes(Time, amplitude)) +
list(
scale_color_brewer(palette = "Set1"),
theme_minimal(),
stat_summary(
data = dataset,
fun = mean,
geom = "line",
size = 1,
aes(colour = condition)
),
scale_colour_discrete(
limits = levels(dataset$condition)),
labs(
x = "Time (ms)",
y = expression(paste(
"Amplitude (",mu,"V)")),
colour = ""),
geom_vline(xintercept = 0,
linetype = "dashed" ),
geom_hline(yintercept = 0,
linetype = "dashed"),
theme(
axis.text = element_text(size = 14),
axis.title = element_text(size = 14),
legend.text = element_text(size=14))
)
add_check_boxes <- function(myval){
if (is.null(myval) == FALSE){
list(
geom_line(
data = filter(dataset,
effectType %in% myval),
aes(group = interaction(
Subject, condition),
colour = condition,
alpha = 0.2
)),
guides(alpha = "none")
)
}
}
fluidPage(
titlePanel("Exploring ERP plot options"),
sidebarLayout(
sidebarPanel(
sliderInput("timeRange",
label = "Time range to plot",
min = -199,
max = 598,
value = c(-100, 400)),
checkboxGroupInput(
"indivEffects",
label = h4("Individual effects"),
choices = list("Main effects" = "Main",
"Difference waves" = "Difference"),
inline = TRUE
)
),
mainPanel(
plotOutput("main_plot")
)
)
)
# tidyverse is already loaded for you
library(tidyverse)
# imports data and helper functions
source("setup_helper.R")
function(input, output){
output$main_plot <- renderPlot({
# This line calls ggplot(data, aes) and
# other helper functions for plotting
graph_setup +
coord_cartesian(
xlim = c(input$timeRange[1],
input$timeRange[2])) +
add_check_boxes(input$indivEffects)
})
}
# The dataset and most of this code are taken from https://github.com/craddm/ERPdemo/blob/master/ERPdemo/app.R
# You won't have to make any changes to this script
dataset <- read_csv(
"https://raw.githubusercontent.com/craddm/ERPdemo/master/ERPdemo/data/levCatObjNon.csv",
c("Object", "Non-Object",
"Time", "Subject")) %>%
mutate(
Difference = Object - `Non-Object`) %>%
gather(condition, amplitude,
-Time, -Subject) %>%
mutate(
effectType = factor(if_else(
condition == "Difference", "Difference",
"Main"
)),
condition = factor(
condition,
levels = c("Object", "Non-Object",
"Difference"),
labels = c("Object", "Non-Object",
"Difference")
)
)
dataset$Subject <- factor(dataset$Subject)
dataset$condition <- factor(dataset$condition)
graph_setup <- ggplot(data = dataset,
aes(Time, amplitude)) +
list(
scale_color_brewer(palette = "Set1"),
theme_minimal(),
stat_summary(
data = dataset,
fun = mean,
geom = "line",
size = 1,
aes(colour = condition)
),
scale_colour_discrete(
limits = levels(dataset$condition)),
labs(
x = "Time (ms)",
y = expression(paste(
"Amplitude (",mu,"V)")),
colour = ""),
geom_vline(xintercept = 0,
linetype = "dashed" ),
geom_hline(yintercept = 0,
linetype = "dashed"),
theme(
axis.text = element_text(size = 14),
axis.title = element_text(size = 14),
legend.text = element_text(size=14))
)
add_check_boxes <- function(myval){
if (is.null(myval) == FALSE){
list(
geom_line(
data = filter(dataset,
effectType %in% myval),
aes(group = interaction(
Subject, condition),
colour = condition,
alpha = 0.2
)),
guides(alpha = "none")
)
}
}
But the app can be downloaded via Github and run locally for unlimited time
Putting it all together