Title: | Append 'WITH (NOLOCK)' to 'SQL' Queries, Get Packages in Active Script |
---|---|
Description: | Provides a suite of tools that can assist in enhancing the processing efficiency of 'SQL' and 'R' scripts. - The 'libr_unused()' retrieves a vector of package names that are called within an 'R' script but are never actually used in the script. - The 'libr_used()' retrieves a vector of package names actively utilized within an 'R' script; packages loaded using 'library()' but not actually used in the script will not be included. - The 'libr_called()' retrieves a vector of all package names which are called within an 'R' script. - 'nolock()' appends 'WITH (nolock)' to all tables in 'SQL' queries. This facilitates reading from databases in scenarios where non-blocking reads are preferable, such as in high-transaction environments. |
Authors: | Arkadiusz W. Pajda [aut, cre, cph] |
Maintainer: | Arkadiusz W. Pajda <[email protected]> |
License: | GPL-3 |
Version: | 1.1.0 |
Built: | 2025-03-11 02:59:30 UTC |
Source: | https://github.com/cran/nolock |
Retrieves a vector of all package names which are called within an 'R' script.
libr_called(script = NULL)
libr_called(script = NULL)
script |
Character vector. 'R' script to be processed. If NULL, an active 'R' script is used. |
Retrieves a vector of package names.
script_content <- 'library(rstudioapi) ActiveDocument <- getActiveDocumentContext()$path min_c <- min(c(1,2,3)) require(dplyr) pacman::p_load(tidymodels)' temp_script_path <- tempfile(fileext = ".R") writeLines(script_content, con = temp_script_path) libr_called(script = temp_script_path) unlink(temp_script_path)
script_content <- 'library(rstudioapi) ActiveDocument <- getActiveDocumentContext()$path min_c <- min(c(1,2,3)) require(dplyr) pacman::p_load(tidymodels)' temp_script_path <- tempfile(fileext = ".R") writeLines(script_content, con = temp_script_path) libr_called(script = temp_script_path) unlink(temp_script_path)
Retrieves a vector of package names that are called within an 'R' script but are never actually used in the script.
libr_unused(script = NULL)
libr_unused(script = NULL)
script |
Character vector. 'R' script to be processed. If NULL, an active 'R' script is used. |
Retrieves a vector of package names which are never really used in the code.
script_content <- 'library(rstudioapi) ActiveDocument <- getActiveDocumentContext()$path min_c <- min(c(1,2,3)) require(dplyr) pacman::p_load(tidymodels)' temp_script_path <- tempfile(fileext = ".R") writeLines(script_content, con = temp_script_path) libr_unused(script = temp_script_path) unlink(temp_script_path)
script_content <- 'library(rstudioapi) ActiveDocument <- getActiveDocumentContext()$path min_c <- min(c(1,2,3)) require(dplyr) pacman::p_load(tidymodels)' temp_script_path <- tempfile(fileext = ".R") writeLines(script_content, con = temp_script_path) libr_unused(script = temp_script_path) unlink(temp_script_path)
The 'libr_used()' retrieves a vector of package names actively utilized within an 'R' script; packages loaded using 'library()' but not actually used in the script will not be included.
libr_used(script = NULL)
libr_used(script = NULL)
script |
Character vector. 'R' script to be processed. If NULL, an active 'R' script is used. |
Returns the vector of all package names used in the active 'R' script, based on all the functions used in the script with fully loaded namespaces in the environment.
script_content <- 'library(rstudioapi) ActiveDocument <- getActiveDocumentContext()$path min_c <- min(c(1,2,3)) ' temp_script_path <- tempfile(fileext = ".R") writeLines(script_content, con = temp_script_path) libr_used(script = temp_script_path) unlink(temp_script_path)
script_content <- 'library(rstudioapi) ActiveDocument <- getActiveDocumentContext()$path min_c <- min(c(1,2,3)) ' temp_script_path <- tempfile(fileext = ".R") writeLines(script_content, con = temp_script_path) libr_used(script = temp_script_path) unlink(temp_script_path)
Automatically appends 'WITH (nolock)' to all tables in 'SQL' queries using a utility function. This facilitates reading from databases in scenarios where non-blocking reads are preferable, such as in high-transaction environments.
nolock(query = NULL)
nolock(query = NULL)
query |
Character vector. 'SQL' query to be processed. If NULL, a temporary 'SQL' text document is opened for user input. |
Returns the processed 'SQL' query as a character vector with 'WITH (nolock)' added for each table in the query.
example_SQL <- " WITH CTE AS (SELECT C.TABLE_NAME, C.COLUMN_NAME, T.TABLE_TYPE FROM INFORMATION_SCHEMA.COLUMNS AS C JOIN INFORMATION_SCHEMA.TABLES T ON C.TABLE_NAME = T.TABLE_NAME) SELECT * FROM CTE;" nolock(query = example_SQL) ## Not run: nolock() ## End(Not run)
example_SQL <- " WITH CTE AS (SELECT C.TABLE_NAME, C.COLUMN_NAME, T.TABLE_TYPE FROM INFORMATION_SCHEMA.COLUMNS AS C JOIN INFORMATION_SCHEMA.TABLES T ON C.TABLE_NAME = T.TABLE_NAME) SELECT * FROM CTE;" nolock(query = example_SQL) ## Not run: nolock() ## End(Not run)