Skip to content

Commit

Permalink
Deploying to gh-pages from @ 5286607 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
IndrajeetPatil committed Jun 9, 2024
1 parent bc1b4ed commit 78ad955
Show file tree
Hide file tree
Showing 52 changed files with 433 additions and 429 deletions.
2 changes: 1 addition & 1 deletion 404.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ <h1>Page not found<a class="anchor" aria-label="anchor" href="#page-not-found"><
<footer class="bg-primary text-light mt-5"><div class="container"><div class="row">

<div class="col-12 col-md-6 mt-3">
<p>"<strong>Advanced R Exercises</strong>" was written by <a class="text-light" href="https://sites.google.com/site/indrajeetspatilmorality/">Indrajeet Patil</a>. It was last built on 2024-06-02.</p>
<p>"<strong>Advanced R Exercises</strong>" was written by <a class="text-light" href="https://sites.google.com/site/indrajeetspatilmorality/">Indrajeet Patil</a>. It was last built on 2024-06-09.</p>
</div>

<div class="col-12 col-md-6 mt-3">
Expand Down
6 changes: 3 additions & 3 deletions Conditions.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ rlang::catch_cnd
#> return(NULL)
#> })))
#> }
#> <bytecode: 0x55d1586005c8>
#> <bytecode: 0x55b59c4e0508>
#> <environment: namespace:rlang>
```

Expand Down Expand Up @@ -718,7 +718,7 @@ sessioninfo::session_info(include_base = TRUE)
#> collate C.UTF-8
#> ctype C.UTF-8
#> tz UTC
#> date 2024-06-02
#> date 2024-06-09
#> pandoc 3.2 @ /opt/hostedtoolcache/pandoc/3.2/x64/ (via rmarkdown)
#>
#> ─ Packages ───────────────────────────────────────────────
Expand Down Expand Up @@ -755,7 +755,7 @@ sessioninfo::session_info(include_base = TRUE)
#> pkgconfig 2.0.3 2019-09-22 [1] RSPM
#> pkgload 1.3.4 2024-01-16 [1] RSPM
#> R6 2.5.1 2021-08-19 [1] RSPM
#> rlang * 1.1.3 2024-01-10 [1] RSPM
#> rlang * 1.1.4 2024-06-04 [1] RSPM
#> rmarkdown 2.27 2024-05-17 [1] RSPM
#> rprojroot 2.0.4 2023-11-05 [1] RSPM
#> sass 0.4.9 2024-03-15 [1] RSPM
Expand Down
4 changes: 2 additions & 2 deletions Control-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ sessioninfo::session_info(include_base = TRUE)
#> collate C.UTF-8
#> ctype C.UTF-8
#> tz UTC
#> date 2024-06-02
#> date 2024-06-09
#> pandoc 3.2 @ /opt/hostedtoolcache/pandoc/3.2/x64/ (via rmarkdown)
#>
#> ─ Packages ───────────────────────────────────────────────
Expand Down Expand Up @@ -304,7 +304,7 @@ sessioninfo::session_info(include_base = TRUE)
#> memoise 2.0.1 2021-11-26 [1] RSPM
#> methods * 4.4.0 2024-05-25 [3] local
#> R6 2.5.1 2021-08-19 [1] RSPM
#> rlang 1.1.3 2024-01-10 [1] RSPM
#> rlang 1.1.4 2024-06-04 [1] RSPM
#> rmarkdown 2.27 2024-05-17 [1] RSPM
#> sass 0.4.9 2024-03-15 [1] RSPM
#> sessioninfo 1.2.2 2021-12-06 [1] RSPM
Expand Down
18 changes: 9 additions & 9 deletions Environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ library(rlang)
e <- env()
e$loop <- e
env_print(e)
#> <environment: 0x556391479db8>
#> <environment: 0x55bdbdb35fa0>
#> Parent: <environment: global>
#> Bindings:
#> • loop: <env>
Expand All @@ -51,8 +51,8 @@ The binding `loop` should have the same memory address as the environment `e`:

``` r
lobstr::ref(e$loop)
#> █ [1:0x556391479db8] <env>
#> └─loop = [1:0x556391479db8]
#> █ [1:0x55bdbdb35fa0] <env>
#> └─loop = [1:0x55bdbdb35fa0]
```

**Q3.** Create a pair of environments as illustrated by this picture.
Expand All @@ -71,12 +71,12 @@ e2$deloop <- e1

# following should be the same
lobstr::obj_addrs(list(e1, e2$deloop))
#> [1] "0x5563923ee880" "0x5563923ee880"
#> [1] "0x55bdbdfcca78" "0x55bdbdfcca78"
```

``` r
lobstr::obj_addrs(list(e2, e1$loop))
#> [1] "0x55639235f570" "0x55639235f570"
#> [1] "0x55bdbdec3600" "0x55bdbdec3600"
```

**Q4.** Explain why `e[[1]]` and `e[c("a", "b")]` don't make sense when `e` is an environment.
Expand Down Expand Up @@ -339,7 +339,7 @@ fget("mean", inherits = FALSE)
fget("mean", inherits = TRUE)
#> function (x, ...)
#> UseMethod("mean")
#> <bytecode: 0x55638e701850>
#> <bytecode: 0x55bdbbcf5aa0>
#> <environment: namespace:base>
```

Expand Down Expand Up @@ -569,7 +569,7 @@ rlang::caller_env
#> {
#> parent.frame(n + 1)
#> }
#> <bytecode: 0x5563901338a8>
#> <bytecode: 0x55bdbd738658>
#> <environment: namespace:rlang>
```

Expand Down Expand Up @@ -625,7 +625,7 @@ sessioninfo::session_info(include_base = TRUE)
#> collate C.UTF-8
#> ctype C.UTF-8
#> tz UTC
#> date 2024-06-02
#> date 2024-06-09
#> pandoc 3.2 @ /opt/hostedtoolcache/pandoc/3.2/x64/ (via rmarkdown)
#>
#> ─ Packages ───────────────────────────────────────────────
Expand Down Expand Up @@ -663,7 +663,7 @@ sessioninfo::session_info(include_base = TRUE)
#> pkgconfig 2.0.3 2019-09-22 [1] RSPM
#> png 0.1-8 2022-11-29 [1] RSPM
#> R6 2.5.1 2021-08-19 [1] RSPM
#> rlang * 1.1.3 2024-01-10 [1] RSPM
#> rlang * 1.1.4 2024-06-04 [1] RSPM
#> rmarkdown 2.27 2024-05-17 [1] RSPM
#> sass 0.4.9 2024-03-15 [1] RSPM
#> sessioninfo 1.2.2 2021-12-06 [1] RSPM
Expand Down
14 changes: 7 additions & 7 deletions Evaluation.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ withr::with_tempdir(
foo()
}
)
#> <environment: 0x5636b795f8f8>
#> <environment: 0x55b79b5b44f0>
#> Parent: <environment: global>
```

Expand Down Expand Up @@ -268,23 +268,23 @@ q1 <- new_quosure(expr(x), env(x = 1))
q1
#> <quosure>
#> expr: ^x
#> env: 0x5636b83c6d70
#> env: 0x55b79c073568
```

``` r
q2 <- new_quosure(expr(x + !!q1), env(x = 10))
q2
#> <quosure>
#> expr: ^x + (^x)
#> env: 0x5636b8ac4748
#> env: 0x55b79cbb62f0
```

``` r
q3 <- new_quosure(expr(x + !!q2), env(x = 100))
q3
#> <quosure>
#> expr: ^x + (^x + (^x))
#> env: 0x5636b97cf2f0
#> env: 0x55b79d4868b0
```

**A1.** Correctly predicted 😉
Expand Down Expand Up @@ -331,7 +331,7 @@ enenv(x)

foo <- function(x) enenv(x)
foo()
#> <environment: 0x5636ba31a1a0>
#> <environment: 0x55b79dfcfda0>
```

---
Expand Down Expand Up @@ -722,7 +722,7 @@ sessioninfo::session_info(include_base = TRUE)
#> collate C.UTF-8
#> ctype C.UTF-8
#> tz UTC
#> date 2024-06-02
#> date 2024-06-09
#> pandoc 3.2 @ /opt/hostedtoolcache/pandoc/3.2/x64/ (via rmarkdown)
#>
#> ─ Packages ───────────────────────────────────────────────
Expand Down Expand Up @@ -754,7 +754,7 @@ sessioninfo::session_info(include_base = TRUE)
#> pillar 1.9.0 2023-03-22 [1] RSPM
#> purrr 1.0.2 2023-08-10 [1] RSPM
#> R6 2.5.1 2021-08-19 [1] RSPM
#> rlang * 1.1.3 2024-01-10 [1] RSPM
#> rlang * 1.1.4 2024-06-04 [1] RSPM
#> rmarkdown 2.27 2024-05-17 [1] RSPM
#> sass 0.4.9 2024-03-15 [1] RSPM
#> sessioninfo 1.2.2 2021-12-06 [1] RSPM
Expand Down
6 changes: 3 additions & 3 deletions Expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ call_standardise(quote(mean(x = 1:10, , TRUE)))
mean
#> function (x, ...)
#> UseMethod("mean")
#> <bytecode: 0x55893f33d850>
#> <bytecode: 0x55872d21faa0>
#> <environment: namespace:base>
```

Expand Down Expand Up @@ -1137,7 +1137,7 @@ sessioninfo::session_info(include_base = TRUE)
#> collate C.UTF-8
#> ctype C.UTF-8
#> tz UTC
#> date 2024-06-02
#> date 2024-06-09
#> pandoc 3.2 @ /opt/hostedtoolcache/pandoc/3.2/x64/ (via rmarkdown)
#>
#> ─ Packages ───────────────────────────────────────────────
Expand Down Expand Up @@ -1171,7 +1171,7 @@ sessioninfo::session_info(include_base = TRUE)
#> pillar 1.9.0 2023-03-22 [1] RSPM
#> purrr 1.0.2 2023-08-10 [1] RSPM
#> R6 2.5.1 2021-08-19 [1] RSPM
#> rlang * 1.1.3 2024-01-10 [1] RSPM
#> rlang * 1.1.4 2024-06-04 [1] RSPM
#> rmarkdown 2.27 2024-05-17 [1] RSPM
#> sass 0.4.9 2024-03-15 [1] RSPM
#> sessioninfo 1.2.2 2021-12-06 [1] RSPM
Expand Down
52 changes: 26 additions & 26 deletions Function-factories.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ library(ggplot2, warn.conflicts = FALSE)
force
#> function (x)
#> x
#> <bytecode: 0x563010149e00>
#> <bytecode: 0x557738b0ce00>
#> <environment: namespace:base>
```

Expand All @@ -47,8 +47,8 @@ f <- approxfun(x, y)
f
#> function (v)
#> .approxfun(x, y, v, method, yleft, yright, f, na.rm)
#> <bytecode: 0x563015cbafd0>
#> <environment: 0x563015cba6a0>
#> <bytecode: 0x55773e6899d8>
#> <environment: 0x55773e6890a8>
```

``` r
Expand Down Expand Up @@ -233,7 +233,7 @@ new_counter3()
#> i <- i + 1
#> i
#> }
#> <environment: 0x563015c96ea0>
#> <environment: 0x55773e664420>
```

``` r
Expand All @@ -243,8 +243,8 @@ new_counter3()
#> i <- i + 1
#> i
#> }
#> <bytecode: 0x563015f350b0>
#> <environment: 0x563015d24880>
#> <bytecode: 0x55773e904ee0>
#> <environment: 0x55773e6f3d20>
```

---
Expand Down Expand Up @@ -282,7 +282,7 @@ ggplot2::label_bquote
#> }
#> structure(fun, class = "labeller")
#> }
#> <bytecode: 0x563016332ac0>
#> <bytecode: 0x55773ecfd6d0>
#> <environment: namespace:ggplot2>
```

Expand All @@ -305,7 +305,7 @@ scales::number_format
#> scale_cut = scale_cut, trim = trim, ...)
#> }
#> }
#> <bytecode: 0x563016532ce0>
#> <bytecode: 0x55773eefa278>
#> <environment: namespace:scales>
```

Expand Down Expand Up @@ -401,7 +401,7 @@ Let's have a look at one example with each:
``` r
boxcox2(1)
#> function(x) (x^lambda - 1) / lambda
#> <environment: 0x563015237a20>
#> <environment: 0x55773dbcfc80>
```

``` r
Expand All @@ -414,7 +414,7 @@ boxcox3(mtcars$wt)
#> (x^lambda - 1) / lambda
#> }
#> }
#> <environment: 0x5630153733a8>
#> <environment: 0x55773dd24018>
```

As can be seen:
Expand Down Expand Up @@ -447,7 +447,7 @@ boot_permute(mtcars, "mpg")
#> col <- df[[var]]
#> col[sample(n, replace = TRUE)]
#> }
#> <environment: 0x563015d66720>
#> <environment: 0x55773e707ea0>
```

This is why we don't need to worry about a copy being made because the `df` in the function environment points to the memory address of the data frame. We can confirm this by comparing their memory addresses:
Expand All @@ -456,7 +456,7 @@ This is why we don't need to worry about a copy being made because the `df` in t
``` r
boot_permute_env <- rlang::fn_env(boot_permute(mtcars, "mpg"))
rlang::env_print(boot_permute_env)
#> <environment: 0x5630162cba80>
#> <environment: 0x55773ec785f8>
#> Parent: <environment: global>
#> Bindings:
#> • n: <int>
Expand Down Expand Up @@ -521,8 +521,8 @@ bench::mark(
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 LL1 28.6µs 30µs 32455. 12.8KB 42.2
#> 2 LL2 15.3µs 16.1µs 59356. 0B 41.6
#> 1 LL1 28.6µs 30.7µs 31678. 12.8KB 44.4
#> 2 LL2 15.6µs 16.6µs 56938. 0B 39.9
```

As can be seen, the second version is much faster than the first version.
Expand All @@ -548,16 +548,16 @@ generate_ll_benches <- function(n) {
#> # A tibble: 10 × 5
#> length expression min median `itr/sec`
#> <dbl> <bch:expr> <bch:tm> <bch:tm> <dbl>
#> 1 10 LL1 40.6µs 42.4µs 22371.
#> 2 10 LL2 18.3µs 19.1µs 51239.
#> 3 20 LL1 43µs 44.3µs 22273.
#> 4 20 LL2 17.8µs 18.5µs 53389.
#> 5 50 LL1 47.4µs 49µs 20016.
#> 6 50 LL2 17.1µs 17.8µs 55515.
#> 7 100 LL1 62.9µs 65.1µs 14869.
#> 8 100 LL2 18.3µs 19.1µs 51653.
#> 9 1000 LL1 856.4µs 892.6µs 1088.
#> 10 1000 LL2 56.1µs 57.7µs 17003.
#> 1 10 LL1 40.2µs 42.2µs 22075.
#> 2 10 LL2 18.4µs 19.2µs 51175.
#> 3 20 LL1 43.5µs 45.1µs 21848.
#> 4 20 LL2 17.9µs 18.7µs 52651.
#> 5 50 LL1 47µs 48.4µs 20232.
#> 6 50 LL2 17µs 17.7µs 55790.
#> 7 100 LL1 62.9µs 64.5µs 15080.
#> 8 100 LL2 18.2µs 19µs 52008.
#> 9 1000 LL1 856.3µs 918.5µs 1086.
#> 10 1000 LL2 56.6µs 57.9µs 16971.
```

``` r
Expand Down Expand Up @@ -714,7 +714,7 @@ sessioninfo::session_info(include_base = TRUE)
#> collate C.UTF-8
#> ctype C.UTF-8
#> tz UTC
#> date 2024-06-02
#> date 2024-06-09
#> pandoc 3.2 @ /opt/hostedtoolcache/pandoc/3.2/x64/ (via rmarkdown)
#>
#> ─ Packages ───────────────────────────────────────────────
Expand Down Expand Up @@ -760,7 +760,7 @@ sessioninfo::session_info(include_base = TRUE)
#> profmem 0.6.0 2020-12-13 [1] RSPM
#> purrr 1.0.2 2023-08-10 [1] RSPM
#> R6 2.5.1 2021-08-19 [1] RSPM
#> rlang * 1.1.3 2024-01-10 [1] RSPM
#> rlang * 1.1.4 2024-06-04 [1] RSPM
#> rmarkdown 2.27 2024-05-17 [1] RSPM
#> sass 0.4.9 2024-03-15 [1] RSPM
#> scales * 1.3.0 2023-11-28 [1] RSPM
Expand Down
Binary file modified Function-factories_files/figure-html/Function-factories-28-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 78ad955

Please sign in to comment.