/grepr

Search for Regular Expressions and Mark Matches

Primary LanguageROtherNOASSERTION

grepr

grepr aims to provide a similar functionality as the bash command grep and display the results as RStudio source markers

Installation

You can install the developent version of grepr from GitHub with:

remotes::install_github("GregorDeCillia/grepr")

Example

The function grepr can be used to find matches in files based on a regular expression.

library(grepr)
grepr("message")[5:9, ]
file line content
R/grepr.R 60     message = readLines(file)[line],
R/print.R 5     rstudioapi::sourceMarkers(name = “grepr”, markup_messages(x),
R/print.R 9     message(“no matches to show”)
R/print.R 12 markup_messages <- function(matches, env_open = “<font color=‘red’>”,
R/print.R 20     message <- match$message

If grepr is called in RStudio, the results are displayed as RStudio source markers.

Arguments

Argument Description
pattern a character string containing a regular expression
dir a directory from which the search is conducted. Defaults to the working directory.
ignore_dotfiles should hidden files be ignored? Defaults to TRUE
file_pattern a regular expression which can be used to exclude certain files from the search

Umlauts

grep_umlauts is a wrapper around grepr which looks for umlauts via the pattern argument. It can be used with a flag ignore_roxygen to skip Rd Files and roxygen comments.

Supported symbols: Ö, Ä, Ü, ö, ä, ü, ß

grep_umlauts(file_pattern = "README\\.Rmd")
file line content
README.Rmd 61 Supported symbols: Ö, Ä, Ü, ö, ä, ü, ß
README.Rmd 61 Supported symbols: Ö, Ä, Ü, ö, ä, ü, ß
README.Rmd 61 Supported symbols: Ö, Ä, Ü, ö, ä, ü, ß
README.Rmd 61 Supported symbols: Ö, Ä, Ü, ö, ä, ü, ß
README.Rmd 61 Supported symbols: Ö, Ä, Ü, ö, ä, ü, ß
README.Rmd 61 Supported symbols: Ö, Ä, Ü, ö, ä, ü, ß
README.Rmd 61 Supported symbols: Ö, Ä, Ü, ö, ä, ü, ß

The function print_substitution_table() can be used to suggest alternatives

print_substitution_table()
#> the following substitutions should be used if R CMD check adds NOTEs because of umlauts
#> Ä: \u00c4    ä: \u00e4   Ö: \u00d6   ö: \u00f6   Ü: \u00dc   ü: \u00fc   ß: \u00df   

Class methods

Calls to grepr::grepr() create objects of the class grepr. Those objects can be used in the following ways.

  • printing an object in an interactive console will display the matches with rstudioapi::sourceMarkers()
  • printing an object in a RMarkdown file will render the matches with knitr::kable()
  • the generic as.data.frame() converts the matches into a tidy data.frame that contains filenames, row numbers, colum numbers and lengths of the matches.
grep_umlauts(file_pattern = "README\\.Rmd") %>% as.data.frame()
#>         file line column length                                message
#> 1 README.Rmd   61     20      1 Supported symbols: Ö, Ä, Ü, ö, ä, ü, ß
#> 2 README.Rmd   61     23      1 Supported symbols: Ö, Ä, Ü, ö, ä, ü, ß
#> 3 README.Rmd   61     26      1 Supported symbols: Ö, Ä, Ü, ö, ä, ü, ß
#> 4 README.Rmd   61     29      1 Supported symbols: Ö, Ä, Ü, ö, ä, ü, ß
#> 5 README.Rmd   61     32      1 Supported symbols: Ö, Ä, Ü, ö, ä, ü, ß
#> 6 README.Rmd   61     35      1 Supported symbols: Ö, Ä, Ü, ö, ä, ü, ß
#> 7 README.Rmd   61     38      1 Supported symbols: Ö, Ä, Ü, ö, ä, ü, ß

Known Issues

  • in RMarkdown documents with markdown output, square brackets ([ and ]) get escaped in a way that can affect the markup of knit_print.grepr(): rstudio/rmarkdown#667
  • double quotes (") are displayed as fancy quotes () in column content
  • currently, there is no check if RStudio is running which might lead to errors in the print method in case another IDE or no IDE is used

Possible improvements

  • add grep_package() which respects the contents of .gitignore and .Rbuildignore by default
  • ignore BLOBS and other problematic file types by default
  • add gsub_umlauts() which applies the suggestions in print_substitution_table() automatically