Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

During the lockdown of the city, game nights are happening on Zoom a lot. I was asked if I could code up a bingo game—about an hour and a half before game time. I’m sure there’s probably some type of bingo game programmed in Shiny already, but I wanted to see what I could do under crunch from scratch. Here’s what I’ve got.

Using the rules from Wikipedia, I made two apps: One for the bingo master (who shares their screen) and one for each person playing bingo. All code is below and at my GitHub.

The bingo master app is for drawing a new letter-number combination, keeping a record of what has been called, and then starting a new game when necessary.

The bingo player app is a grid of checkboxes that someone can check whenever that letter-number combination has been called. At a previous job, I remember getting creative with using tagList and lapply statements, which would be handy for generating a grid of checkboxes in less code, but I can’t remember how to do that at the moment, so you’ll see some copy-pasted stanza code in the player script. I know this would offend my statistical programming professor from grad school, but ¯\_(ツ)_/¯

## Bingo Master

```library(shiny)
library(dplyr)

ui <- fluidPage(
sidebarLayout(
sidebarPanel(
actionButton("draw", "Draw"),
br(),
br(),
actionButton("new_game", "New Game"),

),
mainPanel(
uiOutput("display")
)
)
)

server <- function(input, output) {
v <- reactiveValues(
current = NULL,
previous = NULL,
counter = 0,
master = list(
b = paste0("B-", sample(1:15)),
i = paste0("I-", sample(16:30)),
n = paste0("N-", sample(31:45)),
g = paste0("G-", sample(46:60)),
o = paste0("O-", sample(61:75))
) %>%
unlist() %>%
`[`(sample(1:75))
)

observeEvent(input\$new_game, {
v\$current <- NULL
v\$previous <- NULL
v\$counter <- 0
})

observeEvent(input\$draw, {

if (v\$counter > 74) {
NULL
} else {
v\$counter <- v\$counter + 1
v\$current <- v\$master[v\$counter]
v\$previous <- c(v\$current, v\$previous)
}
})

output\$display <- renderUI({
tagList(
p(v\$current, style = "font-size:50px"),
br(),
p(paste(v\$previous, collapse = ", "))
)
})
}

shinyApp(ui = ui, server = server)```

## Bingo Player

```library(shiny)

make_card <- function() {
out <- data.frame(
B = sample(1:15,  5),
I = sample(16:30, 5),
N = sample(31:45, 5),
G = sample(46:60, 5),
O = sample(61:75, 5)
)

out\$N <- NA

return(out)
}

ui <- fluidPage(
sidebarLayout(
sidebarPanel(
actionButton("card", "Make a New Card")
),
mainPanel(
column(2, uiOutput("b")),
column(2, uiOutput("i")),
column(2, uiOutput("n")),
column(2, uiOutput("g")),
column(2, uiOutput("o"))
)
)
)

server <- function(input, output) {
v <- reactiveValues(b = NULL, i = NULL, n = NULL, g = NULL, o = NULL)

observeEvent(input\$card, {
card <- make_card()
v\$b <- card\$B
v\$i <- card\$I
v\$n <- card\$N
v\$g <- card\$G
v\$o <- card\$O
})

output\$b <- renderUI({
tagList(
p("B", style = "font-size:25px"),
checkboxInput("b1", v\$b),
checkboxInput("b2", v\$b),
checkboxInput("b3", v\$b),
checkboxInput("b4", v\$b),
checkboxInput("b5", v\$b)
)
})

output\$i <- renderUI({
tagList(
p("I", style = "font-size:25px"),
checkboxInput("i1", v\$i),
checkboxInput("i2", v\$i),
checkboxInput("i3", v\$i),
checkboxInput("i4", v\$i),
checkboxInput("i5", v\$i)
)
})

output\$n <- renderUI({
tagList(
p("N", style = "font-size:25px"),
checkboxInput("n1", v\$n),
checkboxInput("n2", v\$n),
checkboxInput("n3", "FREE", TRUE),
checkboxInput("n4", v\$n),
checkboxInput("n5", v\$n)
)
})

output\$g <- renderUI({
tagList(
p("G", style = "font-size:25px"),
checkboxInput("g1", v\$g),
checkboxInput("g2", v\$g),
checkboxInput("g3", v\$g),
checkboxInput("g4", v\$g),
checkboxInput("g5", v\$g)
)
})

output\$o <- renderUI({
tagList(
p("O", style = "font-size:25px"),
checkboxInput("o1", v\$o),
checkboxInput("o2", v\$o),
checkboxInput("o3", v\$o),
checkboxInput("o4", v\$o),
checkboxInput("o5", v\$o)
)
})

}

shinyApp(ui = ui, server = server)```