Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iteration3 #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

iteration3 #3

wants to merge 2 commits into from

Conversation

moodymudskipper
Copy link
Collaborator

@moodymudskipper moodymudskipper commented Apr 5, 2024

I prefer #2 that generalises this, but this is the simpler version you asked for @krlmlr . The output is the same. I didn't publish my iteration 2 using quosures because it's more complex and harder to generalise, I can if you want though.

semi-manual reprex:

library(dplyr, w = F)

mutate
#> function(.data, ...) {
#>   ..new_caller_env <- env_clone_lazy(parent.frame())
#>   ..call = sys.call()
#>   archive(
#>     call = ..call,
#>     env = ..new_caller_env
#>   )
#>   on.exit({
#>     env_cleanup(..new_caller_env)
#>   })
#>   ..call[[1]] <- function(.data, ...) {
#>     UseMethod("mutate")
#>   }
#>   eval(..call, ..new_caller_env)
#> }
#> <bytecode: 0x12c036f98>
#> <environment: namespace:dplyr>

starwars %>%
  select(name, mass) %>%
  mutate(
    mass2 = mass * 2,
    mass2_squared = mass2 * mass2
  )
#> # A tibble: 87 × 4
#>    name                mass mass2 mass2_squared
#>    <chr>              <dbl> <dbl>         <dbl>
#>  1 Luke Skywalker        77   154         23716
#>  2 C-3PO                 75   150         22500
#>  3 R2-D2                 32    64          4096
#>  4 Darth Vader          136   272         73984
#>  5 Leia Organa           49    98          9604
#>  6 Owen Lars            120   240         57600
#>  7 Beru Whitesun lars    75   150         22500
#>  8 R5-D4                 32    64          4096
#>  9 Biggs Darklighter     84   168         28224
#> 10 Obi-Wan Kenobi        77   154         23716
#> # ℹ 77 more rows

mtcars %>%
  group_by(cyl) %>%
  summarise(disp = mean(disp), sd = sd(disp))
#> # A tibble: 3 × 3
#>     cyl  disp    sd
#>   <dbl> <dbl> <dbl>
#> 1     4  105.    NA
#> 2     6  183.    NA
#> 3     8  353.    NA

qs::qsave(dplyr:::globals$archive, "archive.qs")
#> Warning in qs::qsave(dplyr:::globals$archive, "archive.qs"): 'package:dplyr'
#> may not be available when loading

#> Warning in qs::qsave(dplyr:::globals$archive, "archive.qs"): 'package:dplyr'
#> may not be available when loading

.rs.restartR()
archive <- qs::qread("archive.qs")

eval(archive$call[[1]], archive$env[[1]])
#> # A tibble: 87 × 4
#>    name                mass mass2 mass2_squared
#>    <chr>              <dbl> <dbl>         <dbl>
#>  1 Luke Skywalker        77   154         23716
#>  2 C-3PO                 75   150         22500
#>  3 R2-D2                 32    64          4096
#>  4 Darth Vader          136   272         73984
#>  5 Leia Organa           49    98          9604
#>  6 Owen Lars            120   240         57600
#>  7 Beru Whitesun lars    75   150         22500
#>  8 R5-D4                 32    64          4096
#>  9 Biggs Darklighter     84   168         28224
#> 10 Obi-Wan Kenobi        77   154         23716
#> # ℹ 77 more rows

eval(archive$call[[2]], archive$env[[2]])
#> # A tibble: 3 × 3
#>     cyl  disp    sd
#>   <dbl> <dbl> <dbl>
#> 1     4  105.    NA
#>.2     6  183.    NA
#> 3     8  353.    NA

@krlmlr
Copy link

krlmlr commented Apr 10, 2024

Brilliant!

CC @hannes.

options(conflicts.policy = list(warn = FALSE))
library(dplyr)

mutate
#> function(.data, ...) {
#>   ..new_caller_env <- env_clone_lazy(parent.frame())
#>   ..call = sys.call()
#>   archive(
#>     call = ..call,
#>     env = ..new_caller_env
#>   )
#>   on.exit({
#>     env_cleanup(..new_caller_env)
#>   })
#>   ..call[[1]] <- function(.data, ...) {
#>     UseMethod("mutate")
#>   }
#>   eval(..call, ..new_caller_env)
#> }
#> <bytecode: 0x10879f6f8>
#> <environment: namespace:dplyr>

factor <- 2

starwars %>%
  select(name, mass) %>%
  mutate(
    mass2 = mass * factor,
    mass2_squared = mass2 * mass2
  )
#> # A tibble: 87 × 4
#>    name                mass mass2 mass2_squared
#>    <chr>              <dbl> <dbl>         <dbl>
#>  1 Luke Skywalker        77   154         23716
#>  2 C-3PO                 75   150         22500
#>  3 R2-D2                 32    64          4096
#>  4 Darth Vader          136   272         73984
#>  5 Leia Organa           49    98          9604
#>  6 Owen Lars            120   240         57600
#>  7 Beru Whitesun lars    75   150         22500
#>  8 R5-D4                 32    64          4096
#>  9 Biggs Darklighter     84   168         28224
#> 10 Obi-Wan Kenobi        77   154         23716
#> # ℹ 77 more rows

fun <- function() {
  three <- 3

  mtcars %>%
    group_by(cyl) %>%
    summarise(disp = mean(disp), sd = sd(disp), high = three + factor)
}

fun()
#> # A tibble: 3 × 4
#>     cyl  disp    sd  high
#>   <dbl> <dbl> <dbl> <dbl>
#> 1     4  105.    NA     5
#> 2     6  183.    NA     5
#> 3     8  353.    NA     5

suppressWarnings(qs::qsave(dplyr:::globals$archive, "archive.qs"))

callr::r(function() {
  archive <- qs::qread("archive.qs")

  list(
    eval(archive$call[[1]], archive$env[[1]]),
    eval(archive$call[[2]], archive$env[[2]])
  )
})
#> [[1]]
#> # A tibble: 87 × 4
#>    name                mass mass2 mass2_squared
#>    <chr>              <dbl> <dbl>         <dbl>
#>  1 Luke Skywalker        77   154         23716
#>  2 C-3PO                 75   150         22500
#>  3 R2-D2                 32    64          4096
#>  4 Darth Vader          136   272         73984
#>  5 Leia Organa           49    98          9604
#>  6 Owen Lars            120   240         57600
#>  7 Beru Whitesun lars    75   150         22500
#>  8 R5-D4                 32    64          4096
#>  9 Biggs Darklighter     84   168         28224
#> 10 Obi-Wan Kenobi        77   154         23716
#> # ℹ 77 more rows
#> 
#> [[2]]
#> # A tibble: 3 × 4
#>     cyl  disp    sd  high
#>   <dbl> <dbl> <dbl> <dbl>
#> 1     4  105.    NA     5
#> 2     6  183.    NA     5
#> 3     8  353.    NA     5

Created on 2024-04-10 with reprex v2.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants