[This article was first published on R-posts.com, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)


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

After some years as a Stata user, I found myself in a new position where the tools available were SQL and SPSS. I was impressed by the power of SQL, but I was unhappy with going back to SPSS after five years with Stata.

Luckily, I got the go-ahead from my leaders at the department to start testing out R as a tool to supplement SQL in data handling.

This was in the beginning of 2020, and by March we were having a social gathering at our workplace. A Bingo night! Which turned out to be the last social event before the pandemic lockdown.

What better opportunity to learn a new programming language than to program some bingo cards! I learnt a lot from this little project.

It uses the packages grid and gridExtra to prepare and embellish the cards.

The function BingoCard draws the cards and is called from the function Bingo3. When Bingo3 is called it runs BingoCard the number of times necessary to create the requested number of sheets and stores the result as a pdf inside a folder defined at the beginning of the script.

All steps could have been added together in a single function. For instance, a more complete function could have included input for the color scheme of the cards, the number of cards on each sheet and more advanced features for where to store the results.
Still, this worked quite well, and was an excellent way of learning since it was both so much fun and gave me the opportunity to talk enthusiastically about R during Bingo Night.

library(gridExtra)
library(grid)



##################################################################
# Be sure to have a folder where results are stored
##################################################################

CardFolder <- "BingoCards"

if (!dir.exists(CardFolder)) {dir.create(CardFolder)}


##################################################################
# Create a theme to use for the cards
##################################################################

thema <- ttheme_minimal(
          base_size = 24, padding = unit(c(6, 6), "mm"),
          core=list(bg_params = list(fill = rainbow(5),
                                     alpha = 0.5,
                                     col="black"),
          fg_params=list(fontface="plain",col="darkblue")),
          colhead=list(fg_params=list(col="darkblue")),
          rowhead=list(fg_params=list(col="white")))


##################################################################
##  Define the function BingoCard
##################################################################

BingoCard <- function() {

  B <- sample(1:15,  5, replace=FALSE)
  I <- sample(16:30, 5, replace=FALSE)
  N <- sample(31:45, 5, replace=FALSE)
  G <- sample(46:60, 5, replace=FALSE)
  O <- sample(61:75, 5, replace=FALSE)

  BingoCard <- as.data.frame(cbind(B,I,N,G,O))

  BingoCard[3,"N"]<-"X"

  a <-  tableGrob(BingoCard, theme = thema)
  return(a)
}

##################################################################
##  Define the function Bingo3
##  The function has two arguments
##  By default, 1 sheet with 3 cards is stored in the CardFolder
##  The default name is "bingocards.pdf"
##  This function calls the BingoCard function
##################################################################

Bingo3 <- function(NumberOfSheets=1, SaveFileName="bingocards") {

  myplots <- list()
  N <- NumberOfSheets*3
  for (i in 1 : N   )      {
    a1 <- BingoCard()
    myplots[[i]] <- a1
  }
  ml <- marrangeGrob(myplots, nrow=3, ncol=1,top="")

  save_here <- paste0(CardFolder,"/",SaveFileName,".pdf")

  ggplot2::ggsave(save_here,  ml, device = "pdf", width = 210,
                  height = 297, units = "mm")
}

##################################################################
##  Run Bingo3 with default values
##################################################################

Bingo3()

##################################################################
##  Run Bingo3 with custom values
##################################################################

Bingo3(NumberOfSheets = 30, SaveFileName = "30_BingoCards")


The Fun in Functional Programming was first posted on March 15, 2024 at 3:26 pm.

To leave a comment for the author, please follow the link and comment on their blog: R-posts.com.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you’re looking to post or find an R/data-science job.


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

Continue reading: The Fun in Functional Programming

Adaptability and Proactiveness in the Dynamic Realm of Programming Languages

In a digital age, we consistently see the rise and fall of programming languages and tools, compelling many developers to continually expand their skill sets. Often, they get introduced to new tools due to the requirements of their roles or out of curiosity on their part. With constant movement and growth in technical roles, developers are encouraged to stay dynamically inclined towards learning new languages.

In the shared user perspective, the shift was made from Stata to the use of SQL and SPSS. They found SQL’s power quite impressive but were trudging towards SPSS’s adoption due to past experiences. Fortunately, the opportunity arose to explore R, an option that appeared quite appealing.

Capitalizing on R: A Highly Effective Programming Tool

R programming language became an essential tool in the user’s quest for better data handling in conjunction with SQL. With its introduction around 2020, it paved the way to an enjoyable journey of learning, starting with the programming of bingo cards.

Implementing R in Project Scenarios

Technically, the R-language exercised in the bingo project involved packages like ‘grid’ and ‘gridExtra’ for card preparation and enhancement. Functions like “BingoCard”, which was used to draw the cards, and “Bingo3”, which called on “BingoCard” to create the required number of bingo sheets. The product of these functions was stored as a PDF in a predefined folder in the script.

Interestingly, all steps could be incorporated in one single function, enabling further customization, such as altering the cards’ color scheme, controlling the number of cards per sheet, and advanced features for storing the results. The two functions described were enough for the task at hand and served as an excellent starting point for the user to explore R.

Implications and Future Developments

This account of a journey into R programming signifies the flexibility and adaptability programmers require in a constantly developing tech ecosystem. It also highlights how learning new tools and languages can be made interesting and engaging through hands-on projects.

  • On-Demand Learning: As the tech world evolves, the demand for learning new languages will increase. This makes it necessary for professionals to be proactive in picking up new skills.
  • Complex Problem Solving: Even though R was initially used for a straightforward application like creating Bingo cards, it can be repurposed for more complex applications like data modeling, statistical computing, and graphic representation of data.
  • Future Developments: With the R programming language’s capabilities, future developments could include more comprehensive functions and a broader array of applications beyond data handling and card creation.

This suggests that while developers should be ready to take on new tools and languages as they evolve, they might find themselves adopting languages like R in the long run due to its flexibility and versatility. They should consider investing time and resources in learning R to tackle complex data-related tasks in their professional roles.

Actionable Advice

Professionals in technical roles, particularly those dealing with data, should be open to exploring new tools such as R, while developers should consider standalone projects or activities to understand a new language better. Future tasks could include more complex problems to maximize the language’s capabilities and grow as a well-skilled and adaptable developer in this dynamic digital ecosystem.

Read the original article