rdd_extract_code {rdocdump} | R Documentation |
Extract R Source Code from a Package
Description
This function extracts the R source code from a package. For installed packages, it retrieves the package namespace and deparses all functions found in the package. For package source directories or archives (non-installed packages), it reads all .R
files from the R
directory and, optionally, from the tests
directory. Optionally, it can include roxygen2 documentation from these files.
Usage
rdd_extract_code(
pkg,
file = NULL,
include_tests = FALSE,
include_roxygen = FALSE,
force_fetch = FALSE,
cache_path = getOption("rdocdump.cache_path"),
keep_files = "none",
repos = getOption("rdocdump.repos", getOption("repos"))
)
Arguments
pkg |
A
|
file |
Optional. Save path for the output text file. If set, the function will return the path to the file instead of the combined text. Defaults to |
include_tests |
|
include_roxygen |
|
force_fetch |
|
cache_path |
A |
keep_files |
A
|
repos |
A |
Value
A single string containing the combined R source code (and, optionally, roxygen2 documentation) from the package.
Examples
# Extract only R source code (excluding roxygen2 documentation) from an installed package.
code <- rdd_extract_code("splines")
cat(substr(code, 1, 1000))
# Extract R source code including roxygen2 documentation from a package source directory.
# set cache directory for `rdocdump`
rdd_set_cache_path(paste0(tempdir(), "/rdocdump_cache"))
local({
code_with_roxygen <- rdd_extract_code(
"ini",
include_roxygen = TRUE,
force_fetch = TRUE,
repos = c("CRAN" = "https://cran.r-project.org")
)
cat(substr(code_with_roxygen, 1, 1000))
})
# Extract R source code from a package source directory,
# including test files but excluding roxygen2 docs.
local({
code_with_tests <- rdd_extract_code(
"ini",
include_roxygen = TRUE,
include_tests = TRUE,
force_fetch = TRUE,
repos = c("CRAN" = "https://cran.r-project.org")
)
cat(substr(code_with_tests, 1, 1000))
})
# clean cache directory
unlink(getOption("rdocdump.cache_path"), recursive = TRUE, force = TRUE)