# Clear workspace
rm(list = ls())



#
# Read a file line by line and identify loaded libraries
#
# @param   vector  filepath     current file
# @return  vector               identified libraries
#
processFile <- function(filepath) {
    con <- file(filepath, "r")
    libs <- c()
    while (TRUE) {
        line <- readLines(con, n = 1)
        line <- stringr::str_trim(gsub("(#.+)$", "", line)) # remove comments
        if ( length(line) == 0 ) {
            break
        }

        # loaded via "library"
        g <- gregexpr("library\\(([^\\s]+)\\)", line, perl = TRUE,
                      ignore.case = TRUE)
        if (g[[1]] == 1) {
            start <- attr(g[[1]], "capture.start")[1]
            stop <- start + attr(g[[1]], "capture.length")[1] - 1
            libs <- c(libs, substr(line, start = start, stop = stop))
        }

        # directly addressed functions
        g <- gregexpr("([[:alnum:]]+)::", line, perl = TRUE, ignore.case = TRUE)
        if (g[[1]] > 0) {
            start <- attr(g[[1]], "capture.start")[1]
            stop <- start + attr(g[[1]], "capture.length")[1] - 1
            libs <- c(libs, substr(line, start = start, stop = stop))
        }
    }

    close(con)
    libs
}

1 Gather used libraries

# all R syntax files
files <- list.files(here::here("Code"), recursive = FALSE)

# all loaded libraries
libs <- c()
for (file in files) {

    # only check R files
    if (grepl("..+\\.[R]", file, perl = TRUE,
              ignore.case = TRUE)) {

        # extract loaded libraries
        libs <- c(libs, processFile(here::here("Code", file)))
    }
}
rm(files, file, processFile)
(libs <- sort(unique(libs)))
##  [1] "doBy"       "dplyr"      "effectsize" "ggplot2"    "ggpubr"    
##  [6] "haven"      "here"       "Hmisc"      "jsonlite"   "lme4"      
## [11] "lmerTest"   "lmtest"     "merTools"   "openxlsx"   "rmarkdown" 
## [16] "sandwich"   "stringr"    "tidyverse"
# detach all packages
basic.packages <- c("package:stats", "package:graphics",
                    "package:grDevices", "package:utils",
                    "package:datasets", "package:methods",
                    "package:base")
package.list <- search()[ifelse(unlist(gregexpr("package:", search())) == 1, TRUE, FALSE)]
package.list <- setdiff(package.list, basic.packages)
if (length(package.list) > 0) {
    for (package in package.list) detach(package, character.only = TRUE)
}
rm(basic.packages, package.list)

# load all packages
for (lib in libs) {
    suppressPackageStartupMessages({
        library(lib, character.only = TRUE, warn.conflicts = FALSE)
    })
}
rm(libs, lib)

2 R version and packages

sessionInfo()
## R version 4.0.3 (2020-10-10)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19041)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=German_Germany.1252     LC_CTYPE=German_Germany.1252      
## [3] LC_MONETARY=German_Germany.1252    LC_NUMERIC=C                      
## [5] LC_TIME=English_United States.1252
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] forcats_0.5.0    purrr_0.3.4      readr_1.4.0      tidyr_1.1.2     
##  [5] tibble_3.0.4     tidyverse_1.3.0  stringr_1.4.0    sandwich_3.0-0  
##  [9] rmarkdown_2.5    openxlsx_4.2.3   merTools_0.5.2   arm_1.11-2      
## [13] MASS_7.3-53      lmtest_0.9-38    zoo_1.8-8        lmerTest_3.1-3  
## [17] lme4_1.1-25      Matrix_1.2-18    jsonlite_1.7.1   Hmisc_4.4-1     
## [21] Formula_1.2-4    survival_3.2-7   lattice_0.20-41  here_1.0.0      
## [25] haven_2.3.1      ggpubr_0.4.0     ggplot2_3.3.2    effectsize_0.4.0
## [29] dplyr_1.0.2      doBy_4.6.8      
## 
## loaded via a namespace (and not attached):
##   [1] readxl_1.3.1        backports_1.2.0     blme_1.0-4         
##   [4] plyr_1.8.6          TMB_1.7.18          splines_4.0.3      
##   [7] optimx_2020-4.2     digest_0.6.27       foreach_1.5.1      
##  [10] htmltools_0.5.0     fansi_0.4.1         magrittr_1.5       
##  [13] checkmate_2.0.0     cluster_2.1.0       modelr_0.1.8       
##  [16] jpeg_0.1-8.1        colorspace_2.0-0    rvest_0.3.6        
##  [19] xfun_0.19           crayon_1.3.4        iterators_1.0.13   
##  [22] glue_1.4.2          gtable_0.3.0        car_3.0-10         
##  [25] abind_1.4-5         scales_1.1.1        mvtnorm_1.1-1      
##  [28] DBI_1.1.0           rstatix_0.6.0       Rcpp_1.0.5         
##  [31] xtable_1.8-4        htmlTable_2.1.0     foreign_0.8-80     
##  [34] htmlwidgets_1.5.2   httr_1.4.2          RColorBrewer_1.1-2 
##  [37] ellipsis_0.3.1      pkgconfig_2.0.3     farver_2.0.3       
##  [40] nnet_7.3-14         dbplyr_2.0.0        utf8_1.1.4         
##  [43] tidyselect_1.1.0    labeling_0.4.2      rlang_0.4.8        
##  [46] reshape2_1.4.4      later_1.1.0.1       munsell_0.5.0      
##  [49] cellranger_1.1.0    tools_4.0.3         cli_2.1.0          
##  [52] generics_0.1.0      broom_0.7.2         evaluate_0.14      
##  [55] fastmap_1.0.1       yaml_2.2.1          knitr_1.30         
##  [58] fs_1.5.0            zip_2.1.1           packrat_0.5.0      
##  [61] nlme_3.1-149        mime_0.9            xml2_1.3.2         
##  [64] compiler_4.0.3      rstudioapi_0.13     curl_4.3           
##  [67] png_0.1-7           ggsignif_0.6.0      reprex_0.3.0       
##  [70] statmod_1.4.35      stringi_1.5.3       highr_0.8          
##  [73] parameters_0.9.0    nloptr_1.2.2.2      vctrs_0.3.4        
##  [76] pillar_1.4.6        lifecycle_0.2.0     cowplot_1.1.0      
##  [79] data.table_1.13.2   insight_0.10.0      httpuv_1.5.4       
##  [82] R6_2.5.0            latticeExtra_0.6-29 promises_1.1.1     
##  [85] gridExtra_2.3       rio_0.5.16          codetools_0.2-16   
##  [88] boot_1.3-25         assertthat_0.2.1    rprojroot_2.0.2    
##  [91] withr_2.3.0         Deriv_4.1.1         broom.mixed_0.2.6  
##  [94] bayestestR_0.7.5    hms_0.5.3           grid_4.0.3         
##  [97] rpart_4.1-15        coda_0.19-4         minqa_1.2.4        
## [100] carData_3.0-4       numDeriv_2016.8-1.1 shiny_1.5.0        
## [103] lubridate_1.7.9.2   base64enc_0.1-3