Add Package Qualifiers to Functions in a Script
checkPkg_add_pkgFnctns_links.RdThis function scans a provided R script for function calls that should have package qualifiers based on the packages currently loaded in the session. If it finds any unqualified function calls, it replaces them with the fully qualified notation [package]::[function].
Details
This function uses the session information to identify which packages are loaded and the functions they contain. It then reads the script, identifies non-qualified function calls, and replaces them with their qualified counterparts if a match is found in the loaded packages.
Examples
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
library(stringr)
library(DescTools)
ex_script <- "mtcars %>%
mutate(
var_1 = Quantile(disp, probs = .6),
var_2 = str_glue('{disp} {hp}'))"
# Write the example script to a temporary file
temp_script <- tempfile(fileext = ".R")
writeLines(ex_script, temp_script)
checkPkg_add_pkgFnctns_links(temp_script)
#> ++++++++++++++++++++++++++++++++++++++++++++++++++
#> Total lines changed: 3
#> ++++++++++++++++++++++++++++++++++++++++++++++++++}
#> mtcars %>%
#> dplyr::mutate( #!!!! LINE CHANGED
#> var_1 = DescTools::Quantile(disp, probs = .6), #!!!! LINE CHANGED
#> var_2 = stringr::str_glue('{disp} {hp}')) #!!!! LINE CHANGED
#> [1] "mtcars %>%\n dplyr::mutate( #!!!! LINE CHANGED\n var_1 = DescTools::Quantile(disp, probs = .6), #!!!! LINE CHANGED\n var_2 = stringr::str_glue('{disp} {hp}')) #!!!! LINE CHANGED"