Package 'tidycode'

Title: Analyze Lines of R Code the Tidy Way
Description: Analyze lines of R code using tidy principles. This allows you to input lines of R code and output a data frame with one row per function included. Additionally, it facilitates code classification via included lexicons.
Authors: Lucy D'Agostino McGowan [aut, cre] , Jim Hester [ctb], Joshua Rosenberg [ctb], Jeff Leek [ldr]
Maintainer: Lucy D'Agostino McGowan <[email protected]>
License: MIT + file LICENSE
Version: 0.1.1
Built: 2024-10-27 03:04:01 UTC
Source: https://github.com/lucymcgowan/tidycode

Help Index


Get a tidy data frame of classifications of all functions used in your analysis

Description

Get a tidy data frame of classifications of all functions used in your analysis

Usage

get_classifications(lexicon = NULL, include_duplicates = TRUE)

Arguments

lexicon

Character. The classification lexicon to retrieve. Either "crowdsource" or "leeklab". If NULL (default), will return all lexicons.

include_duplicates

Logical. Indicates whether to include all functions and classifications along with their score (default, TRUE) - this may result in multiple lines (with multiple classifications) for a single function. If FALSE, the most prevalent classification will be selected.

Value

A tbl_df with columns:

  • func: the function

  • classification: the classification

If include_duplicates = TRUE, will include a column:

  • score: the score

If lexicon is NULL, will include a column:

  • lexicon: the classification lexicon

Examples

# Get a data frame of all classifications
get_classifications()

# Get a data frame of the most prevalent classifications
get_classifications(include_duplicates = FALSE)

# Get a data frame of only `leeklab` classifications
get_classifications("leeklab")

Get Package Functions

Description

Loads all packages called in a list of R calls and creates a data frame of all functions included in these packages. Packages that were not previously loaded that are loaded as part of this call will be detached.

Usage

get_package_functions(x)

Arguments

x

an R call or list of R calls

Value

a data frame with the following columns:

  • package: The name of the package

  • func: The name of the function

Examples

get_package_functions(
  list(
    quote(library(tidycode)),
    quote(library(purrr))
  )
)

Get a tidy data frame of a "stopword" lexicon for R functions

Description

Get a data frame listing one function per row.

Usage

get_stopfuncs()

Value

A tbl_df with one column:

  • func: the function identified as a "stopword"

Examples

get_stopfuncs()

Load packages

Description

Loads all R packages in the list of R calls.

Usage

load_packages(x)

Arguments

x

an R call or list of R calls

Examples

load_packages(
  list(
    quote(library(tidycode)),
    quote(library(purrr))
  )
)

List packages

Description

List packages

Usage

ls_packages(x)

Arguments

x

an R call or list of R calls

Value

Character. Vector of packages called.

Examples

ls_packages(
  list(
    quote(library(tidycode)),
    quote(library(purrr))
  )
)

Read R file(s) as a tidy data frame

Description

Read R file(s) as a tidy data frame

Usage

read_rfiles(...)

Arguments

...

One or more quoted R file paths to read

Value

A tidy data frame, a tbl_df, with one row per R call. There will be three columns,

  • file: the path of the original R file

  • expr: the R call

  • line: the line of the R call

Examples

d <- read_rfiles(
  tidycode_example("example_plot.R"),
  tidycode_example("example_analysis.R")
)

Get path to example file

Description

tidycode comes bundled with a few small files to use in examples. This function makes them easy to access.

Usage

tidycode_example(path = NULL)

Arguments

path

Name of file. If NULL, the example files will be listed.

Examples

tidycode_example()
tidycode_example("example_plot.R")

Unnest R calls

Description

Unnest R calls

Usage

unnest_calls(.data, input, drop = TRUE)

Arguments

.data

A data frame

input

Input column that contains an R call or list of R calls to be split into individual functions

drop

logical. Whether the original input column should be dropped.

Value

The original data frame with an additional three columns:

  • line: the line number of the call

  • func: the name of the function called

  • args: a list of arguments

Examples

d <- read_rfiles(tidycode_example("example_plot.R"))

# Unnest a model call
d %>%
  unnest_calls(expr)

# Unnest a model call and keep the call itself using the drop parameter
d %>%
  unnest_calls(expr, drop = FALSE)