I am unable to replicate an example from the book "Hands-on Programming with R". I have literally cut / copied the code from the book, but it simply won't work.
setup <- function(deck) {
DECK <- deck
DEAL <- function() {
card <- deck[1, ]
assign("deck", deck[-1, ], envir = globalenv())
card
}
SHUFFLE <- function(){
random <- sample(1:52, size = 52)
assign("deck", DECK[random, ], envir = globalenv())
}
list(deal = DEAL, shuffle = SHUFFLE)
}
cards <- setup(deck)
deal <- cards$deal
shuffle <- cards$shuffle
The objective is to use the returned list to make two functions: one that shuffles a card deck, and another to deal a card and then remove the card once dealt.
When I cut / copy the individual functions they work:
DEAL <- function() {
card <- deck[1, ]
assign("deck", deck[-1, ], envir = globalenv())
card
}
SHUFFLE <- function(){
random <- sample(1:52, size = 52)
assign("deck", DECK[random, ], envir = globalenv())
}
But when I attempt to assign the functions using the returned list by the setup() the code no longer works. Sigh.
Any help would be appreciated.