Skip to content

Commit

Permalink
version 0.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis D. Verde Arregoitia authored and cran-robot committed Oct 7, 2022
1 parent 89d3b7b commit 34e7179
Show file tree
Hide file tree
Showing 20 changed files with 263 additions and 63 deletions.
10 changes: 5 additions & 5 deletions DESCRIPTION
@@ -1,6 +1,6 @@
Package: annotater
Title: Annotate Package Load Calls
Version: 0.2.1
Version: 0.2.2
Authors@R:
person(given = "Luis D.",
family = "Verde Arregoitia",
Expand All @@ -12,17 +12,17 @@ Description: Provides non-invasive annotation of package load calls
the packages we are loading are meant for.
License: MIT + file LICENSE
Encoding: UTF-8
RoxygenNote: 7.2.0
RoxygenNote: 7.2.1
Imports: stringr, purrr, tibble, dplyr, stringi, readr, rstudioapi,
tidyr, rlang
tidyr, rlang,
URL: https://github.com/luisDVA/annotater, https://annotater.liomys.mx
BugReports: https://github.com/luisDVA/annotater/issues
Suggests: testthat, knitr, rmarkdown, covr
VignetteBuilder: knitr
NeedsCompilation: no
Packaged: 2022-06-14 18:14:33 UTC; luisd
Packaged: 2022-10-07 14:36:52 UTC; luisd
Author: Luis D. Verde Arregoitia [aut, cre]
(<https://orcid.org/0000-0001-9520-6543>)
Maintainer: Luis D. Verde Arregoitia <luis@liomys.mx>
Repository: CRAN
Date/Publication: 2022-06-15 13:20:02 UTC
Date/Publication: 2022-10-07 14:50:06 UTC
34 changes: 19 additions & 15 deletions MD5
@@ -1,34 +1,36 @@
6f7bc9eee40deda1feff0ac88e688e25 *DESCRIPTION
3a5e8a85c388777b667cd0611e038cbf *DESCRIPTION
f569df5601902781594d60ee0009d0d9 *LICENSE
cf49980b95a429093918e56db7f0ae3f *NAMESPACE
ab7cbbff65e29f6a0aa707466daa408b *NEWS.md
7f9395b333824161693816dedcd527f2 *R/align_annotations.R
d0f23b7e98963ba13b6ab51efb1755ad *NAMESPACE
7e54aa5678b22197a803ecd739a5cd58 *NEWS.md
e33bfcf87680c0771997a6df1d049129 *R/align_annotations.R
8193c4d883d22c93b58e994e927e5388 *R/annotate_active_file.R
c1782dcc6c5eb860e7e15406e4170c7d *R/annotate_fun_calls.R
ca660baa01383046e7bb0a0c7b6a93fa *R/annotate_fun_calls_active_file.R
9da45dd3a5ab7dba0291728f09a51253 *R/annotate_pkg_calls.R
5fca3cda84a9f5a6bca9994e239b65b7 *R/annotate_repo_source.R
76329c9c189fb92de8de4869fb23350c *R/annotate_repo_source.R
2c855231707ddc20b35d741f16308f6a *R/annotate_repos_active_file.R
544a2cb53354706fa2c29d9493d75455 *R/annotate_repostitle.R
0780398673a42c5314203e855aaa52d5 *R/annotate_repostitle.R
ef5fb3f446ed3b9b6ee3a39ebb9ff84a *R/annotate_repostitle_active.R
c82e29f3ad96ade59b859a9bf8beb60c *R/annotate_script.R
b4fe12876c37f8a22cde06e9701eecf0 *R/annotater-package.R
0de7ac9356c70ce748761e7f4e4c4f90 *R/match_pkg_names.R
b7ef0be9f866854ea54db4fdbefb26db *R/expand_metapackages.R
7eadd5f38759c93130b58d89d68a40fb *R/expand_metapackages_active.R
2dd0e12513675bfa8aec05859991b3d4 *R/match_pkg_names.R
789009f36e4b9826c70b6264b165d1da *R/pkg_version.R
02f81c4174b2c4fdca8f1f4c5f5dd014 *R/repo_details.R
c903d4fd54a4f73fc477bc6a33ef354a *README.md
6eef26e70fec4a8cd577aceae5ff57e7 *build/vignette.rds
77e4ba9ed916407deb3c312bd1f094e2 *README.md
1b92dc9910cb8c3a40d44151de6e3134 *build/vignette.rds
1003aafa9c0de00e925a1e26501cb8e4 *inst/doc/annotater_intro.R
ece7f68dff9eb24d75ab8b151e89b4a7 *inst/doc/annotater_intro.Rmd
633baa6dfe232c49a6d7730983743f5b *inst/doc/annotater_intro.html
2e7f9d9adc7dda2246ccdafb90027c8b *inst/doc/annotater_intro.Rmd
3a5c26443f351f498b42927f37df239e *inst/doc/annotater_intro.html
90f73f1fdff15ab8e46d2aee044ec8c4 *inst/media/annotcalls.gif
a9d0346f4b4ef35611a0d65ac46f8dfa *inst/media/annotpacmanFns.gif
96feda3145925f69e7f3206e277a5665 *inst/media/annotpacmanRepos.gif
1c08b88020b2bc935d39e518877f333b *inst/media/jcruz.gif
19551c666ace53dcedbc1c3d65f7740e *inst/media/repos2.gif
66d0ef6ba454d9da041153b30827618a *inst/media/repostitles.gif
79f14201262f21c45f90af5cc9cf5701 *inst/rstudio/addins.dcf
0d031cd663b6a655024d2bf1d12bf266 *man/align_annotations.Rd
80c4504b4801f2d7cc5f3a8c9439a4c3 *inst/rstudio/addins.dcf
74265ebc892426c1af08dbd263cdd2af *man/align_annotations.Rd
d1077a7f355c9ad950dfd0e9756b3b1f *man/annotate_active_file.Rd
4a4e4ec85b35746ac29f52f447284888 *man/annotate_fun_calls.Rd
414c92e436b085ed3b3d48fce86f43f9 *man/annotate_fun_calls_active_file.Rd
Expand All @@ -39,6 +41,8 @@ d4c8f99b9c1c060dee557fc777d662e2 *man/annotate_repos_active_file.Rd
0d7acf00566b16beeeb9836c02539e75 *man/annotate_repostitle_active.Rd
61ad7de1afbd073f529dcb7fe8d18253 *man/annotate_script.Rd
b2cc3e8d424b40fccf00fa142987107c *man/annotater-package.Rd
0fee9cb4042ea01c7734dfe87dd8a9bc *man/expand_metapackages.Rd
c1bb18922449dfba9f1c1b30cacff1d0 *man/expand_metapackages_active_file.Rd
c76485bd8263191cf80864d6dbeb2cd9 *man/figures/logo.png
f8dabb44ecfff01eba28124f3f6b922a *man/match_pkg_names.Rd
aa3b041759ebfced3a544be598c50a09 *man/pkg_version.Rd
Expand All @@ -48,6 +52,6 @@ b4179a19d64f0ddadb63448549cd6f85 *tests/testthat/annotating_scripts/demo-script.
cb9059966495f2dfe76b0bb158be6555 *tests/testthat/test-annotate_fun_calls.R
c84ec794747dc4a183ddc0614a79ee5a *tests/testthat/test-annotate_script.R
4676e05b169839a2834d6b4661094cda *tests/testthat/test-match_pkg_names.R
2905970833776f00a30b03de37761ebd *tests/testthat/test-overall-annotation.R
2d73b58271b99d090bda351e25a21b16 *tests/testthat/test-overall-annotation.R
d41d8cd98f00b204e9800998ecf8427e *tests/testthat/testing-helpers.R
ece7f68dff9eb24d75ab8b151e89b4a7 *vignettes/annotater_intro.Rmd
2e7f9d9adc7dda2246ccdafb90027c8b *vignettes/annotater_intro.Rmd
3 changes: 3 additions & 0 deletions NAMESPACE
Expand Up @@ -9,6 +9,8 @@ export(annotate_repos_active_file)
export(annotate_repostitle)
export(annotate_repostitle_active)
export(annotate_script)
export(expand_metapackages)
export(expand_metapackages_active_file)
export(match_pkg_names)
importFrom(dplyr,`%>%`)
importFrom(dplyr,filter)
Expand All @@ -19,4 +21,5 @@ importFrom(rstudioapi,getActiveDocumentContext)
importFrom(rstudioapi,modifyRange)
importFrom(stringi,stri_replace_all_fixed)
importFrom(stringi,stri_split_lines1)
importFrom(stringr,str_detect)
importFrom(tibble,rowid_to_column)
6 changes: 6 additions & 0 deletions NEWS.md
@@ -1,3 +1,9 @@
# annotater 0.2.2

* Bug fixes
* Add function to expand metapackages
* Fix failing test affecting purrr

# annotater 0.2.1

* Additional unit tests
Expand Down
3 changes: 2 additions & 1 deletion R/align_annotations.R
Expand Up @@ -8,7 +8,8 @@
#' @return A character vector with vertically aligned package calls.
#'
align_annotations <- function(annot_string,
regex_ai = "(?!\\))(?!\\s)(?=\\#\\sCR|\\#\\sBio|\\#\\snot i|\\#\\s\\[)", sep_str = "") {
regex_ai = "(?!\\))(?!\\s)(?=\\#\\sCRAN\\sv|\\#\\sBiocon|\\#\\snot\\sinstal|\\#\\s\\[)",
sep_str = "") {
if (!is.character(annot_string)) {
stop("input 'stringvec' must be a character vector")
}
Expand Down
4 changes: 2 additions & 2 deletions R/annotate_repo_source.R
Expand Up @@ -56,7 +56,7 @@ annotate_repo_source <- function(string_og) {
pacld <- pck_descs[stringr::str_detect(out_tb$call, ".+load\\("), ]
pacld$pkgnamesep <- paste0(pacld$package_name, ",")
pacld <- dplyr::mutate(dplyr::group_by(pacld, call), pkgnamesep = ifelse(dplyr::row_number() == dplyr::n(), gsub(",", "", .data$pkgnamesep), .data$pkgnamesep))
pacld$annotatedpac <- paste(pacld$pkgnamesep, "#", pacld$annotation, " v", pacld$version)
pacld$annotatedpac <- paste0(pacld$pkgnamesep, " # ", pacld$annotation, " v", pacld$version)
pacld <- dplyr::summarize(dplyr::group_by(pacld, call), pkgs = paste(.data$annotatedpac, collapse = "\n"))
pacld$ldcalls <- stringr::str_extract(pacld$call, ".+\\(")
pacld <- dplyr::mutate(pacld, annotpac = paste(.data$ldcalls, .data$pkgs, ")", sep = "\n"))
Expand All @@ -72,7 +72,7 @@ annotate_repo_source <- function(string_og) {
pacld <- pck_descs[stringr::str_detect(out_tb$call, ".+load\\("), ]
pacld$pkgnamesep <- paste0(pacld$package_name, ",")
pacld <- dplyr::mutate(dplyr::group_by(pacld, call), pkgnamesep = ifelse(dplyr::row_number() == dplyr::n(), gsub(",", "", .data$pkgnamesep), .data$pkgnamesep))
pacld$annotatedpac <- paste(pacld$pkgnamesep, "#", pacld$annotation, " v", pacld$version)
pacld$annotatedpac <- paste0(pacld$pkgnamesep, " # ", pacld$annotation, " v", pacld$version)
pacld <- dplyr::summarize(dplyr::group_by(pacld, call), pkgs = paste(.data$annotatedpac, collapse = "\n"))
pacld$ldcalls <- stringr::str_extract(pacld$call, ".+\\(")
pacld <- dplyr::mutate(pacld, annotpac = paste(.data$ldcalls, .data$pkgs, ")", sep = "\n"))
Expand Down
2 changes: 1 addition & 1 deletion R/annotate_repostitle.R
Expand Up @@ -51,7 +51,7 @@ annotate_repostitle <- function(string_og) {

# build annotations
if (all(!grepl("p_load", pck_descs$call))) { # no pacman calls
pck_descs$annotated <- paste0(pck_descs$package_name, " # ", pck_descs$title, ", ", pck_descs$annotation, " v", pck_descs$version)
pck_descs$annotated <- paste0(pck_descs$call, " # ", pck_descs$title, ", ", pck_descs$annotation, " v", pck_descs$version)

return(
align_annotations(stringi::stri_replace_all_fixed(
Expand Down
89 changes: 89 additions & 0 deletions R/expand_metapackages.R
@@ -0,0 +1,89 @@
#' Expand metapackages
#'
#' @param string_og text string (script) with package load calls
#'
#' @return Text string with metapackage load calls replaced by multiple separate
#' calls to the core packages that make up the metapackage. Core packages will
#' be fenced in four commenting symbols and the order follows the attachment
#' order from each metapackage. Will make note of metapackages not currently
#' installed.
#'
#' @examples
#' test_string <- c("library()\nlibrary(tidyverse)")
#'
#' @importFrom stringr str_detect
#' @importFrom rlang .data
#'
#' @export
#'
expand_metapackages <- function(string_og){
supported_metapackages <- c("tidyverse","tidymodels","easystats")
# metapackage components as of October 6 2022

# tidyverse v1.3.2
tidyverse_core <- c(
"ggplot2", "tibble", "tidyr",
"readr", "purrr", "dplyr",
"stringr", "forcats","lubridate"
)

# tidymodels v1.0.0
tidymodels_core <- c(
"broom", "dials", "dplyr", "ggplot2", "infer",
"modeldata", "parsnip", "purrr", "recipes", "rsample", "tibble",
"tidyr", "tune", "workflows", "workflowsets", "yardstick"
)

# easystats 0.5.2
easystats_core <- c(
"insight", "datawizard", "bayestestR", "performance", "parameters", "effectsize",
"modelbased", "correlation", "see", "report"
)
out_tb <- match_pkg_names(string_og) # list, ordered, packages loading
if (nrow(out_tb) == 0) {
# if no library or require calls, then return same string.
cat("no matching library load calls")
return(string_og)
}

metapkgIndices <- apply(outer(out_tb$pkgname_clean,supported_metapackages, stringr::str_detect), 1, any)
meta_out_tb <- out_tb[metapkgIndices,]

if (nrow(meta_out_tb) == 0) {
# if no metapackages detected, then return same string.
cat("no metapackages")
return(string_og)
}

# check if installed
meta_out_tb$installed <- purrr::map_lgl(meta_out_tb$pkgname_clean,
function(x) {suppressWarnings(nzchar(system.file(package = paste(x))))})

meta_out_tb$load_call <- stringr::str_extract(meta_out_tb$call,"^[ \t]*.+(?=\\()")

meta_out_tb$expand_list <-
ifelse(meta_out_tb$pkgname_clean=="tidymodels"&meta_out_tb$installed==TRUE,
paste(tidymodels_core,collapse = ","),NA)
meta_out_tb$expand_list <-
ifelse(meta_out_tb$pkgname_clean=="tidyverse"&meta_out_tb$installed==TRUE,
paste(tidyverse_core,collapse = ","),meta_out_tb$expand_list)
meta_out_tb$expand_list <-
ifelse(meta_out_tb$pkgname_clean=="easystats"&meta_out_tb$installed==TRUE,
paste(easystats_core,collapse = ","),meta_out_tb$expand_list)
meta_out_tb$expand_list <-
ifelse(is.na(meta_out_tb$expand_list)&meta_out_tb$installed==FALSE,
meta_out_tb$pkgname_clean,meta_out_tb$expand_list)

# build replacement load calls
meta_out_tb <- tidyr::separate_rows(meta_out_tb,.data$expand_list)
meta_out_tb$replacement_call <- paste0(meta_out_tb$load_call,"(",meta_out_tb$expand_list,")")
meta_out_tb$replacement_call <- ifelse(meta_out_tb$installed==FALSE,paste(meta_out_tb$replacement_call,"# not installed"),meta_out_tb$replacement_call)
meta_out_tb <- dplyr::summarize(dplyr::group_by(meta_out_tb,call),replacement_call=paste0(.data$replacement_call,collapse = "\n"))
meta_out_tb$replacement_call <- ifelse(stringr::str_detect(meta_out_tb$replacement_call,"not installed"),
meta_out_tb$replacement_call,paste0("####\n",meta_out_tb$replacement_call,"\n####"))

stringi::stri_replace_all_fixed(
str = string_og, pattern = meta_out_tb$call,
replacement = meta_out_tb$replacement_call, vectorize_all = FALSE)
}

23 changes: 23 additions & 0 deletions R/expand_metapackages_active.R
@@ -0,0 +1,23 @@
#' Expand metapackages in Active File
#'
#' Replaces metapackage load calls in the active .R file with multiple separate
#' calls to its core packages.
#'
#' @return No return value, wraps [expand_metapackages()] for access via Addin
#'
#' @export
expand_metapackages_active_file <- function() {
context <- rstudioapi::getActiveDocumentContext()
if (context[1]$id == "#console") {
stop("Focus (blinking cursor) is not on an open R file")
}
contents_parsed <- paste0(context$contents, sep = "\n", collapse = "")
out <- annotater::expand_metapackages(contents_parsed)
outlines <- stringi::stri_split_lines1(out)

rstudioapi::modifyRange(
c(1, 1, length(context$contents) + 1, 1),
paste0(append(outlines, ""), collapse = "\n"),
id = context$id
)
}
2 changes: 1 addition & 1 deletion R/match_pkg_names.R
Expand Up @@ -15,7 +15,7 @@ match_pkg_names <- function(string_og) {
tb_names <- c("call", "package_name")
uncommented_str <- stringr::str_match_all(string_og, stringr::regex("^(?:(?!#).)*$", multiline = TRUE))
uncommented_str <- paste0(collapse = "\n", purrr::flatten_chr(uncommented_str))
lib_matches <- stringr::str_match_all(uncommented_str, stringr::regex("^\\s*?library\\((.*)\\)", multiline = TRUE))
lib_matches <- stringr::str_match_all(uncommented_str, stringr::regex("^[ \t]*library\\((.*)\\)", multiline = TRUE))
colnames(lib_matches[[1]]) <- tb_names
lib_tb <- tibble::as_tibble(lib_matches[[1]])
req_matches <- stringr::str_match_all(uncommented_str, stringr::regex("^require\\((.*)\\)", multiline = TRUE))
Expand Down
22 changes: 17 additions & 5 deletions README.md
Expand Up @@ -7,8 +7,12 @@

<!-- badges: start -->

[![CRAN
status](https://www.r-pkg.org/badges/version/annotater)](https://CRAN.R-project.org/package=annotater)
[![Codecov test
coverage](https://codecov.io/gh/luisDVA/annotater/branch/master/graph/badge.svg)](https://app.codecov.io/gh/luisDVA/annotater?branch=master)
coverage](https://codecov.io/gh/luisDVA/annotater/branch/master/graph/badge.svg)](https://app.codecov.io/gh/luisDVA/annotater?branch=master)
[![](http://cranlogs.r-pkg.org/badges/last-month/annotater?color=orange)](https://cran.r-project.org/package=annotater)
[![](http://cranlogs.r-pkg.org/badges/grand-total/annotater?color=blue)](https://cran.r-project.org/package=annotater)
<!-- badges: end -->

The goal of `annotater` is to annotate package load calls in character
Expand Down Expand Up @@ -49,9 +53,11 @@ readability.

## Installation

Install the development version of `annotater` from GitHub with:
Install the CRAN release or the development version with:

``` r
# Install annotater from CRAN:
install.packages("annotater")
# install.packages("remotes")
remotes::install_github("luisDVA/annotater")
```
Expand Down Expand Up @@ -82,9 +88,15 @@ Entire .R files can also be parsed and annotated with the
The `tidyverse` package is a meta-package with few exported functions of
its own, so the annotation tools provided here (`annotate_fun_calls`)
will not match the functions from the various individual packages (such
as `dplyr` or `readr`) that get attached when loading `tidyverse`. This
is by design, so consider loading individual packages if annotations for
function calls are desired.
as `dplyr` or `readr`) that get attached when loading `tidyverse`.
Consider using the experimental `expand_metapackages` function first if
annotations for function calls are desired.

### More on metapackages

`annotater` can now expand metapackage load calls into separate
`library` calls for the individual packages that form the core of
`tidyverse`, `tidymodels`, and `easystats`.

Feedback welcome

Expand Down
Binary file modified build/vignette.rds
Binary file not shown.
12 changes: 6 additions & 6 deletions inst/doc/annotater_intro.Rmd
Expand Up @@ -18,7 +18,7 @@ knitr::opts_chunk$set(
library(annotater)
```

`annotater` came about after teaching workshops or helping peers and realizing that many issues relate to package installation failures and dependency issues for packages that were not even used in a problematic script. Scripts get passed around, code is copied and pasted, and we might not know what certain packages are for. Additionally, it is often useful to specify a package's source and version within a script, for reproducibility purposes and to keep a record of where any the packages can be obtained.
`annotater` came about after teaching workshops or helping peers and realizing that many issues relate to package installation failures and dependency issues for packages that were not even used in a problematic script. Scripts get passed around, code is copied and pasted, and we might not know what certain packages are for. Additionally, it is often useful to specify a package's source and version within a script, for reproducibility purposes and to keep a record of where any of the packages can be obtained.


## Package functions
Expand Down Expand Up @@ -66,9 +66,9 @@ map(x, pluck, 2)')
annotate_fun_calls(testcode)
```

## A note on the `tidyverse`
The `tidyverse` package is a meta-package with few exported functions of its own, so the annotation tools provided here (`annotate_fun_calls`) will not match the functions from the various individual packages (such as `dplyr` or `readr`) that get attached when loading `tidyverse`. This is by design, so consider loading individual packages if annotations for function calls are desired.

## A note on the `tidyverse` and other metapackages
The `tidyverse` package is a meta-package with few exported functions of its own, so the annotation tools provided here (`annotate_fun_calls`) will not match the functions from the various individual packages (such as `dplyr` or `readr`) that get attached when loading `tidyverse`. As of version 0.2.2, load calls for metapackages can be replaced with separate calls to each of the core metapackage components as defined by their respective _attach.R_
files.

## Usage in RStudio

Expand All @@ -80,11 +80,11 @@ Describes the packages being loaded by calling `annotate_pkg_calls`.
#### Annotate package repository sources in active file
Adds the source and version by calling `annotate_repo_source`. This addin aligns the commenting symbols vertically for aesthetic purposes.


#### Annotate titles and repository sources in active file
Adds titles, sources, and versions by calling `annotate_repostitles`.


#### Annotate each package's function calls
Adds all the unique functions being called by each loaded package, calls `annotate_fun_calls`.

#### Expand metapackages
Replace a call to a metapackage with multiple calls to each of its core components.

0 comments on commit 34e7179

Please sign in to comment.