astgrepr

Parse and Manipulate R Code

https://github.com/etiennebacher/astgrepr

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (16.8%) to scientific vocabulary
Last synced: 6 months ago · JSON representation

Repository

Parse and Manipulate R Code

Basic Info
Statistics
  • Stars: 27
  • Watchers: 1
  • Forks: 2
  • Open Issues: 5
  • Releases: 11
Created about 2 years ago · Last pushed 9 months ago
Metadata Files
Readme Changelog License

README.Rmd

---
output: github_document
---



```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)
```

# astgrepr


[![R-CMD-check](https://github.com/etiennebacher/astgrepr/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/etiennebacher/astgrepr/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/etiennebacher/astgrepr/branch/main/graph/badge.svg)](https://app.codecov.io/gh/etiennebacher/astgrepr?branch=main)
[![CRAN status](https://www.r-pkg.org/badges/version/astgrepr)](https://CRAN.R-project.org/package=astgrepr)


`astgrepr` provides R bindings to the [ast-grep](https://ast-grep.github.io/) 
Rust crate. `ast-grep` is a tool to parse the abstract syntax tree (AST) of some
code and to perform search and rewrite of code. This is extremely useful to build
linters, stylers, and perform a lot of code analysis.

See the example below and the [“Getting started”
vignette](https://astgrepr.etiennebacher.com/articles/astgrepr) for
a gentle introduction to `astgrepr`.

Since `astgrepr` can be used as a low-level foundation for other tools (such as
linters), the number of R dependencies is kept low:

```r
> pak::local_deps_tree()
✔ Loading metadata database ... done
local::. 0.0.1 [new][bld]                                                  
├─checkmate 2.3.1 [new][dl] (746.54 kB)
│ └─backports 1.5.0 [new]
├─rrapply 1.2.7 [new]
└─yaml 2.3.8 [new][dl] (119.08 kB)

Key:  [new] new | [dl] download | [bld] build
```

## Installation

``` r
install.packages("astgrepr")
```

Development version (you might need to install [Rust](https://www.rust-lang.org/tools/install)):
```r
# install.packages("remotes")
remotes::install_github("etiennebacher/astgrepr")
```

## Demo

```{r}
library(astgrepr)

src <- "library(tidyverse)
x <- rnorm(100, mean = 2)
any(duplicated(y))
plot(x)
any(duplicated(x))
any(is.na(variable))"

root <- src |>
  tree_new() |>
  tree_root()

# get everything inside rnorm()
root |>
  node_find(ast_rule(pattern = "rnorm($$$A)")) |>
  node_get_multiple_matches("A") |>
  node_text_all()

# find occurrences of any(duplicated())
root |>
  node_find_all(ast_rule(pattern = "any(duplicated($A))")) |>
  node_text_all()

# find some nodes and replace them with something else
nodes_to_replace <- root |>
  node_find_all(
    ast_rule(id = "any_na", pattern = "any(is.na($VAR))"),
    ast_rule(id = "any_dup", pattern = "any(duplicated($VAR))")
  )

fixes <- nodes_to_replace |>
  node_replace_all(
    any_na = "anyNA(~~VAR~~)",
    any_dup = "anyDuplicated(~~VAR~~) > 0"
  )

# original code
cat(src)

# new code
tree_rewrite(root, fixes)

```

## Related tools

There is some recent work linking `tree-sitter` and R. Those are not competing
with `astgrepr` but are rather a complement to it:

* [`r-lib/tree-sitter-r`](https://github.com/r-lib/tree-sitter-r): provide the
  R grammar to be used with tools built on `tree-sitter`. `astgrepr` relies on
  this grammar under the hood.
* [`DavisVaughan/r-tree-sitter`](https://github.com/DavisVaughan/r-tree-sitter):
  a companion of `r-lib/tree-sitter-r`. This gives a way to get the tree-sitter
  representation of some code directly in R. This is useful to learn how 
  tree-sitter represents the R grammar, which is required if you want advanced
  use of `astgrepr`. However, it doesn't provide a way to easily select specific
  nodes (e.g. based on patterns).

Owner

  • Name: Etienne Bacher
  • Login: etiennebacher
  • Kind: user

GitHub Events

Total
  • Create event: 9
  • Release event: 2
  • Issues event: 4
  • Watch event: 7
  • Delete event: 2
  • Issue comment event: 18
  • Push event: 51
  • Pull request event: 17
  • Fork event: 4
Last Year
  • Create event: 9
  • Release event: 2
  • Issues event: 4
  • Watch event: 7
  • Delete event: 2
  • Issue comment event: 18
  • Push event: 51
  • Pull request event: 17
  • Fork event: 4

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 22
  • Total pull requests: 31
  • Average time to close issues: 19 days
  • Average time to close pull requests: 7 days
  • Total issue authors: 3
  • Total pull request authors: 3
  • Average comments per issue: 1.27
  • Average comments per pull request: 0.65
  • Merged pull requests: 24
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 6
  • Pull requests: 23
  • Average time to close issues: 27 days
  • Average time to close pull requests: 3 days
  • Issue authors: 2
  • Pull request authors: 3
  • Average comments per issue: 1.0
  • Average comments per pull request: 0.65
  • Merged pull requests: 16
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • etiennebacher (23)
  • IndrajeetPatil (1)
  • Exceret (1)
Pull Request Authors
  • etiennebacher (30)
  • JosiahParry (5)
  • mpadge (2)
Top Labels
Issue Labels
help wanted (1) false-positive (1)
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • cran 362 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 2
  • Total maintainers: 1
cran.r-project.org: astgrepr

Parse and Manipulate R Code

  • Versions: 2
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 362 Last month
Rankings
Dependent packages count: 26.4%
Dependent repos count: 32.5%
Average: 48.5%
Downloads: 86.6%
Last synced: 7 months ago

Dependencies

.github/workflows/R-CMD-check.yaml actions
  • actions/checkout v4 composite
  • r-lib/actions/check-r-package v2 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/pkgdown.yaml actions
  • JamesIves/github-pages-deploy-action v4.5.0 composite
  • actions/checkout v4 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
src/rust/Cargo.lock cargo
  • 114 dependencies
src/rust/Cargo.toml cargo
DESCRIPTION cran
  • checkmate * imports
  • rrapply * imports
  • stringi * imports
  • utils * imports
  • yaml * imports
  • knitr * suggests
  • rmarkdown * suggests
  • rstudioapi * suggests
  • tinytest * suggests