Skip to content

Commit 2fdbdcb

Browse files
committed
Format R code
1 parent b4dcfd7 commit 2fdbdcb

File tree

1 file changed

+48
-37
lines changed

1 file changed

+48
-37
lines changed

app.R

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,60 @@
11
# import Shiny library
22
library(shiny)
33
# define UI component
4-
ui <- fluidPage(titlePanel("Simple Bingo Game in R Shiny"), sidebarLayout(sidebarPanel(actionButton("start",
5-
"Start Game") # button to start game
6-
, textOutput("calledNumber") # called number output
7-
), mainPanel(tableOutput("bingoGrid") # bingo grid table output
8-
)))
4+
ui <- fluidPage(
5+
titlePanel("Simple Bingo Game in R Shiny"),
6+
sidebarLayout(
7+
sidebarPanel(
8+
actionButton("start", "Start Game"), # button to start game
9+
textOutput("calledNumber") # called number output
10+
),
11+
mainPanel(
12+
tableOutput("bingoGrid") # bingo grid table output
13+
)
14+
)
15+
)
916

1017
server <- function(input, output, session) {
11-
# define server component
12-
called_numbers <- 0
13-
called_number <- reactiveValues(value = 0)
14-
number_list <- sample(1:75, 75, replace = FALSE)
15-
16-
observeEvent(input$start, {
18+
# define server component
1719
called_numbers <- 0
18-
called_number$value <- 0
19-
invalidateLater(1000, session)
20-
# when start button is pressed generate random numbers
21-
b <- sample(1:15, 5, replace = FALSE)
22-
i <- sample(16:30, 5, replace = FALSE)
23-
n <- sample(31:45, 5, replace = FALSE)
24-
g <- sample(46:60, 5, replace = FALSE)
25-
o <- sample(61:75, 5, replace = FALSE)
26-
output$bingoGrid <- renderTable({
27-
# render bingo grid table
28-
bingo_table <- matrix(c(b, i, n, g, o), nrow = 5,
29-
ncol = 5, byrow = FALSE) # bingo table
30-
colnames(bingo_table) <- c("B", "I", "N", "G", "O") # column names
31-
bingo_table
20+
called_number <- reactiveValues(value = 0)
21+
number_list <- sample(1:75, 75, replace = FALSE)
22+
23+
observeEvent(input$start, {
24+
called_numbers <- 0
25+
called_number$value <- 0
26+
invalidateLater(1000, session)
27+
# when start button is pressed generate random numbers
28+
b <- sample(1:15, 5, replace = FALSE)
29+
i <- sample(16:30, 5, replace = FALSE)
30+
n <- sample(31:45, 5, replace = FALSE)
31+
g <- sample(46:60, 5, replace = FALSE)
32+
o <- sample(61:75, 5, replace = FALSE)
33+
output$bingoGrid <- renderTable({
34+
# render bingo grid table
35+
bingo_table <- matrix(
36+
c(b, i, n, g, o),
37+
nrow = 5,
38+
ncol = 5,
39+
byrow = FALSE
40+
) # bingo table
41+
colnames(bingo_table) <- c("B", "I", "N", "G", "O") # column names
42+
bingo_table
43+
})
3244
})
33-
})
3445

35-
observe({
36-
invalidateLater(1000, session) # update called number every second
37-
called_numbers <<- called_numbers + 1
38-
isolate({
39-
called_number$value <- number_list[called_numbers]
46+
observe({
47+
invalidateLater(1000, session) # update called number every second
48+
called_numbers <<- called_numbers + 1
49+
isolate({
50+
called_number$value <- number_list[called_numbers]
51+
})
4052
})
41-
})
4253

43-
output$calledNumber <- renderText({
44-
# render called number text
45-
called_number$value
46-
})
54+
output$calledNumber <- renderText({
55+
# render called number text
56+
called_number$value
57+
})
4758
}
4859

49-
shinyApp(ui = ui, server = server) # run the Shiny web app server
60+
shinyApp(ui = ui, server = server) # run the Shiny web app server

0 commit comments

Comments
 (0)