rstudio-education / hopr

This is the development site for Hands-On Programming with R, a book that teaches how to program in R, with hands-on examples. Read the book at https://rstudio-education.github.io/hopr.
Other
254 stars 331 forks source link

8.6 does not work for me #77

Open brobr opened 7 months ago

brobr commented 7 months ago

Hi, the approach of:

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)

gives at my end an ogoing deal() output of 1 king spades 13. I do not understand why this happens (on R4.3.3 on Slackware64 linux with RStudio 2023.12.1 Build 402).

My unsuccessful run:

> decki <- deck
> decki$value
 [1] 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11 10  9  8  7  6
[22]  5  4  3  2  1 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11
[43] 10  9  8  7  6  5  4  3  2  1
> setup <- function(decki) {
+   DECK <- decki
+ 
+   DEAL <- function() {
+     card <- decki[1, ]
+     assign("decki", decki[-1, ], envir = globalenv())
+     card
+   }
+ 
+   SHUFFLE <- function(){
+     random <- sample(1:52, size = 52)
+     assign("decki", DECK[random, ], envir = globalenv())
+   }
+ 
+   list(deal = DEAL, shuffle = SHUFFLE)
+ }
> decki$value
 [1] 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11 10  9  8  7  6
[22]  5  4  3  2  1 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11
[43] 10  9  8  7  6  5  4  3  2  1
> cards <- setup(decki)
> deal <- cards$deal
> shuffle <- cards$shuffle
> decki$value
 [1] 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11 10  9  8  7  6
[22]  5  4  3  2  1 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11
[43] 10  9  8  7  6  5  4  3  2  1
> shuffle()
> decki$value
 [1]  8  9 11  6  4  5 13  4  3  6  1  5  7 13  2 10  6  7  7  6  1
[22]  8  3 10 11 13 12  7  4  9 11  9 12 10 11  2  3 12  1  9 13  5
[43]  8  2  4  5  2  1 12  8  3 10
> deal()
  face   suit value
1 king spades    13
> deal()
  face   suit value
1 king spades    13
> deal()
  face   suit value
1 king spades    13
> shuffle()
> decki$value
 [1]  8  8  1  4  7  9  6  2 11  6 13  7 10  9 11  1  7  6  2 12  5
[22] 13 12 11 10  6 13  2  4  8  9 13  2  4  3 12  3  1  5 12  3  9
[43] 10  4  1  8  5 10  5 11  7  3
> deal()
  face   suit value
1 king spades    13
> deal()
  face   suit value
1 king spades    13
> deal()
  face   suit value
1 king spades    13
> deal()
  face   suit value
1 king spades    13
> globalenv()
<environment: R_GlobalEnv>
> environment()
<environment: R_GlobalEnv>
> environment(setup)
<environment: R_GlobalEnv>
> environment(deal)
<environment: 0x5620e4976028>
> environment(shuffle)
<environment: 0x5620e4976028>
> environment(deck)

I only got this to work with changing all calls to refer to the setup-environment; not the global one. With an extra function STOCK that returns the current state of the used deck one can see what happens after SHUFFLE and DEAL...

#> deckj <- deck
> setup2 <- function(deckj){
+   DECK <- deckj
+ 
+   DEAL <- function(){
+     card <- DECK[1, ]
+     assign("DECK", DECK[-1, ], envir = parent.env(environment()))
+     card
+   }
+ 
+   SHUFFLE <- function(){
+     random <- sample(1:52, size = 52)
+     assign("DECK", deckj[random, ], envir = parent.env(environment()))
+   }
+ 
+   STOCK <- function(){
+     DECK
+   }
+ 
+   list(deal2 = DEAL, shuffle2 = SHUFFLE, stock2 = STOCK)
+ }
> cards2 <- setup2(deck)
> cards2
$deal2
function(){
    card <- DECK[1, ]
    assign("DECK", DECK[-1, ], envir = parent.env(environment()))
    card
  }
<bytecode: 0x5620e5b7b8b8>
<environment: 0x5620e719c620>

$shuffle2
function(){
    random <- sample(1:52, size = 52)
    assign("DECK", deckj[random, ], envir = parent.env(environment()))
  }
<bytecode: 0x5620e4b486f8>
<environment: 0x5620e719c620>

$stock2
function(){
    DECK
  }
<bytecode: 0x5620e4a23750>
<environment: 0x5620e719c620>

> deal2 <- cards2$deal2
> shuffle2 <- cards2$shuffle2
> stock2 <- cards2$stock2
> stock2()$value
 [1] 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11 10  9  8  7  6
[22]  5  4  3  2  1 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11
[43] 10  9  8  7  6  5  4  3  2  1
> shuffle2()
> stock2()$value
 [1]  8  1  6  5  1  3  6  3  9 10 11  7  5 13 12  9  4  5 12 11  6
[22] 12  7 12  5  8  8  7 10 10  4  8  2 13  3 11 11  1 13  6  3  9
[43]  7  9  2 13  1 10  4  2  4  2
> deal2()
    face  suit value
19 eight clubs     8
> deal2()
   face  suit value
26  ace clubs     1
> deal2()
  face   suit value
8  six spades     6
> deal2()
   face     suit value
35 five diamonds     5
> stock2()$value
 [1]  1  3  6  3  9 10 11  7  5 13 12  9  4  5 12 11  6 12  7 12  5
[22]  8  8  7 10 10  4  8  2 13  3 11 11  1 13  6  3  9  7  9  2 13
[43]  1 10  4  2  4  2
> deal()
Error in deal() : could not find function "deal"
> deal2()
   face     suit value
39  ace diamonds     1

Trying the original setup again:

> decki$value
 [1] 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11 10  9  8  7  6  5
[22]  4  3  2  1 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11 10
[43]  9  8  7  6  5  4  3  2  1
> rm(decki)
> rm(deal)
> rm(shuffle)
> decki <- deck
> setup3 <- function(decki) {
+   DECK <- decki
+ 
+   DEAL <- function() {
+     card <- decki[1, ]
+     assign("decki", decki[-1, ], envir = globalenv())
+     card
+   }
+ 
+   SHUFFLE <- function(){
+     random <- sample(1:52, size = 52)
+     assign("decki", DECK[random, ], envir = globalenv())
+   }
+ 
+   list(deal3 = DEAL, shuffle3 = SHUFFLE)
+ }
> decki$value
 [1] 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11 10  9  8  7  6
[22]  5  4  3  2  1 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11
[43] 10  9  8  7  6  5  4  3  2  1
> cards3 <- setup3(decki)
> deal3 <- cards3$deal3
> shuffle3 <- cards3$shuffle3
> shuffle3()
> decki$value
 [1] 11  6  9  2  9  1  2 12  5 12  4  3  3  6  3  7  4  9  8 10  1
[22]  7  4  1  6  9  4  5 10 11 11 13  2 10  6 12 13  8  8  7  8  7
[43]  5 13  3 13 12 11 10  1  5  2
> deal3()
  face   suit value
1 king spades    13
> deal3()
  face   suit value
1 king spades    13
> deal3()
  face   suit value
1 king spades    13
> decki$value
 [1] 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11 10  9  8  7  6  5
[22]  4  3  2  1 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11 10
[43]  9  8  7  6  5  4  3  2  1
> 

The original decki is returned after deal(), not the shuffled one...