Logging Unapproved Package and Function Use

1. Create a named list

The named list contains the functions approved for use for each package. If all functions for a package are approved for use, list “All”.

approved_pkgs <- list(base = "mean",
                      dplyr = "All")
approved_pkgs
#> $base
#> [1] "mean"
#> 
#> $dplyr
#> [1] "All"

2. Build approved.rds

Pass the named list through build_approved() to build your tibble.

If We create a temp directory to save this for illustration.

build_approved(approved_pkgs)
#> # A tibble: 289 × 2
#>    function_name library      
#>    <chr>         <chr>        
#>  1 mean          package:base 
#>  2 rows_upsert   package:dplyr
#>  3 src_local     package:dplyr
#>  4 db_analyze    package:dplyr
#>  5 n_groups      package:dplyr
#>  6 distinct      package:dplyr
#>  7 summarise_    package:dplyr
#>  8 group_split   package:dplyr
#>  9 group_by_if   package:dplyr
#> 10 sample_n      package:dplyr
#> # … with 279 more rows

3. Save as approved.rds

You can use the file argument in build_approved() to save approved.rds instead of return the tibble.

dir <- tempdir()

build_approved(approved_pkgs, file = file.path(dir, "approved.rds"))

4. Update the logrx.approved option

Update the logrx.approved option to point to your approved.rds location. We recommend setting this in your .Rprofile.

options(logrx.approved = file.path(dir, "approved.rds"))

5. You’re done. Let’s axecute!

logrx will take it from there. When each program is executed, packages and functions will be compared to approved.rds and if any unapproved use is found, it will be logged within the “Unapproved Package and Functions” section.

Example

Let’s write a simple script summarizing mean mpg from mtcars. We save this as mpg.R in the temporary directory dir and axecute() it.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

results <- mtcars %>%
   group_by(cyl) %>%
   summarize(mean = mean(mpg)) %>%
   mutate(label = "Mean MPG")

results %>%
   tidyr::pivot_wider(names_from = cyl, values_from = mean, id_cols = label)
#> # A tibble: 1 × 4
#>   label      `4`   `6`   `8`
#>   <chr>    <dbl> <dbl> <dbl>
#> 1 Mean MPG  26.7  19.7  15.1
axecute(file.path(dir,"mpg.R"), remove_log_object = FALSE)

Here we have the log elements for “Used Package and Functions” and “Unapproved Package and Functions”. We can see we used library() from package:base and pivot_wider from package:tidyr. We did not include the base library or tidyr functions in our approved list, so this has been logged!

#> --------------------------------------------------------------------------------
#> -                          Used Package and Functions                          -
#> --------------------------------------------------------------------------------
#> {package:base} library, mean
#> {package:dplyr} %>%, group_by, summarize, mutate
#> {package:tidyr} pivot_wider
#> --------------------------------------------------------------------------------
#> -                       Unapproved Package and Functions                       -
#> --------------------------------------------------------------------------------
#> {package:base} library
#> {package:tidyr} pivot_wider