contextFind is a convenient code search tool designed
for R developers who work with multi-file projects such as Shiny
applications, R packages, or complex analysis workflows. Unlike basic
text search, contextFind provides:
Install from CRAN:
Or get the development version from GitHub:
The core function contextFind() searches for text across
your R and Rmd files:
library(contextFind)
# Search for all function definitions in your project
contextFind("<- function")This will output results like:
Found 15 matches for "<- function"
==============================
Match 15 of 15
server.R (line 42)
Last Modified: 2024-10-15 14:32:10
------------------------------
Line 40:
Line 41: # Define server logic
Line 42: server <- function(input, output, session) {
Line 43:
Line 44: # Reactive values
Each match shows: - File name and line number (clickable in supported terminals) - Last modification time of the file - Context lines around the match (default: 2 before and 2 after) - The matching line highlighted
Control how much surrounding code to display:
Limit your search to specific directories:
The contextFind package includes an RStudio addin that
provides an interactive interface for searching your code.
Install and restart RStudio after installing
contextFind
Assign a keyboard shortcut:
Ctrl+Shift+F or
Cmd+Shift+F on Mac)The addin will pre-fill the search box with any highlighted text, making it fast to search for variables or function names you’re currently working with.
contextFind() invisibly returns a list of results that
you can work with:
# Store results
results <- contextFind("ggplot")
# Access the first match
results[[1]]$file # Full file path
results[[1]]$match_line # Line number where match was found
results[[1]]$mtime # File modification time
results[[1]]$context # Named character vector of context lines
# Get all matching files
unique(sapply(results, function(x) x$file))
# Count matches per file
table(sapply(results, function(x) basename(x$file)))
# Find most recently modified files with matches
recent <- results[[length(results)]]
cat("Most recent match in:", basename(recent$file),
"at line", recent$match_line, "\n")Since contextFind uses literal string matching (not
regex), you can search for partial code patterns:
Search for recent changes in modified files:
The results are sorted by modification time, so your recent changes appear last - right where you’re reading in the console.
Use the function in scripts or reports:
# Find all functions that need documentation
results <- contextFind("<- function", contextLines = 0)
# Check if they have roxygen comments
documented <- sapply(results, function(r) {
lines <- readLines(r$file)
line_before <- lines[max(1, r$match_line - 1)]
grepl("#'", line_before)
})
cat(sprintf("%d of %d functions have documentation\n",
sum(documented), length(documented)))Remember that contextFind is case-sensitive:
While contextFind searches both .R and .Rmd files
automatically, you can narrow results by path:
# Search for roxygen @export tags
results <- contextFind("@export", path = "R/")
# Find what functions are exported
exported_functions <- sapply(results, function(r) {
lines <- readLines(r$file)
# Look ahead for function definition
func_line <- lines[r$match_line + 1]
if (grepl("<- function", func_line)) {
trimws(gsub("<-.*", "", func_line))
} else {
NA
}
})
cat("Exported functions:\n")
print(na.omit(exported_functions))# Count UI elements
ui_results <- contextFind("Input(", path = ".")
cat("Number of input widgets:", length(ui_results), "\n")
# Count outputs
output_results <- contextFind("render", path = ".")
cat("Number of render functions:", length(output_results), "\n")
# Find reactive chains
contextFind("reactive(")
contextFind("observe(")If contextFind returns no results:
path parameter points to the correct
directoryrecursive = TRUE if searching
subdirectoriesTo narrow down results:
path = "R/"recursive = FALSE for current directory onlycontextLines to see less contextClickable links require:
file://
linksFound a bug or have a feature request? Please visit the GitHub repository to open an issue or submit a pull request.