# Create a directory containing dummy R scripts for illustration purposes.
temp_dir <- file.path(tempdir(TRUE), "find-source")
temp_files <- file.path(temp_dir, c("ex-script-1.R", "ex-script-2.R"))
dir.create(temp_dir, showWarnings = FALSE, recursive = TRUE)
cat(
"tr$translate('Hello, world!')",
"tr$translate('Farewell, world!')",
sep = "\n",
file = temp_files[[1L]])
cat(
"tr$translate('Hello, world!')",
"tr$translate('Farewell, world!')",
sep = "\n",
file = temp_files[[2L]])
# Extract calls to method Translator$translate().
find_source(temp_dir)
find_source_in_files(temp_files)
# Use custom functions.
# For illustrations purposes, assume the package
# exports an hypothetical translate() function.
cat(
"translate('Hello, world!')",
"transtlr::translate('Farewell, world!')",
sep = "\n",
file = temp_files[[1L]])
cat(
"translate('Hello, world!')",
"transltr::translate('Farewell, world!')",
sep = "\n",
file = temp_files[[2L]])
# Extract calls to translate() and transltr::translate().
# Since find_source() and find_source_in_files() work on
# a lexical basis, these are always considered to be two
# distinct functions. They also don't need to exist in the
# R session calling find_source() and find_source_in_files().
find_source(temp_dir, interface = quote(translate))
find_source_in_files(temp_files, interface = quote(transltr::translate))
Run the code above in your browser using DataLab