Skip to content

Commit

Permalink
add check_folder()
Browse files Browse the repository at this point in the history
  • Loading branch information
ThierryO committed Mar 1, 2023
1 parent 4dc41b3 commit 47bd3e0
Show file tree
Hide file tree
Showing 13 changed files with 319 additions and 11 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export(check_description)
export(check_documentation)
export(check_environment)
export(check_filename)
export(check_folder)
export(check_license)
export(check_lintr)
export(check_package)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
`Config/checklist/communities` and `Config/checklist/keywords`.
* Store author information to reuse when running `create_package()` or
`create_project()`.
* Add `check_folder()`.

# checklist 0.2.6

Expand Down
246 changes: 246 additions & 0 deletions R/check_folder.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
#' Check the folder structure
#'
#' For the time being, this function only checks projects.
#' Keep in mind that R packages have requirements for the folder structure.
#' `check_cran()` checks those requirements.
#'
#' # Recommended folder structure
#'
#' - `source`: contains all `R` scripts and `Rmd` files.
#' - `data`: contains all data files.
#'
#' # `source`
#'
#' A simple project with only `R` scripts or only `Rmd` files can place all the
#' files directly in the `source` folder.
#'
#' More elaborate projects should place in the files in several folders under
#' `source`.
#' Place every `bookdown` document in a dedicated folder.
#' And create an RStudio project for that folder.
#'
#' # `data`
#'
#' Simple projects in which `source` has no sub folders, place `data` at the
#' root of the project.
#' For more elaborate project you must choose between either `data` at the root
#' of the project or `data` as sub folder of the sub folders of `source`.
#' E.g. `source/report/data`.
#'
#' Place the data in an open file format.
#' E.g. `csv`, `txt` or `tsv` for tabular data.
#' We strongly recommend to use `git2rdata::write_vc()` to store such data.
#' Use the [`geopackage`](https://www.geopackage.org/) format for spatial data.
#' Optionally add description of the data as markdown files.
#'
#' @inheritParams read_checklist
#' @family project
#' @export
#' @importFrom fs dir_ls path path_rel
check_folder <- function(x = ".") {
x <- read_checklist(x = x)
if (x$package) {
return(x)
}

dir_ls(x$get_path, type = "directory") |>
path_rel(x$get_path) -> root_dir
suppressWarnings(
path(x$get_path, "source") |>
dir_ls(type = "directory", fail = FALSE) -> source_1
)

paste(open_data_ext, collapse = "|") |>
sprintf(fmt = "\\.(%s)$") -> data_regexp
dir_ls(x$get_path, type = "file", recurse = TRUE, regexp = data_regexp) |>
path_rel(x$get_path) -> data_files
suppressWarnings(
path(x$get_path, "output") |>
dir_ls(
type = "file", recurse = TRUE, regexp = data_regexp, fail = FALSE
) |>
path_rel(x$get_path) -> ignore_data_files
)
data_files <- data_files[!data_files %in% ignore_data_files]
suppressWarnings(
path(x$get_path, "data") |>
dir_ls(
type = "file", recurse = TRUE, regexp = data_regexp, fail = FALSE
) |>
path_rel(x$get_path) -> ignore_data_files
)
data_files <- data_files[!data_files %in% ignore_data_files]
suppressWarnings(
path(x$get_path, "renv") |>
dir_ls(
type = "file", recurse = TRUE, regexp = data_regexp, fail = FALSE
) |>
path_rel(x$get_path) -> ignore_data_files
)
data_files <- data_files[!data_files %in% ignore_data_files]
suppressWarnings(
path(source_1, "data") |>
dir_ls(
type = "file", recurse = TRUE, regexp = data_regexp, fail = FALSE
) |>
path_rel(x$get_path) -> ignore_data_files
)
data_files <- data_files[!data_files %in% ignore_data_files]

paste(graphics_ext, collapse = "|") |>
sprintf(fmt = "\\.(%s)$") -> graphics_regexp
dir_ls(x$get_path, type = "file", recurse = TRUE, regexp = graphics_regexp) |>
path_rel(x$get_path) -> graphics_files
paste0("_files.*", graphics_regexp) |>
dir_ls(x$get_path, type = "file", recurse = TRUE, regexp = _) |>
path_rel(x$get_path) -> ignore_graphics_files
graphics_files <- graphics_files[!graphics_files %in% ignore_graphics_files]
suppressWarnings(
path(x$get_path, "output") |>
dir_ls(
type = "file", recurse = TRUE, regexp = graphics_regexp, fail = FALSE
) |>
path_rel(x$get_path) -> ignore_graphics_files
)
graphics_files <- graphics_files[!graphics_files %in% ignore_graphics_files]
suppressWarnings(
path(x$get_path, "media") |>
dir_ls(
type = "file", recurse = TRUE, regexp = graphics_regexp, fail = FALSE
) |>
path_rel(x$get_path) -> ignore_graphics_files
)
graphics_files <- graphics_files[!graphics_files %in% ignore_graphics_files]
suppressWarnings(
path(source_1, "media") |>
dir_ls(
type = "file", recurse = TRUE, regexp = graphics_regexp, fail = FALSE
) |>
path_rel(x$get_path) -> ignore_graphics_files
)
graphics_files <- graphics_files[!graphics_files %in% ignore_graphics_files]
suppressWarnings(
path(x$get_path, "renv") |>
dir_ls(
type = "file", recurse = TRUE, regexp = graphics_regexp, fail = FALSE
) |>
path_rel(x$get_path) -> ignore_graphics_files
)
graphics_files <- graphics_files[!graphics_files %in% ignore_graphics_files]

dir_ls(x$get_path, recurse = TRUE, regexp = "_(bookdown|quarto)\\.yml$") |>
dirname() |>
c(source_1) |>
vapply(check_data_media, vector(mode = "list", length = 1)) -> data_media_ok
vapply(data_media_ok, "[[", vector(mode = "list", length = 1), "data") |>
unlist() |>
path_rel(x$get_path) -> ignore_data_files
data_files <- data_files[!data_files %in% ignore_data_files]
vapply(data_media_ok, "[[", vector(mode = "list", length = 1), "cover") |>
unlist() |>
path_rel(x$get_path) -> ignore_cover_files
data_files <- data_files[!data_files %in% ignore_cover_files]
vapply(
data_media_ok, "[[", vector(mode = "list", length = 1), "extra_media"
) |>
unlist() |>
path_rel(x$get_path) -> ignore_graphics_files
graphics_files <- graphics_files[!graphics_files %in% ignore_graphics_files]
vapply(data_media_ok, "[[", vector(mode = "list", length = 1), "media") |>
unlist() |>
path_rel(x$get_path) -> ignore_graphics_files
graphics_files <- graphics_files[!graphics_files %in% ignore_graphics_files]

dir_ls(x$get_path, recurse = TRUE, regexp = "_bookdown\\.yml$") |>
dirname() |>
vapply(check_bookdown, vector(mode = "list", length = 1)) |>
unlist() |>
unname() |>
c(
paste(
"A project should only have `data`, `inst`, `output`, `renv` and ",
"`source` as main folder."
)[
!all(root_dir %in% c("data", "inst", "output", "renv", "source"))
],
sprintf(
"Data files found outside of a `data` folder:\n %s",
paste(data_files, collapse = "\n ")
)[length(data_files) > 1],
sprintf(
"Media files found outside of a `media` folder:\n %s",
paste(graphics_files, collapse = "\n ")
)[length(graphics_files) > 1]
) -> warn

c(
"No `source` main folder found"[!"source" %in% root_dir],
"`src` main folder is not allowed"["src" %in% root_dir],
"`source` cannot have `data` as a subfolder"[
"data" %in% basename(source_1)
],
paste(
"Use either a common `data` folder at the root or a `data` folder within",
"the subfolders of `source`."
)["data" %in% root_dir && length(ignore_data_files) > 0],
paste(
"Use either a common `media` folder at the root or a `media` folder",
"within the subfolders of `source`."
)["media" %in% root_dir && length(ignore_graphics_files) > 0]
) -> problems

x$add_error(problems, item = "folder conventions")
x$add_warnings(warn, item = "folder conventions")
return(x)
}

#' @importFrom fs dir_ls
check_bookdown <- function(path) {
rstudio <- dir_ls(path, regexp = "\\.Rproj$")
c(
paste("No Rstudio project found for bookdown", path)[length(rstudio) == 0],
paste(
"Multiple Rstudio projects found for bookdown", path
)[length(rstudio) > 1]
) -> warn
list(warn)
}

#' @importFrom fs dir_ls is_dir path
check_data_media <- function(path) {
cover_ok <- dir_ls(path, type = "file")
cover_ok <- cover_ok[basename(cover_ok) == "cover.txt"]

suppressWarnings(
paste(open_data_ext, collapse = "|") |>
sprintf(fmt = "\\.(%s)$") |>
dir_ls(
path = path(path, "data"), type = "file", recurse = TRUE, fail = FALSE,
regexp = _
) -> data_ok
)

suppressWarnings(
paste(graphics_ext, collapse = "|") |>
sprintf(fmt = "\\.(%s)$") |>
dir_ls(
path = path(path, "media"), type = "file", recurse = TRUE, fail = FALSE,
regexp = _
) -> media_ok
)

suppressWarnings(
paste(graphics_ext, collapse = "|") |>
sprintf(fmt = "\\.(%s)$") |>
dir_ls(
path = path(path, c("_book", "_extensions", "_freeze", "libs")),
type = "file", recurse = TRUE, regexp = _, fail = FALSE
) -> extra_media_ok
)

list(
cover = list(cover_ok), data = list(data_ok),
extra_media = list(extra_media_ok), media = list(media_ok)
) |>
list()
}
2 changes: 1 addition & 1 deletion R/check_project.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @export
#' @importFrom assertthat assert_that
#' @importFrom fs is_file
#' @family source
#' @family project
check_project <- function(x = ".", fail = !interactive(), quiet = FALSE) {
assert_that(
inherits(x, "checklist") || is_file(path(x, "checklist.yml")),
Expand Down
2 changes: 1 addition & 1 deletion R/check_source.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#' @importFrom fs dir_exists file_exists path
#' @importFrom utils file_test
#' @export
#' @family source
#' @family project
check_source <- function(x = ".", fail = !interactive()) {
# nocov start
.Deprecated("check_project", package = "checklist")
Expand Down
2 changes: 1 addition & 1 deletion R/checklist_class.R
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ Please contact the maintainer of the `checklist` package."
available_checks = c(
"checklist", "CITATION", "DESCRIPTION", "documentation",
"R CMD check", "codemeta", "license", "repository secret",
"filename conventions", "lintr", "spelling"
"filename conventions", "folder conventions", "lintr", "spelling"
),
# stores a named logical vector of checked items.
# names must match the available checks
Expand Down
Binary file modified R/sysdata.rda
Binary file not shown.
6 changes: 3 additions & 3 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ reference:
contents:
- check_package
- has_concept("package")
- title: Functions relevant for checking scripts
- title: Functions relevant for checking projects with R and Rmd scripts
contents:
- has_concept("source")
- title: Functions relevant for checking packages and scripts
- has_concept("project")
- title: Functions relevant for checking packages and projects
contents:
- has_concept("both")
- title: Setting up a project use the checklist package
Expand Down
5 changes: 4 additions & 1 deletion data-raw/iso_639_3.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@ sprintf("`%s`", graphics_ext) |>
#' These are fonts files which often require their own file name scheme.
") |>
writeLines("man-roxygen/graphics.R")
save(iso_639_3, email_regexp, graphics_ext, file = "R/sysdata.rda")
open_data_ext <- c("csv", "gpkg", "tsv", "txt")
save(
email_regexp, graphics_ext, iso_639_3, open_data_ext, file = "R/sysdata.rda"
)
1 change: 1 addition & 0 deletions docker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build_push.sh
54 changes: 54 additions & 0 deletions man/check_folder.Rd

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

5 changes: 3 additions & 2 deletions man/check_project.Rd

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

5 changes: 3 additions & 2 deletions man/check_source.Rd

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

0 comments on commit 47bd3e0

Please sign in to comment.