knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Provide some example R scripts locally

In order to test the functions of this package some R scripts are downloaded from GitHub and provided locally:

# Define URLs to some example scripts
urls <- kwb.utils::resolve(list(
  kwb = "https://raw.githubusercontent.com/KWB-R",
  utils = "<kwb>/kwb.utils/master/R",
  log = "<utils>/log.R",
  main = "<utils>/main.R",
  fakin = "<kwb>/kwb.fakin/master/R/plot_file_distribution.R"
))

# Create a temporary folder
root <- kwb.utils::createDirectory(
  kwb.utils::tempSubdirectory("test"), 
  dbg = FALSE
)

# Helper function to download a text file to the temporary folder
download_script <- function(url) {
  download.file(
    url, 
    destfile = file.path(root, basename(url)), 
    mode = "wt"
  )
}

# Download three scripts to the temporary folder
download_script(urls$fakin)
download_script(urls$log)
download_script(urls$main)

Exported functions

analyse()

The parse tree is analysed. Each node of the tree is given the following attributes:

The idea probably was to use these information to extract objects of special interest from the parse tree (see below: get_elements_by_type())

x <- parse(urls$log)

result <- kwb.code::analyse(x)

arg_names()

This function returns the names of the arguments of a function:

kwb.code::arg_names(kwb.utils::selectColumns)

find_string_constants()

This function requires a directory of R scripts. All scripts are parsed. String constants that are used in the script are returned.

string_constants <- kwb.code::find_string_constants(root)

knitr::kable(string_constants)

find_weaknesses_in_scripts()

Check for expressions in scripts that can be improved.

x <- parse(text = c(
  "texts <- c(", 
  "  paste('this is a very long', 'text'),",
  "  paste('this is a very long', 'string')",
  ")",
  "",
  "indices <- 1:length(texts)"
))

weaknesses <- kwb.code::find_weaknesses_in_scripts(
  x = list(test = x), 
  min_duplicate_frequency = 2L
)

knitr::kable(weaknesses)

get_elements_by_type

This function groups similar elements that are found in a parse tree.

# Parse an R script file (here, a file from kwb.utils)
x <- parse(urls$log)

# For each "type" of code segment, extract all occurrences
elements <- kwb.code::get_elements_by_type(x, result = result)

# Show all code blocks in curly braces
elements[["language|call|{|2|"]]

get_full_function_info()

This function analyses a list of parse trees each of which has been read from an R script.

It provides information on the functions that are defined in the scripts:

trees <- kwb.code::parse_scripts(root, dbg = FALSE)
function_info <- kwb.code::get_full_function_info(trees)
knitr::kable(function_info)

get_names_of_used_packages()

What packages are used in the scripts?

kwb.code::get_names_of_used_packages(root)

This function simply looks for calls to library(). It does not take into account functions that are called with :: as the following simple grep() reveals:

pattern <- "[^A-Za-z_.]([A-Za-z_.]+::[A-Za-z_.]+)[^A-Za-z_.]"

text <- grep(pattern, readLines(urls$fakin), value = TRUE)

unique(kwb.utils::extractSubstring(pattern, text, index = 1))

TODO: Use another function instead...

get_package_function_usage()

tree <- kwb.code::parse_scripts(root, dbg = FALSE)
function_usage <- kwb.code::get_package_function_usage(
  tree, 
  package = "kwb.utils"
)

knitr::kable(function_usage)

get_package_usage_per_script()

package_usage <- kwb.code::get_package_usage_per_script(
  root, 
  packages = "kwb.utils"
)
knitr::kable(package_usage)

get_string_constants_in_scripts()

string_constants <- kwb.code::get_string_constants_in_scripts(root)
knitr::kable(string_constants)

parse_scripts()

x <- kwb.code::parse_scripts(root)

str(x, 2)

to_full_script_info()

This function creates statistics about R scripts.

script_statistics <- kwb.code::to_full_script_info(trees)

knitr::kable(script_statistics)

walk_tree()

This function walks along a parse tree.

x <- parse(urls$log)

result <- kwb.code::walk_tree(x, dbg = FALSE)


KWB-R/kwb.code documentation built on July 28, 2023, 5:57 p.m.