Skip to content
Snippets Groups Projects
Commit c3ca04ca authored by David Dorchies's avatar David Dorchies
Browse files

feat: add report list generation

Refs #4
parent 1b0d0e54
No related branches found
No related tags found
No related merge requests found
Pipeline #174495 passed
......@@ -11,6 +11,7 @@ RoxygenNote: 7.3.1
Imports:
bookdown,
config,
dplyr,
gert,
httr,
jsonlite,
......
......@@ -8,6 +8,7 @@ export(create_fairify)
export(create_reports)
export(getDataPath)
export(iconv_filename)
export(list_reports)
export(loadConfig)
export(mermaid)
export(mermaid_gen_link)
......@@ -19,3 +20,4 @@ export(updateMermaid)
export(update_fairify)
import(utils)
importFrom(magrittr,"%>%")
importFrom(stats,setNames)
#' List reports with headers
#'
#' @param reports_dir [character] path for the reports
#' @param reports [character] subfolders containing each report. By default,
#' reports are folders in `reports_dir` that match `pattern`
#' @param pattern [character] regex pattern for filtering report directories in
#' `reports_dir` folder (ignored if `reports` is provided)
#' @param index [character] name of the report index file
#'
#' @return A [data.frame] containing one row by report found. Each row contain the following columns:
#'
#' - `report`: the folder name of the report
#' - `path`: the full path of the report folder
#' - and all the metadata located in the header of the report index file: `title`, `author`, ...
#'
#' @importFrom stats setNames
#' @export
#'
list_reports <- function(reports_dir = file.path(pkgload::pkg_path(), "reports"),
reports = list.dirs(reports_dir, full.names = FALSE, recursive = FALSE),
index = "index.Rmd") {
paths <- setNames(file.path(reports_dir, reports), reports)
l <- lapply(paths, get_report_header)
df <- do.call(dplyr::bind_rows, l)
df <- cbind(report = reports, path = paths, df)
return(df)
}
#' Get report header
#'
#' @param path path of the report
#' @inheritParams list_reports
#'
#' @return A [list] with header of the report index file
#' @noRd
#'
get_report_header <- function(path, index = "index.Rmd") {
l <- read_rmd(file.path(path, index))
return(l$header)
}
#' @noRd
#'
#' @title Read R-markdown Documents
#'
#' @description
#' Import Rmd files into objects of class [rmd_doc-class].
#'
#' The function `txt_body()` add a line break at the end of each element of a
#' character vector considering them as single lines.
#'
#' Note that comments will be deleted in the input file.
#'
#' @param file Character value indicating the path and the name to the Rmd file.
#' @param ... Arguments passed by `read_rmd()` to [readLines()].
#' In `txt_body()` they are character values passed to `c()`.
#' @param skip_head Logical value indicating whether the yaml head should be
#' skip or not (this argument is not used at the moment).
#'
#' @return
#' The function `read_rmd()` returns a [rmd_doc-class] object.
#' The function `txt_body()`, a character vector suitable for the parameter
#' `body` in the function [write_rmd()].
#'
#' @examples
#' \dontrun{
#' ## Read pre-installed example
#' ex_document <- read_rmd(file.path(
#' path.package("yamlme"),
#' "taxlistjourney.Rmd"
#' ))
#' }
#' @source https://github.com/kamapu/yamlme/blob/master/R/read_rmd.R
read_rmd <- function(file, ..., skip_head = FALSE) {
file <- txt_body(readLines(file, ...))
if (substr(file[1], 1, 3) != "---") {
message("Rmd file seems to be headless")
yaml_head <- list(body = file)
} else {
idx <- cumsum(grepl("---", file, fixed = TRUE))
yaml_head <- list(header = yaml::yaml.load(paste0(file[idx == 1], collapse = "")))
yaml_head$body <- file[idx > 1][-1]
}
if (skip_head) {
yaml_head$header <- NULL
}
class(yaml_head) <- c("rmd_doc", "list")
return(yaml_head)
}
#' @noRd
txt_body <- function(...) {
return(paste0(c(...), "\n"))
}
......@@ -15,7 +15,7 @@
#'
#' @example man-examples/render_reports.R
#'
render_reports <- function(reports_dir = "./reports",
render_reports <- function(reports_dir = file.path(pkgload::pkg_path(), "reports"),
reports = list.dirs(reports_dir, full.names = FALSE, recursive = FALSE),
publish_dir = "../../public/reports",
...) {
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/list_reports.R
\name{list_reports}
\alias{list_reports}
\title{List reports with headers}
\usage{
list_reports(
reports_dir = file.path(pkgload::pkg_path(), "reports"),
reports = list.dirs(reports_dir, full.names = FALSE, recursive = FALSE),
index = "index.Rmd"
)
}
\arguments{
\item{reports_dir}{\link{character} path for the reports}
\item{reports}{\link{character} subfolders containing each report. By default,
reports are folders in \code{reports_dir} that match \code{pattern}}
\item{index}{\link{character} name of the report index file}
\item{pattern}{\link{character} regex pattern for filtering report directories in
\code{reports_dir} folder (ignored if \code{reports} is provided)}
}
\value{
A \link{data.frame} containing one row by report found. Each row contain the following columns:
\itemize{
\item \code{report}: the folder name of the report
\item \code{path}: the full path of the report folder
\item and all the metadata located in the header of the report index file: \code{title}, \code{author}, ...
}
}
\description{
List reports with headers
}
......@@ -14,7 +14,7 @@ render_report(
)
render_reports(
reports_dir = "./reports",
reports_dir = file.path(pkgload::pkg_path(), "reports"),
reports = list.dirs(reports_dir, full.names = FALSE, recursive = FALSE),
publish_dir = "../../public/reports",
...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment