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.
Arguments
- pkg
A
character
string specifying the package. This can be:an installed package name,
a full path to a package source directory,
a full path to a package archive file (tar.gz), or
a package name not installed (which will then be downloaded from CRAN).
- 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
NULL
.- include_tests
logical
. IfTRUE
, for non-installed packages, the function will also include R source code from thetests
directory. Defaults toFALSE
.- include_roxygen
logical
. IfTRUE
, roxygen2 documentation lines (lines starting with "#'") from R files will be included in the output. Defaults toFALSE
.- force_fetch
logical
. IfTRUE
, the package source will be fetched from CRAN even if the package is installed locally. Default isFALSE
.- cache_path
A
character
string specifying the directory to use as a cache. Defaults to the value ofgetOption("rdocdump.cache_path")
.- repos
A
character
vector of repository URLs. By default, it uses the value ofgetOption("rdocdump.repos")
which sets the repository URLs to the default R repositories and is itself set toc("CRAN" = "https://cloud.r-project.org")
on package load to prevent accidental downloads of pre-built packages from Posit Package Manager and R Universe.
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))
#> --------------------------------------------------------------------------------
#> Function: .onUnload()
#> function (libpath)
#> library.dynam.unload("splines", libpath)
#>
#> --------------------------------------------------------------------------------
#> Function: as.data.frame.xyVector()
#> function (x, ...)
#> data.frame(x = x$x, y = x$y)
#>
#> --------------------------------------------------------------------------------
#> Function: as.polySpline()
#> function (object, ...)
#> polySpline(object, ...)
#>
#> --------------------------------------------------------------------------------
#> Function: asVector()
#> function (object)
#> UseMethod("asVector")
#>
#> --------------------------------------------------------------------------------
#> Function: asVector.xyVector()
#> function (object)
#> object$y
#>
#> --------------------------------------------------------------------------------
#> Function: backSpline()
#> function (object)
#> UseMethod("backSpline")
#>
#> --------------------------------------------------------------------------------
#> F
# Extract R source code including roxygen2 documentation from a package source directory.
# \donttest{
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))
})
#> Fetching package source from CRAN...
#>
#> --------------------------------------------------------------------------------
#> File: ini.R
#>
#> #' Read and parse .ini file to list
#> #'
#> #' @param filepath file to parse
#> #' @param encoding Encoding of filepath parameter, will default to system
#> #' encoding if not specifield
#> #'
#> #' @details Lines starting with '#' or ';' are comments and will not be parsed
#> #'
#> #' @seealso \code{\link{write.ini}}
#> #'
#> #' @return List with length equivalent to number of [sections], each section is
#> #' a new list
#> #'
#> #' @examples
#> #' ## Create a new temp ini for reading
#> #' iniFile <- tempfile(fileext = '.ini')
#> #'
#> #' sink(iniFile)
#> #' cat("; This line is a comment\n")
#> #' cat("# This one too!\n")
#> #' cat("[ Hello World]\n")
#> #' cat("Foo = Bar \n")
#> #' cat("Foo1 = Bar=345 \n")
#> #' sink()
#> #'
#> #' ## Read ini
#> #' checkini <- read.ini(iniFile)
#> #'
#> #' ## Check structure
#> #' checkini
#> #' checkini$`Hello World`$Foo
#> #'
#> #' @export
#> #'
#> read.ini <- function(filepath, encoding = getOption("encoding")) {
#>
#> index <- function(x, run
# 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))
})
#> Fetching package source from CRAN...
#>
#> --------------------------------------------------------------------------------
#> File: ini.R
#>
#> #' Read and parse .ini file to list
#> #'
#> #' @param filepath file to parse
#> #' @param encoding Encoding of filepath parameter, will default to system
#> #' encoding if not specifield
#> #'
#> #' @details Lines starting with '#' or ';' are comments and will not be parsed
#> #'
#> #' @seealso \code{\link{write.ini}}
#> #'
#> #' @return List with length equivalent to number of [sections], each section is
#> #' a new list
#> #'
#> #' @examples
#> #' ## Create a new temp ini for reading
#> #' iniFile <- tempfile(fileext = '.ini')
#> #'
#> #' sink(iniFile)
#> #' cat("; This line is a comment\n")
#> #' cat("# This one too!\n")
#> #' cat("[ Hello World]\n")
#> #' cat("Foo = Bar \n")
#> #' cat("Foo1 = Bar=345 \n")
#> #' sink()
#> #'
#> #' ## Read ini
#> #' checkini <- read.ini(iniFile)
#> #'
#> #' ## Check structure
#> #' checkini
#> #' checkini$`Hello World`$Foo
#> #'
#> #' @export
#> #'
#> read.ini <- function(filepath, encoding = getOption("encoding")) {
#>
#> index <- function(x, run
# }