library(instrumentr)
> ── Setup ───────────────────────────────────────────────────────────────────────
> • R Version                    4.0.3 (2020-10-10)
> • instrumentr Version          0.1
> ────────────────────────────────────────────────────────────────────────────────
> • Working Directory            /Users/runner/work/instrumentr/instrumentr/vignettes
> ────────────────────────────────────────────────────────────────────────────────
> • Environment Variables
>  -  USER                       runner
>  -  HOME                       /Users/runner
>  -  R_HOME                     /Library/Frameworks/R.framework/Resources
>  -  R_INCLUDE_DIR              /Library/Frameworks/R.framework/Resources/include
>  -  R_LIBS_USER                /Users/runner/work/_temp/Library
>  -  R_LIBS_SITE                
>  -  R_SESSION_TMPDIR           /var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T//RtmpaMaggh
>  -  R_KEEP_PKG_SOURCE          
>  -  R_KEEP_PKG_PARSE_DATA      
>  -  R_ENABLE_JIT               
>  -  R_DISABLE_BYTECODE         
>  -  OMP_NUM_THREADS            
> ────────────────────────────────────────────────────────────────────────────────

package_load_callback <- function(tracer, callback, application, package) {
    cat("Loaded", get_name(package), "\n")
}

package_unload_callback <- function(tracer, callback, application, package) {
    cat("Unloaded", get_name(package), "\n")
}

package_attach_callback <- function(tracer, callback, application, package) {
    cat("Attached", get_name(package), "\n")
}

package_detach_callback <- function(tracer, callback, application, package) {
    cat("Detached", get_name(package), "\n")
}

tracing_initialization_callback <- function(tracer, callback, application) {
    print("Trace Entry")
}

tracing_finalization_callback <- function(tracer, callback, application) {
    cat("Trace Exit")
    print(search())
    print(names(get_packages(application)))
}

tracer <- create_tracer(tracing_initialization_callback = tracing_initialization_callback,
                        tracing_finalization_callback = tracing_finalization_callback,
                        package_load_callback = package_load_callback,
                        package_unload_callback = package_unload_callback,
                        package_attach_callback = package_attach_callback,
                        package_detach_callback = package_detach_callback)
result <- trace_code(tracer, {
    suppressPackageStartupMessages(library(dplyr))

    starwars %>%
      filter(species == "Droid")

    starwars %>%
      select(name, ends_with("color"))

    starwars %>%
      mutate(name, bmi = mass / ((height / 100)  ^ 2)) %>%
      select(name:mass, bmi)

    starwars %>%
      arrange(desc(mass))

    starwars %>%
      group_by(species) %>%
      summarise(
          n = n(),
          mass = mean(mass, na.rm = TRUE)
      ) %>%
      filter(n > 1)

      unloadNamespace("dplyr")
})
> Added 1113 functions from base
> Added 532 functions from methods
> Added 0 functions from datasets
> Added 468 functions from utils
> Added 179 functions from grDevices
> Added 125 functions from graphics
> Added 903 functions from stats
> Added 399 functions from instrumentr
> Added 39 functions from rprojroot
> Added 103 functions from crayon
> Added 55 functions from digest
> Added 36 functions from assertthat
> Added 13 functions from R6
> Added 21 functions from magrittr
> Added 44 functions from evaluate
> Added 266 functions from stringi
> Added 928 functions from rlang
> Added 11 functions from cachem
> Added 169 functions from fs
> Added 8 functions from ragg
> Added 338 functions from rmarkdown
> Added 372 functions from pkgdown
> Added 6 functions from textshaping
> Added 216 functions from desc
> Added 661 functions from tools
> Added 66 functions from stringr
> Added 6 functions from yaml
> Added 141 functions from xfun
> Added 6 functions from fastmap
> Added 139 functions from compiler
> Added 38 functions from systemfonts
> Added 16 functions from memoise
> Added 137 functions from htmltools
> Added 403 functions from knitr
> [1] "Trace Entry"
> Added 970 functions from vctrs
> Loaded vctrs
> Added 49 functions from generics
> Loaded generics
> Added 47 functions from glue
> Loaded glue
> Added 50 functions from lifecycle
> Loaded lifecycle
> Added 12 functions from ellipsis
> Loaded ellipsis
> Added 216 functions from pillar
> Loaded pillar
> Added 7 functions from pkgconfig
> Loaded pkgconfig
> Added 329 functions from tibble
> Loaded tibble
> Added 282 functions from purrr
> Loaded purrr
> Added 164 functions from tidyselect
> Loaded tidyselect
> Added 678 functions from dplyr
> Loaded dplyr 
> Attached dplyr 
> Detached dplyr 
> Unloaded dplyr 
> Trace Exit [1] ".GlobalEnv"          "package:instrumentr" "package:stats"      
>  [4] "package:graphics"    "package:grDevices"   "package:utils"      
>  [7] "package:datasets"    "package:methods"     "Autoloads"          
> [10] "tools:callr"         "package:base"       
>  [1] "base"        "methods"     "datasets"    "utils"       "grDevices"  
>  [6] "graphics"    "stats"       "instrumentr" "rprojroot"   "crayon"     
> [11] "digest"      "assertthat"  "R6"          "magrittr"    "evaluate"   
> [16] "stringi"     "rlang"       "cachem"      "fs"          "ragg"       
> [21] "rmarkdown"   "pkgdown"     "textshaping" "desc"        "tools"      
> [26] "stringr"     "yaml"        "xfun"        "fastmap"     "compiler"   
> [31] "systemfonts" "memoise"     "htmltools"   "knitr"       "vctrs"      
> [36] "generics"    "glue"        "lifecycle"   "ellipsis"    "pillar"     
> [41] "pkgconfig"   "tibble"      "purrr"       "tidyselect"

print(get_value(result))
> NULL

get_exec_stats(tracer)
>                 callback execution_count minimum_time maximum_time average_time
> 1 tracing_initialization               1     0.000178     0.000178 0.0001780000
> 2   tracing_finalization               1     0.000452     0.000452 0.0004520000
> 3           package_load              11     0.000038     0.002309 0.0002488182
> 4         package_unload               1     0.000037     0.000037 0.0000370000
> 5         package_attach               1     0.000041     0.000041 0.0000410000
> 6         package_detach               1     0.000038     0.000038 0.0000380000
> 7                tracing               1     0.957536     0.957536 0.9575360000
>   total_time
> 1   0.000178
> 2   0.000452
> 3   0.002737
> 4   0.000037
> 5   0.000041
> 6   0.000038
> 7   0.957536

#get_exec_stats(get_application_load_callback(tracer))
#get_exec_stats(get_application_unload_callback(tracer))
#get_exec_stats(get_call_exit_callback(tracer))