Skip to content

Commit

Permalink
doc example fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Qile0317 committed Jul 7, 2024
1 parent a416586 commit 356c9bd
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 44 deletions.
1 change: 1 addition & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
# check compatibility with older releases
- {os: ubuntu-latest, r: 'oldrel-1'}
- {os: ubuntu-latest, r: 'oldrel-2'}
- {os: ubuntu-latest, r: 'oldrel-3'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
Expand Down
37 changes: 11 additions & 26 deletions R/packageDevelopment.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
#' This function retrieves keywords from all package documentation files located
#' in the `/man` directory of the specified R package. It can return a unique list
#' of keywords or a frequency distribution of these keywords as a `table` object,
#' sorted by the keys.
#' sorted by the keys.
#'
#' Note that the "internal" keyword is ignored.
#'
#' @param pkg The path to the R package directory.
#' @param asDistribution Logical; if FALSE, returns a character vector of unique keywords. If TRUE, returns a table with the frequency of each keyword.
#'
#' @return If \code{asDistribution} is FALSE, a character vector of unique keywords is returned.
#' @return If \code{asDistribution} is FALSE, a sorted character vector of unique keywords is returned.
#' If \code{asDistribution} is TRUE, a table of keywords and their frequencies is returned.
#' If no keywords were detected, returns a character of length 0.
#' @export
Expand Down Expand Up @@ -78,35 +78,20 @@ getPkgKeywords <- function(pkg = ".", asDistribution = FALSE) {
#' specified sections in their Rd files. May be length 0 if all fulfill criteria.
#' @export
#' @keywords packageDevelopment
#'
#' @examples
#' findMissingRdSections(c("examples", "example"), pkg = ".")
#' try(
#' findMissingRdSections(c("examples", "example"), pkg = "."),
#' silent = TRUE
#' )
#'
findMissingRdSections <- function(
sectionName, pkg = ".", ignore = NULL, .ignore = "-package$"
) {

rdPath <- file.path(pkg, "man")
rdFiles <- list.files(rdPath, pattern = "\\.Rd$", full.names = TRUE)

missingFunctionNames <- character(0)

for (file in rdFiles) {

rdContent <- readLines(file)

sectionMissing <- sapply(paste("\\", sectionName, "{", sep = ""), function(section) {
all(grepl(section, rdContent, fixed = TRUE) == FALSE)
})

if (!any(sectionMissing)) next

missingFunctionNames <- c(
missingFunctionNames,
tools::file_path_sans_ext(basename(file))
)
}

Filter(function(x) !grepl(joinRegex(.ignore, ignore), x), missingFunctionNames)
paste("\\", sectionName, "{", sep = "") %>%
greplDir(file.path(pkg, "man")) %>%
(function(x) names(which(!x))) %>%
basename()
}

#' @rdname findMissingRdSections
Expand Down
20 changes: 14 additions & 6 deletions R/regex.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ joinRegex <- function(...) {
#' names of elements to be removed.
#' @param silent A logical indicating whether to silence a warning if no names are
#' detected.
#'
#'
#' @return The input object with elements removed based on the name regex.
#' @export
#' @keywords regex
#'
#'
#' @examples
#' myList <- list(a = 1, b_test = 2, c = 3, d_test = 4)
#' rmByName(myList, "_test")
#'
rmByName <- function(x, pattern, silent = FALSE) {

assertthat::assert_that(is.character(pattern))
Expand All @@ -54,31 +55,38 @@ rmByName <- function(x, pattern, silent = FALSE) {

#' Search for a Pattern in Files within a Directory
#'
#' @description
#' `r lifecycle::badge("experimental")`
#'
#' The `greplDir` function searches for a specified pattern in all files within a given directory.
#' It allows for optional exclusion of files matching a specified regular expression.
#' Note that all files are assumed to be a single string, with each line joined by the
#' newline character `"\n"`.
#'
#' @param dirPath Character. The path to the directory containing files to be searched.
#' @param fpattern Character. The pattern to search for within the files.
#' @param dirPath Character. The path to the directory containing files to be searched.
#' @param fIgnoreRegex Character. A regular expression to match file names that should be ignored (default is NULL).
#' @param ... Additional arguments passed to [listFiles()], which are passed to [list.files()]
#'
#' @return A named logical vector indicating which files contain the pattern.
#' The names attribute contains the file names.
#' @export
#' @keywords regex
#'
#'
#' @examples
#' \donttest{
#' result <- greplDir(getwd(), "error", "\\.log$")
#' result <- tryCatch(
#' greplDir("error", fIgnoreRegex = "\\.log$"),
#' warning = function(w) c(exFname = TRUE),
#' error = function(e) c(exFname = TRUE)
#' )
#' # its even more useful to use `base::which` on the result to
#' # get matches and mismatches - this returns it with names
#' # by default
#' which(result)
#' which(!result)
#' }
greplDir <- function(dirPath, fpattern, fIgnoreRegex = NULL, ...) {
greplDir <- function(fpattern, dirPath = getwd(), fIgnoreRegex = NULL, ...) {

allFiles <- listFiles(dirPath, ...)

Expand Down
7 changes: 4 additions & 3 deletions R/testing.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
#'
#' @examples
#' \donttest{
#' # Initialize testthat files in the default directories
#' try({
#'
#' initTestthat()
#' # Initialize testthat files in a custom R directory and test directory
#' initTestthat(rDir = "src", testDir = "tests")
#' # Initialize testthat files, ignoring additional patterns
#' initTestthat(ignore = c("^foo", "-bar.R$"))
#'
#' }, silent = TRUE)
#' }
#'
initTestthat <- function(
Expand Down
5 changes: 4 additions & 1 deletion man/findMissingRdSections.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/getPkgKeywords.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions man/greplDir.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions man/initTestthat.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/rmByName.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 356c9bd

Please sign in to comment.