|
1 | 1 | # import Shiny library |
2 | 2 | library(shiny) |
3 | 3 | # 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 | +) |
9 | 16 |
|
10 | 17 | 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 |
17 | 19 | 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 | + }) |
32 | 44 | }) |
33 | | - }) |
34 | 45 |
|
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 | + }) |
40 | 52 | }) |
41 | | - }) |
42 | 53 |
|
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 | + }) |
47 | 58 | } |
48 | 59 |
|
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