-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path04_simple.R
74 lines (63 loc) · 2.31 KB
/
04_simple.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
library(shiny)
library(ggplot2)
library(dplyr)
library(tidyr)
ui <- fluidPage(
fluidRow(
column(width = 12,
# Clicking the button increases the value of input$do_sample by 1
actionButton("do_sample", "Moar!"),
plotOutput("sample_plot"),
textOutput("sample_result")
)
)
)
server <- function(input, output) {
# Initialize the reactive value at 10.
# The reactive value can store intermediate values
# while keeping them in "reactive context"
sample_count <- reactiveVal(10)
# When the button is clicked, input$do_sample changes
# and triggers the contents of the "observeEvent" expression
# to run. The observeEvent does not return a value like "render"
# expressions do.
observeEvent(input$do_sample,{
x <- sample_count() + 10
sample_count(x)
})
sample_dat <- reactive({
# When observeEvent is triggers, it changes the value of
# sample_count(), so everything depending on sample_count()
# recalculates. Here it regenerates the data frame.
data.frame("rowid" = paste0("row", seq(1, sample_count())),
"thicc bois" = rnorm(sample_count(), 0, 1),
"chonkers" = rnorm(sample_count(), 0.2, 1),
stringsAsFactors = FALSE,
check.names = FALSE)
})
output$sample_plot <- renderPlot({
sample_dat() %>%
gather(population, measurement, -rowid) %>%
ggplot(aes(x = population, y = measurement, fill = population)) +
geom_boxplot() +
geom_point(position = position_jitterdodge(0.5)) +
ggtitle(paste0("You sampled ", nrow(sample_dat()), " absolute units")) +
theme(axis.text = element_text(size = rel(2)),
axis.title = element_text(size = rel(2)),
legend.text = element_text(size = rel(1.5)),
legend.title = element_text(size = rel(1.5)),
plot.title = element_text(size = rel(2)))
})
output$sample_result <- renderText({
p <- t.test(sample_dat()[["thicc bois"]],
sample_dat()[["chonkers"]],
paired = FALSE)[["p.value"]]
if(p >= 0.05){
paste("p-value = ", p)
} else {
paste("p-value = ", p, "*")
}
})
}
# Run the application
shinyApp(ui = ui, server = server)