-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathapp.R
70 lines (56 loc) · 1.74 KB
/
app.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
# Shiny app for tossing coin
# Author: Gaston Sanchez
library(shiny)
library(ggplot2)
# source toss() function
source('functions.R')
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Frequency of Heads"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("prob",
"Prob of heads",
min = 0,
max = 1,
value = 0.5),
sliderInput("times",
"Number of tosses",
min = 1,
max = 5000,
value = 100),
numericInput("seed", label = "random seed", value = 123)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw the plot
server <- function(input, output) {
output$distPlot <- renderPlot({
coin <- c('heads', 'tails')
set.seed(input$seed)
tosses <- toss(coin, times = input$times,
prob = c(input$prob, 1 - input$prob))
head_freqs <- cumsum(tosses == 'heads')
head_props <- head_freqs / (1:length(tosses))
heads_df <- data.frame(
num_tosses = 1:length(tosses),
head_props = head_props
)
# draw frequency line
ggplot(data = heads_df, aes(x = num_tosses, y = head_props)) +
geom_hline(yintercept = 0.5, col = 'gray50') +
geom_path(size = 1.5, color = '#4078d1') +
ylim(0, 1) +
xlab("Number of tosses") +
ylab("Proportion of heads") +
theme_minimal()
})
}
# Run the application
shinyApp(ui = ui, server = server)